By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,480 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,480 IT Pros & Developers. It's quick & easy.

Add linkbutton field to Gridview

P: n/a
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

Jul 11 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Jul 11 '06 #2

P: n/a
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" <sa*************@googlemail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
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

Jul 11 '06 #3

P: n/a
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" <sa*************@googlemail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
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
Jul 12 '06 #4

P: n/a
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

Jul 13 '06 #5

P: n/a
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
Jul 14 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.