Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net Datagrid Control (http://www.velocityreviews.com/forums/f60-asp-net-datagrid-control.html)
-   -   ItemCommand problems (http://www.velocityreviews.com/forums/t760167-itemcommand-problems.html)

Mark Kelly 01-23-2004 01:30 AM

ItemCommand problems
 
Hi, I don't seem to be able to get a imagebutton (within a template column)
to fire an itemcommand event when clicked. Looking through this NG, it seems
like the same problem has been raised many times, except in most cases the
problem occurs as the datagrid's databind method is being called on
Postback - something that definately isn't happening in my code.

I also have a standard ButtonColumn labled 'test' - which when clicked seems
to work fine, i.e the dgFileList_ItemCommand fucntion is executed.

Can anybody tell me why the event isn't being raised by a click of the
imagebutton (it's definately doing a postback, but thats all)? Is it
anything to do with the fact its a user control?

Any help is greatly appreciated

Here are the relevant sections of my code:
..ascx
<asp:datagrid id="dgFileList" Enabled="False" AutoGenerateColumns="False"
runat="server">
<Columns>
<asp:ButtonColumn Text="test"></asp:ButtonColumn>
<asp:TemplateColumn HeaderText="Image">
<ItemTemplate>
<asp:ImageButton CommandName="ClickMe" ImageUrl='<%#
DataBinder.Eval(Container.DataItem, "ThumbURL")%>' id="ImageSelectButton"
runat="server">
</asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>


..ascx.cs
public abstract class FlieList : System.Web.UI.UserControl

{

protected System.Web.UI.WebControls.DataGrid dgFileList;

private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here

//this.ShowFiles();

}

public void ShowFiles()

{

//Datagrid - Get file list

Files files = new Files();

FileItem[] fileList = files.GetFileList();

dgFileList.DataSource = fileList;

dgFileList.DataBind();

}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: This call is required by the ASP.NET Web Form Designer.

//

InitializeComponent();

base.OnInit(e);

}


/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.dgFileList.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dgFileList_ItemCo
mmand);

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

private void dgFileList_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

string s = "test";

string y = "anothertest";

if(e.CommandName=="ClickMe")

{

s = "test";

y = "anothertest";



}

s += y;

}







Bob Clegg 01-23-2004 08:35 AM

Re: ItemCommand problems
 
Hi Mark,
I have a similar problem which seemed to be solved by allowing databinding
in the Postback.
ie. Initially My pageload only had databinding for the grid in the not
ispostback section of an if statement.
Grid worked OK till I decided to programmatically add a template column with
a item button control.
Initial load was OK but press the button and the column disappeared and no
way was it firing the ItemCommand event.
In desperation I added a databind to the postback side of the If statement.
As soon as I did that the column stayed on screen and the test code in the
ItemCommand event handler fired.

I am new to Web development and Webforms so I don't know yet if this is a
fix or just a dead end that will fail with further testing.

FWIW.
regards
Bob

"Mark Kelly" <thedude@thedudehatesspam.com> wrote in message
news:OzDP6BV4DHA.2612@tk2msftngp13.phx.gbl...
> Hi, I don't seem to be able to get a imagebutton (within a template

column)
> to fire an itemcommand event when clicked. Looking through this NG, it

seems
> like the same problem has been raised many times, except in most cases the
> problem occurs as the datagrid's databind method is being called on
> Postback - something that definately isn't happening in my code.
>
> I also have a standard ButtonColumn labled 'test' - which when clicked

