Add linkbutton field to Gridview

G

graphicsxp

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
 
G

Gary

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
 
J

Jesse Liberty

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.
 
G

graphicsxp

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 ?
 
G

Gary

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
 
G

graphicsxp

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=' said:
</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
 
Joined
Sep 12, 2008
Messages
1
Reaction score
0
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?
 
Joined
Dec 7, 2010
Messages
6
Reaction score
0
Hi check following HTML code.
define command Argv and Command name in HTML for Linkbutton
<asp:TemplateField>
<HeaderTemplate>
Edit
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CommandName="editExt" CommandArgument='<%#Eval("CompanyID") %>' Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

Then In Code behind handle Row Command click Event as
If e.commandName = "editExt" Then[/URL]
...

Thanks.
Sandeep
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top