Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > DataGrid Command Doesn't work

Reply
Thread Tools

DataGrid Command Doesn't work

 
 
A.M
Guest
Posts: n/a
 
      05-28-2004
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.DataGridCommandEventHand ler(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


 
Reply With Quote
 
 
 
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      05-29-2004
Hi Alan,

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


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


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.DataGridCommandEventHand ler(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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.DataGridCommandEventHand ler(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
>
>


 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      05-31-2004
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.DataGridCommandEventHand ler(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




 
Reply With Quote
 
A.M
Guest
Posts: n/a
 
      05-31-2004

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 [Microsoft MVP]" <(E-Mail Removed)> wrote in message
news:O%23%(E-Mail Removed)...
> Hi Alan,
>
> The code seems to work for me. Here's what I was using...
>
>
> <aspataGrid id="dg" runat="server">
> <Columns>
> <asp:ButtonColumn Text="Select" DataTextField="StringValue"
> HeaderText="Name" CommandName="Select1">
> <ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle>
> </asp:ButtonColumn>
> </Columns>
> </aspataGrid>
>
>
> 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.DataGridCommandEventHand ler(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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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.DataGridCommandEventHand ler(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
> >
> >

>



 
Reply With Quote
 
A.M
Guest
Posts: n/a
 
      05-31-2004
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




"Steven Cheng[MSFT]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.DataGridCommandEventHand ler(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
>
>
>
>



 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      06-01-2004
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

 
Reply With Quote
 
A.M
Guest
Posts: n/a
 
      06-01-2004
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





"Steven Cheng[MSFT]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JSpec command line run command doesn't work P. A. Ruby 0 09-04-2009 02:30 AM
Datagrid cancel command does not work when adding new record Julia B ASP .Net Web Controls 2 04-11-2006 12:42 PM
MS work around on text wrapping in a datagrid does not work TB ASP .Net 2 02-22-2006 10:34 PM
Call Datagrid Command column outside datagrid Dave ASP .Net 0 11-20-2003 11:11 AM
copying value of DDL in a Datagrid "pre-edit command" to value in "post edit command" San Diego Guy ASP .Net 0 08-07-2003 08:59 PM



Advertisments