seems
> to work fine, i.e the dgFileList_ItemCommand fucntion is executed.
>
> Can anybody tell me why the event isn't being raised by a click of the
> imagebutton (it's definately doing a postback, but thats all)? Is it
> anything to do with the fact its a user control?
>
> Any help is greatly appreciated
>
> Here are the relevant sections of my code:
> .ascx
> <asp:datagrid id="dgFileList" Enabled="False" AutoGenerateColumns="False"
> runat="server">
> <Columns>
> <asp:ButtonColumn Text="test"></asp:ButtonColumn>
> <asp:TemplateColumn HeaderText="Image">
> <ItemTemplate>
> <asp:ImageButton CommandName="ClickMe" ImageUrl='<%#
> DataBinder.Eval(Container.DataItem, "ThumbURL")%>' id="ImageSelectButton"
> runat="server">
> </asp:ImageButton>
> </ItemTemplate>
> </asp:TemplateColumn>
>
>
> .ascx.cs
> public abstract class FlieList : System.Web.UI.UserControl
>
> {
>
> protected System.Web.UI.WebControls.DataGrid dgFileList;
>
> private void Page_Load(object sender, System.EventArgs e)
>
> {
>
> // Put user code to initialize the page here
>
> //this.ShowFiles();
>
> }
>
> public void ShowFiles()
>
> {
>
> //Datagrid - Get file list
>
> Files files = new Files();
>
> FileItem[] fileList = files.GetFileList();
>
> dgFileList.DataSource = fileList;
>
> dgFileList.DataBind();
>
> }
>
> #region Web Form Designer generated code
>
> override protected void OnInit(EventArgs e)
>
> {
>
> //
>
> // CODEGEN: This call is required by the ASP.NET Web Form Designer.
>
> //
>
> InitializeComponent();
>
> base.OnInit(e);
>
> }
>
>
> /// <summary>
>
> /// Required method for Designer support - do not modify
>
> /// the contents of this method with the code editor.
>
> /// </summary>
>
> private void InitializeComponent()
>
> {
>
> this.dgFileList.ItemCommand += new
>

System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dgFileList_ItemCo
> mmand);
>
> this.Load += new System.EventHandler(this.Page_Load);
>
> }
>
> #endregion
>
> private void dgFileList_ItemCommand(object source,
> System.Web.UI.WebControls.DataGridCommandEventArgs e)
>
> {
>
> string s = "test";
>
> string y = "anothertest";
>
> if(e.CommandName=="ClickMe")
>
> {
>
> s = "test";
>
> y = "anothertest";
>
>
>
> }
>
> s += y;
>
> }
>
>
>
>
>
>




Mark Kelly 01-26-2004 10:53 PM

Re: ItemCommand problems
 
Hi Bob

Thanks for your response, I tried your suggestion, but unfortunately it
didn't work. I'm wondering if the problem is occuring because this DataGrid
is within a custom control and whether I may need to override the
CreateChildControls() method??? I really don't know enough about this so
I'll have to investigate further. If anybody has any suggestions I'd greatly
appreciate it.

Thanks

Mark


"Bob Clegg" <bclegg@clear.net.nz> wrote in message
news:urXzauY4DHA.536@tk2msftngp13.phx.gbl...
> Hi Mark,
> I have a similar problem which seemed to be solved by allowing databinding
> in the Postback.
> ie. Initially My pageload only had databinding for the grid in the not
> ispostback section of an if statement.
> Grid worked OK till I decided to programmatically add a template column

with
> a item button control.
> Initial load was OK but press the button and the column disappeared and no
> way was it firing the ItemCommand event.
> In desperation I added a databind to the postback side of the If

statement.
> As soon as I did that the column stayed on screen and the test code in the
> ItemCommand event handler fired.
>
> I am new to Web development and Webforms so I don't know yet if this is a
> fix or just a dead end that will fail with further testing.
>
> FWIW.
> regards
> Bob
>
> "Mark Kelly" <thedude@thedudehatesspam.com> wrote in message
> news:OzDP6BV4DHA.2612@tk2msftngp13.phx.gbl...
> > Hi, I don't seem to be able to get a imagebutton (within a template

> column)
> > to fire an itemcommand event when clicked. Looking through this NG, it

> seems
> > like the same problem has been raised many times, except in most cases

the
> > problem occurs as the datagrid's databind method is being called on
> > Postback - something that definately isn't happening in my code.
> >
> > I also have a standard ButtonColumn labled 'test' - which when clicked

