Add linkbutton field to Gridview

Discussion in 'ASP .Net' started by graphicsxp, Jul 11, 2006.

  1. graphicsxp

    graphicsxp Guest

    Hi,
    I've added a linkbutton field to my gridview. Now I would like that
    when the user clicks on it for a particular row, a server-side function
    should be executed, which takes as parameters the id of the selected
    row. How can I do that ?

    Thanks
     
    graphicsxp, Jul 11, 2006
    #1
    1. Advertising

  2. graphicsxp

    Gary Guest

    You'll want to capture the rowcommand event. The important thing to
    remember is that this event is called at various times during loading,
    and so you need to check the CommandName field of the event argument.

    So if you're definition looks like this:

    <asp:ButtonField CommandName="Select" Text="Sel"></asp:ButtonField>

    Your handler will need to look like this:

    Protected Sub List_RowCommand(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
    List.RowCommand
    If e.CommandName = "Select" Then
    ' Do work here
    End If
    End Sub

    All this being said, a more '2.0' method is to use the CommandField
    construct. So check in MSDN for GridView CommandField and check out
    the SelectedIndexChanged event.

    Thanks,
    Gary


    graphicsxp wrote:
    > Hi,
    > I've added a linkbutton field to my gridview. Now I would like that
    > when the user clicks on it for a particular row, a server-side function
    > should be executed, which takes as parameters the id of the selected
    > row. How can I do that ?
    >
    > Thanks
     
    Gary, Jul 11, 2006
    #2
    1. Advertising

  3. Attached is a very small web site that does what you want (zipped). The
    steps are:

    1. Create the web site
    2. Drag the customers table from Northwind onto the page to create a data
    source control and a grid
    3. use the smart tag in the grid to add a new (command field) column. I
    added a button field and set its command name to Select, its button type to
    Link its header to My Button Field and its text (creatively) to My Button.
    4. Click on the grid and set its RowCommand Event as follows:

    protected void GridView1_RowCommand( object sender, GridViewCommandEventArgs
    e )
    {
    int index = Convert.ToInt32( e.CommandArgument );
    myFunction( index );
    }

    This grabs the index of the row from the CommandArgument property of the
    GridViewCommandEventArgs passed into the event. You can then use it in your
    server side method, thus...

    private void myFunction (int rowIndex)
    {
    Response.Write ("Do some action with row " + rowIndex.ToString());
    }

    Remember, however that the rows are zero-based (the first row is row zero.

    Best of luck.
    --

    Jesse Liberty
    Author, Programmer
    Microsoft MVP


    "graphicsxp" <> wrote in message
    news:...
    > Hi,
    > I've added a linkbutton field to my gridview. Now I would like that
    > when the user clicks on it for a particular row, a server-side function
    > should be executed, which takes as parameters the id of the selected
    > row. How can I do that ?
    >
    > Thanks
    >
     
    Jesse Liberty, Jul 11, 2006
    #3
  4. graphicsxp

    graphicsxp Guest

    Hi,
    thank for the reply.
    I can't see any attachments....

    First probleme is that if I create a commandfield column, there is no
    commandname property ?

    Jesse Liberty wrote:

    > Attached is a very small web site that does what you want (zipped). The
    > steps are:
    >
    > 1. Create the web site
    > 2. Drag the customers table from Northwind onto the page to create a data
    > source control and a grid
    > 3. use the smart tag in the grid to add a new (command field) column. I
    > added a button field and set its command name to Select, its button type to
    > Link its header to My Button Field and its text (creatively) to My Button.
    > 4. Click on the grid and set its RowCommand Event as follows:
    >
    > protected void GridView1_RowCommand( object sender, GridViewCommandEventArgs
    > e )
    > {
    > int index = Convert.ToInt32( e.CommandArgument );
    > myFunction( index );
    > }
    >
    > This grabs the index of the row from the CommandArgument property of the
    > GridViewCommandEventArgs passed into the event. You can then use it in your
    > server side method, thus...
    >
    > private void myFunction (int rowIndex)
    > {
    > Response.Write ("Do some action with row " + rowIndex.ToString());
    > }
    >
    > Remember, however that the rows are zero-based (the first row is row zero.
    >
    > Best of luck.
    > --
    >
    > Jesse Liberty
    > Author, Programmer
    > Microsoft MVP
    >
    >
    > "graphicsxp" <> wrote in message
    > news:...
    > > Hi,
    > > I've added a linkbutton field to my gridview. Now I would like that
    > > when the user clicks on it for a particular row, a server-side function
    > > should be executed, which takes as parameters the id of the selected
    > > row. How can I do that ?
    > >
    > > Thanks
    > >
     
    graphicsxp, Jul 12, 2006
    #4
  5. graphicsxp

    Gary Guest

    Sorry, should have been more specific, but I had to run. What you'd do
    with the commandfield approach is use a GridView something like this:

    <asp:GridView ID="MyList" runat="server" DataKeyNames="MyTableKey">
    <Columns>
    <asp:CommandField SelectText="Select" ShowSelectButton="True" />
    </Columns>
    </asp:GridView>

    Then in the code you'd trap the event like this:

    Protected Sub MyList_SelectedIndexChanged(ByVal sender As Object, ByVal
    e As System.EventArgs) Handles MyList.SelectedIndexChanged
    ' Obtain the database key defined in the DataKeyNames attribute of
    the GridView tag
    Dim theKey As Int32 = MyList.SelectedDataKey.Value
    ' Do work here
    End Sub

    In this way you can easily capture the key associated with the selected
    row. I hope this helps.

    Thanks,
    Gary
     
    Gary, Jul 13, 2006
    #5
  6. graphicsxp

    graphicsxp Guest

    Thanks for replying.

    Actually I've achieved it by doing this:

    <asp:TemplateField ShowHeader="False" >
    <ItemStyle width="50px" HorizontalAlign="Center"
    VerticalAlign="Middle" />
    <ItemTemplate>
    <asp:LinkButton ID="lnkScrap" CommandName="Scrap" runat="server"
    CommandArgument='<%# Bind("CuttingID")%>' Text="Scrap It"
    ></asp:LinkButton>

    </ItemTemplate>
    </asp:TemplateField>


    Protected Sub grdCuttings_RowCommand(ByVal sender As Object, ByVal e
    As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
    grdCuttings.RowCommand
    If e.CommandName = "Scrap" Then
    'I do my stuff here
    End If
    End Sub




    Gary wrote:
    > Sorry, should have been more specific, but I had to run. What you'd do
    > with the commandfield approach is use a GridView something like this:
    >
    > <asp:GridView ID="MyList" runat="server" DataKeyNames="MyTableKey">
    > <Columns>
    > <asp:CommandField SelectText="Select" ShowSelectButton="True" />
    > </Columns>
    > </asp:GridView>
    >
    > Then in the code you'd trap the event like this:
    >
    > Protected Sub MyList_SelectedIndexChanged(ByVal sender As Object, ByVal
    > e As System.EventArgs) Handles MyList.SelectedIndexChanged
    > ' Obtain the database key defined in the DataKeyNames attribute of
    > the GridView tag
    > Dim theKey As Int32 = MyList.SelectedDataKey.Value
    > ' Do work here
    > End Sub
    >
    > In this way you can easily capture the key associated with the selected
    > row. I hope this helps.
    >
    > Thanks,
    > Gary
     
    graphicsxp, Jul 14, 2006
    #6
  7. graphicsxp

    mymyrsx

    Joined:
    Sep 12, 2008
    Messages:
    1
    Hello
    I have the same problem
    I tried the same thing as graphicsxp.
    I put a break point on my sub in my code behind and it seems that doesn't pass there.
    Here is my code
    HTML:
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
             <Columns>
                   <asp:TemplateField >
                    <ItemTemplate>
                        <asp:LinkButton ID="LKBConfirme" CommandName="Select" runat="server">Confirmer</asp:LinkButton>
                        <asp:LinkButton ID="lnkScrap" CommandName="Scrap" runat="server" CommandArgument='<%# Bind("NO_PRODUIT")%>' Text="Scrap It"></asp:LinkButton>
                    </ItemTemplate>
                 </asp:TemplateField>
               </Columns>
            </asp:GridView>
    And here is my code behind

    Private Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
    If e.CommandName = "Select" Then
    Dim Indexligne As Integer = GridView1.SelectedRow.RowIndex
    Dim iNoProduit As Integer = Integer.Parse(DirectCast(GridView1.Rows(Indexligne).FindControl("LBLNumeroPersonne"), Label).Text)
    iNoProduit = iNoProduit
    End If
    If e.CommandName = "Scrap" Then
    'I do my stuff here
    End If

    End Sub

    What am I doing wrong?
     
    mymyrsx, Sep 12, 2008
    #7
  8. graphicsxp

    patil.sandiip

    Joined:
    Dec 7, 2010
    Messages:
    6
    Location:
    India
    Hi check following HTML code.
    define command Argv and Command name in HTML for Linkbutton
    Then In Code behind handle Row Command click Event as
    Thanks.
    Sandeep
     
    patil.sandiip, Dec 9, 2010
    #8
    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. Sathyaish
    Replies:
    3
    Views:
    3,113
    Sathyaish
    Sep 8, 2005
  2. kskewes
    Replies:
    0
    Views:
    1,826
    kskewes
    Apr 30, 2009
  3. Sathyaish

    Linkbutton does not look like a linkbutton

    Sathyaish, Sep 8, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    189
    Sathyaish
    Sep 8, 2005
  4. Sound
    Replies:
    2
    Views:
    454
    Randy Webb
    Sep 28, 2006
  5. jr
    Replies:
    3
    Views:
    426
Loading...

Share This Page