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
 

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

Top