> seems
> > to work fine, i.e the dgFileList_ItemCommand fucntion is executed.
> >
> > Can anybody tell me why the event isn't being raised by a click of the
> > imagebutton (it's definately doing a postback, but thats all)? Is it
> > anything to do with the fact its a user control?
> >
> > Any help is greatly appreciated
> >
> > Here are the relevant sections of my code:
> > .ascx
> > <asp:datagrid id="dgFileList" Enabled="False"

AutoGenerateColumns="False"
> > runat="server">
> > <Columns>
> > <asp:ButtonColumn Text="test"></asp:ButtonColumn>
> > <asp:TemplateColumn HeaderText="Image">
> > <ItemTemplate>
> > <asp:ImageButton CommandName="ClickMe" ImageUrl='<%#
> > DataBinder.Eval(Container.DataItem, "ThumbURL")%>'

id="ImageSelectButton"
> > runat="server">
> > </asp:ImageButton>
> > </ItemTemplate>
> > </asp:TemplateColumn>
> >
> >
> > .ascx.cs
> > public abstract class FlieList : System.Web.UI.UserControl
> >
> > {
> >
> > protected System.Web.UI.WebControls.DataGrid dgFileList;
> >
> > private void Page_Load(object sender, System.EventArgs e)
> >
> > {
> >
> > // Put user code to initialize the page here
> >
> > //this.ShowFiles();
> >
> > }
> >
> > public void ShowFiles()
> >
> > {
> >
> > //Datagrid - Get file list
> >
> > Files files = new Files();
> >
> > FileItem[] fileList = files.GetFileList();
> >
> > dgFileList.DataSource = fileList;
> >
> > dgFileList.DataBind();
> >
> > }
> >
> > #region Web Form Designer generated code
> >
> > override protected void OnInit(EventArgs e)
> >
> > {
> >
> > //
> >
> > // CODEGEN: This call is required by the ASP.NET Web Form Designer.
> >
> > //
> >
> > InitializeComponent();
> >
> > base.OnInit(e);
> >
> > }
> >
> >
> > /// <summary>
> >
> > /// Required method for Designer support - do not modify
> >
> > /// the contents of this method with the code editor.
> >
> > /// </summary>
> >
> > private void InitializeComponent()
> >
> > {
> >
> > this.dgFileList.ItemCommand += new
> >

>

System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dgFileList_ItemCo
> > mmand);
> >
> > this.Load += new System.EventHandler(this.Page_Load);
> >
> > }
> >
> > #endregion
> >
> > private void dgFileList_ItemCommand(object source,
> > System.Web.UI.WebControls.DataGridCommandEventArgs e)
> >
> > {
> >
> > string s = "test";
> >
> > string y = "anothertest";
> >
> > if(e.CommandName=="ClickMe")
> >
> > {
> >
> > s = "test";
> >
> > y = "anothertest";
> >
> >
> >
> > }
> >
> > s += y;
> >
> > }
> >
> >
> >
> >
> >
> >

>
>




Mark Kelly 01-26-2004 11:06 PM

Re: ItemCommand problems
 
Sorry, I meant User Control, not Custom Control!

"Mark Kelly" <thedude@thedudehatesspam.com> wrote in message
news:OKPg$8F5DHA.2300@TK2MSFTNGP10.phx.gbl...
> Hi Bob
>
> Thanks for your response, I tried your suggestion, but unfortunately it
> didn't work. I'm wondering if the problem is occuring because this

DataGrid
> is within a custom control and whether I may need to override the
> CreateChildControls() method??? I really don't know enough about this so
> I'll have to investigate further. If anybody has any suggestions I'd

greatly
> appreciate it.
>
> Thanks
>
> Mark
>
>
> "Bob Clegg" <bclegg@clear.net.nz> wrote in message
> news:urXzauY4DHA.536@tk2msftngp13.phx.gbl...
> > Hi Mark,
> > I have a similar problem which seemed to be solved by allowing

databinding
> > in the Postback.
> > ie. Initially My pageload only had databinding for the grid in the not
> > ispostback section of an if statement.
> > Grid worked OK till I decided to programmatically add a template column

> with
> > a item button control.
> > Initial load was OK but press the button and the column disappeared and

no
> > way was it firing the ItemCommand event.
> > In desperation I added a databind to the postback side of the If

> statement.
> > As soon as I did that the column stayed on screen and the test code in

the
> > ItemCommand event handler fired.
> >
> > I am new to Web development and Webforms so I don't know yet if this is

a
> > fix or just a dead end that will fail with further testing.
> >
> > FWIW.
> > regards
> > Bob
> >
> > "Mark Kelly" <thedude@thedudehatesspam.com> wrote in message
> > news:OzDP6BV4DHA.2612@tk2msftngp13.phx.gbl...
> > > Hi, I don't seem to be able to get a imagebutton (within a template

> > column)
> > > to fire an itemcommand event when clicked. Looking through this NG, it

