DataGrid Command Doesn't work

A

A.M

Hi,

I have this column in my DataGrid:

<asp:ButtonColumn DataTextField="site_name" HeaderText="Name"
CommandName="Select1">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle>
</asp:ButtonColumn>

This is how I bind the command to event handler:

this.dg.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_Command);

And this is the event handler:

private void dg_Command(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// Should handle all dg's commands including CommandName="Select1"
string s;
s = e.Item.ItemIndex.ToString(); //I have a breakpoint here
}


I have a breakpoint on the event handler and I found that the event handler
never being ran.

Is anything wrong with my way of hanling Command event?

Thanks,
Alan
 
K

Ken Cox [Microsoft MVP]

Hi Alan,

The code seems to work for me. Here's what I was using...


<asp:DataGrid id="dg" runat="server">
<Columns>
<asp:ButtonColumn Text="Select" DataTextField="StringValue"
HeaderText="Name" CommandName="Select1">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle>
</asp:ButtonColumn>
</Columns>
</asp:DataGrid>


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace p4320workcs
{
/// <summary>
/// Summary description for dgevent.
/// </summary>
public class dgevent : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dg;

private void Page_Load(object sender, System.EventArgs e)
{
this.dg.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_Command);
dg.DataSource=CreateDataSource();
dg.DataBind();

}

ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));

for (int i = 0; i < 9; i++)
{
dr = dt.NewRow();

dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * (i + 1);

dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;
}


private void dg_Command(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// Should handle all dg's commands including CommandName="Select1"
string s;
s = e.Item.ItemIndex.ToString(); //I have a breakpoint here
}

#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.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
 
S

Steven Cheng[MSFT]

Hi Alan,

As for the EventHandler for the DataGrid's ItemCommand event, how do you
register to the Grid? Is the code
this.dg.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_Command);

you mentioned genertated by the IDE in property window or you manually add
in the codebehind? IF you manually add it, where do you add it? Page's Init
event or Load event? And are you registering it everytime the page is
loaded rather than only in the if( ! IsPostBack)? You can refer to Ken's
code to see whether there is anything difference. If you still have feel
unclear, please feel free to post here. Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
A

A.M

Thanks for help.

I can see you manually added ItemCommand, but I tried to use IDE to add the
event handler. I am wondering what is wrong with IDE's event handler
creation.

Alan


Ken Cox said:
Hi Alan,

The code seems to work for me. Here's what I was using...


<asp:DataGrid id="dg" runat="server">
<Columns>
<asp:ButtonColumn Text="Select" DataTextField="StringValue"
HeaderText="Name" CommandName="Select1">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle>
</asp:ButtonColumn>
</Columns>
</asp:DataGrid>


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace p4320workcs
{
/// <summary>
/// Summary description for dgevent.
/// </summary>
public class dgevent : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dg;

private void Page_Load(object sender, System.EventArgs e)
{
this.dg.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_Command);
dg.DataSource=CreateDataSource();
dg.DataBind();

}

ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));

for (int i = 0; i < 9; i++)
{
dr = dt.NewRow();

dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * (i + 1);

dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;
}


private void dg_Command(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// Should handle all dg's commands including CommandName="Select1"
string s;
s = e.Item.ItemIndex.ToString(); //I have a breakpoint here
}

#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.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}



A.M said:
Hi,

I have this column in my DataGrid:

<asp:ButtonColumn DataTextField="site_name" HeaderText="Name"
CommandName="Select1">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle>
</asp:ButtonColumn>

This is how I bind the command to event handler:

this.dg.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_Command);

And this is the event handler:

private void dg_Command(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// Should handle all dg's commands including CommandName="Select1"
string s;
s = e.Item.ItemIndex.ToString(); //I have a breakpoint here
}


I have a breakpoint on the event handler and I found that the event
handler
never being ran.

Is anything wrong with my way of hanling Command event?

Thanks,
Alan
 
A

A.M

Thanks for help.

I registered the event by VS.NET's IDE. On the properties window, I click on
the events Icon and clicked on ItemCommand and typed the event handler
method name.
Do we have situations that VS.NET's IDE doesn't generate event handler code
properly, or do we need to do extra work after IDE generates event handler
code?


Alan
 
S

Steven Cheng[MSFT]

Hi Alan,

That really sounds strange. Generally the VS.NET generated event handler
will be registered in the InitalizeComponent method which is called in
Page's OnInit , yes? You can check the page's codebehind to see whether it
does like this and whether it has been set the correct eventhandler
function's name.

Also, as far as I known, there is a issue which will cause the postback
event not fire, that is we rebind the datagrid with datasource everytime
the page is posted back rather than only at the first time it is loaded.
Such as

.......... in Page_Load
{
datagrid.DataSource = xxxxx;
datagrid.DataBind();

}

This will cause the registered event handler not work, you should bind the
datasource as
if(! IsPostBack)
{
//BindData....
}

Also, I think it'll be much helpful if you can generated a sample page to
repro the problem and provide the complete source so that we can perform
some tests on ourside. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
A

A.M

Thanks Steven for help.

I didn't have time to challange with it more, so for that particualr
incident I used link column rather than button and it works fine.
In future if I have to work with buttom columns and DataGrid gives me
problem I will post the source code.

Thanks Again,
Alan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top