Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > How to call function on ButtonColumn click

Reply
Thread Tools

How to call function on ButtonColumn click

 
 
Lars Pedersen
Guest
Posts: n/a
 
      11-24-2003
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..


 
Reply With Quote
 
 
 
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      11-26-2003
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/de...webcontrol.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" <lsp*_AT_*oncable.dk> wrote in message
news:(E-Mail Removed)...
> 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..
>
>



 
Reply With Quote
 
 
 
 
Lars Pedersen
Guest
Posts: n/a
 
      11-26-2003
Thanks Ken, that was a very useful example you gave me.

-Lars

"Ken Cox [Microsoft MVP]" <(E-Mail Removed)> skrev i en
meddelelse news:(E-Mail Removed)...
> 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/de...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" <lsp*_AT_*oncable.dk> wrote in message
> news:(E-Mail Removed)...
> > 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..
> >
> >

>
>



 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Click anywhere of DataGrid row to fire ButtonColumn event Hardy Wang ASP .Net 2 09-15-2006 01:42 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
running client side script after an <asp:ButtonColumn> click event Steven Spits ASP .Net Datagrid Control 6 06-22-2004 02:28 PM
running client side script after an <asp:ButtonColumn> click event Steven Spits ASP .Net 6 06-22-2004 02:28 PM



Advertisments