How to call function on ButtonColumn click

Discussion in 'ASP .Net Web Controls' started by Lars Pedersen, Nov 24, 2003.

  1. 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..
     
    Lars Pedersen, Nov 24, 2003
    #1
    1. Advertising

  2. 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();
    }

    "Lars Pedersen" <lsp*_AT_*oncable.dk> wrote in message
    news:...
    > 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..
    >
    >
     
    Ken Cox [Microsoft MVP], Nov 26, 2003
    #2
    1. Advertising

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

    -Lars

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

    >
    >
     
    Lars Pedersen, Nov 26, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven Spits
    Replies:
    6
    Views:
    4,536
    z. f.
    Jun 22, 2004
  2. Hardy Wang
    Replies:
    2
    Views:
    8,031
    Hardy Wang
    Sep 15, 2006
  3. grbgooglefan
    Replies:
    2
    Views:
    459
    Pascal Bourguignon
    Jan 30, 2008
  4. Steven Spits

    running client side script after an <asp:ButtonColumn> click event

    Steven Spits, Jun 22, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    6
    Views:
    218
    z. f.
    Jun 22, 2004
  5. Lazlo Woodbine
    Replies:
    1
    Views:
    131
    Lazlo Woodbine
    May 14, 2004
Loading...

Share This Page