Adding a command button to a datagrid

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Group

I am creating and binding my data columns of my datagrid at runtime, and I would like to know how to add a command button to a new column, and when clicked, it needs to fire in a method. Any information would be greatly appreciate

Thanks
Ji

Flying Ace Digital Solution
www.jimmace.co

"The test of success is not what you do when your on top. Success is how high you bounce when you hit bottom." Gen. George S. Patton
 
In html:

<Columns>

<asp:ButtonColumn Text="text_on_commandbutton" ButtonType="PushButton"
CommandName="your_method_name"></asp:ButtonColumn>

</Columns>

In vb:

Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.ItemCommand


If (e.CommandName = "your_method_name") Then

'put your code here

End If

End Sub

Did this help?

Jim Mace said:
Group,

I am creating and binding my data columns of my datagrid at runtime, and I
would like to know how to add a command button to a new column, and when
clicked, it needs to fire in a method. Any information would be greatly
appreciated
Thanks,
Jim

Flying Ace Digital Solutions
www.jimmace.com

"The test of success is not what you do when your on top. Success is how
high you bounce when you hit bottom." Gen. George S. Patton
 
Jim, the key is per Reva's reply: Don't confuse CommandName with any actual code that will run. When *anything* is clicked in the datagrid, ItemCommand will fire, which means that within ItemCommand you need to figure out which button, hyperlink, etc. was clicked. CommandName is just a way for you to distinguish these controls, and from there you can handle the click right there, call another method, etc. Re parameters, you can use CommandArgument, which comes along for the ride from the button, or usually easier is to just go get whichever parameters you would have passed anyway, either that are exposed as variables, something you can retrieve either from the container control or maybe the underlying datasource, etc

----- Jim Mace wrote: ----

I am using the button column object, but when I set the CommandName to my method name, the method does not fire. Also, how would you pass parameters to a method from the button column. Again, I am trying to do this all at runtime

Jim
 
Back
Top