> > seems
> > > like the same problem has been raised many times, except in most cases

> the
> > > problem occurs as the datagrid's databind method is being called on
> > > Postback - something that definately isn't happening in my code.
> > >
> > > I also have a standard ButtonColumn labled 'test' - which when clicked

> > seems
> > > to work fine, i.e the dgFileList_ItemCommand fucntion is executed.
> > >
> > > Can anybody tell me why the event isn't being raised by a click of the
> > > imagebutton (it's definately doing a postback, but thats all)? Is it
> > > anything to do with the fact its a user control?
> > >
> > > Any help is greatly appreciated
> > >
> > > Here are the relevant sections of my code:
> > > .ascx
> > > <asp:datagrid id="dgFileList" Enabled="False"

> AutoGenerateColumns="False"
> > > runat="server">
> > > <Columns>
> > > <asp:ButtonColumn Text="test"></asp:ButtonColumn>
> > > <asp:TemplateColumn HeaderText="Image">
> > > <ItemTemplate>
> > > <asp:ImageButton CommandName="ClickMe" ImageUrl='<%#
> > > DataBinder.Eval(Container.DataItem, "ThumbURL")%>'

> id="ImageSelectButton"
> > > runat="server">
> > > </asp:ImageButton>
> > > </ItemTemplate>
> > > </asp:TemplateColumn>
> > >
> > >
> > > .ascx.cs
> > > public abstract class FlieList : System.Web.UI.UserControl
> > >
> > > {
> > >
> > > protected System.Web.UI.WebControls.DataGrid dgFileList;
> > >
> > > private void Page_Load(object sender, System.EventArgs e)
> > >
> > > {
> > >
> > > // Put user code to initialize the page here
> > >
> > > //this.ShowFiles();
> > >
> > > }
> > >
> > > public void ShowFiles()
> > >
> > > {
> > >
> > > //Datagrid - Get file list
> > >
> > > Files files = new Files();
> > >
> > > FileItem[] fileList = files.GetFileList();
> > >
> > > dgFileList.DataSource = fileList;
> > >
> > > dgFileList.DataBind();
> > >
> > > }
> > >
> > > #region Web Form Designer generated code
> > >
> > > override protected void OnInit(EventArgs e)
> > >
> > > {
> > >
> > > //
> > >
> > > // CODEGEN: This call is required by the ASP.NET Web Form Designer.
> > >
> > > //
> > >
> > > InitializeComponent();
> > >
> > > base.OnInit(e);
> > >
> > > }
> > >
> > >
> > > /// <summary>
> > >
> > > /// Required method for Designer support - do not modify
> > >
> > > /// the contents of this method with the code editor.
> > >
> > > /// </summary>
> > >
> > > private void InitializeComponent()
> > >
> > > {
> > >
> > > this.dgFileList.ItemCommand += new
> > >

> >

>

System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dgFileList_ItemCo
> > > mmand);
> > >
> > > this.Load += new System.EventHandler(this.Page_Load);
> > >
> > > }
> > >
> > > #endregion
> > >
> > > private void dgFileList_ItemCommand(object source,
> > > System.Web.UI.WebControls.DataGridCommandEventArgs e)
> > >
> > > {
> > >
> > > string s = "test";
> > >
> > > string y = "anothertest";
> > >
> > > if(e.CommandName=="ClickMe")
> > >
> > > {
> > >
> > > s = "test";
> > >
> > > y = "anothertest";
> > >
> > >
> > >
> > > }
> > >
> > > s += y;
> > >
> > > }
> > >
> > >
> > >
> > >
> > >
> > >

> >
> >

>
>




werddomain 02-16-2012 07:17 AM

Hi all,
I have this problem in a repeater when i use ImageButton ...
i have search the net for this solution when LinkButton work, but not ImageButton ...

then i think, LinkButton work? so i will use it :)
<asp:LinkButton CommandName="Filter" CommandArgument='<%# Eval("ID") %>' Text="" runat="server" >
<asp:image imageurl='<%#Eval("Img") %>' runat="server"/>

</asp:LinkButton>
So, the image is inside the <A> tag

have fun :)

WerdDomain


All times are GMT. The time now is 12:27 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.