How to call function on ButtonColumn click

L

Lars Pedersen

Hi!

In a datagrid, where I'm building all my columns in codebehind, I have a
ButtonColumn.
When user clicks the button, I want to call a function.
I have tried the CommandName property, but it wont fire.

Example:

ButtonColumn bcol = new ButtonColumn();
bcol.ButtonType = ButtonColumnType.LinkButton;
bcol.HeaderText = "Kurv";
bcol.DataTextField = "ID";
dgProductList.Columns.Add(bcol);

private void DoTask()
{
...Some code
}

Question:
How do I call DoTask(), when clicking the ButtonColums button.

A syntax example would be very helpful to me.
Thanks..
 
K

Ken Cox [Microsoft MVP]

Hi Lars,

You need to catch the button click in the Datagrid's ItemCommand event.
After that, you use the DataGridCommandEventArgs to find out which control
caused the event. If it was the button (as evidenced by the CommandName),
you can call your custom DoTask(), perhaps passing it information about the
row or other values.

There's some code at the bottom of this page that should give you the idea:

http://msdn.microsoft.com/library/d...skaddingbuttoncolumnstodatagridwebcontrol.asp


// C#
private void DataGrid1_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// e.Item is the row of the table where the button was
// clicked.
string productID = e.Item.Cells[2].Text;

if (e.CommandName == "AddToCart")
{
// Add code here to add the productID item to the cart.
}
DataGrid1.DataBind();
}
 
L

Lars Pedersen

Thanks Ken, that was a very useful example you gave me.

-Lars

Ken Cox said:
Hi Lars,

You need to catch the button click in the Datagrid's ItemCommand event.
After that, you use the DataGridCommandEventArgs to find out which control
caused the event. If it was the button (as evidenced by the CommandName),
you can call your custom DoTask(), perhaps passing it information about the
row or other values.

There's some code at the bottom of this page that should give you the idea:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/
vbtskaddingbuttoncolumnstodatagridwebcontrol.asp


// C#
private void DataGrid1_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// e.Item is the row of the table where the button was
// clicked.
string productID = e.Item.Cells[2].Text;

if (e.CommandName == "AddToCart")
{
// Add code here to add the productID item to the cart.
}
DataGrid1.DataBind();
}

Lars Pedersen said:
Hi!

In a datagrid, where I'm building all my columns in codebehind, I have a
ButtonColumn.
When user clicks the button, I want to call a function.
I have tried the CommandName property, but it wont fire.

Example:

ButtonColumn bcol = new ButtonColumn();
bcol.ButtonType = ButtonColumnType.LinkButton;
bcol.HeaderText = "Kurv";
bcol.DataTextField = "ID";
dgProductList.Columns.Add(bcol);

private void DoTask()
{
...Some code
}

Question:
How do I call DoTask(), when clicking the ButtonColums button.

A syntax example would be very helpful to me.
Thanks..
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top