473,386 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Add linkbutton field to Gridview

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
5 4920
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ram | last post by:
Hi All, I am binding a GridView control using object data source. I have included a linkbutton with in the gridview and using its commandname property , I am passing few values( id and name...
0
by: jm | last post by:
I have a Gridview with a LinkButton. The button has text from my database. It is not the datakey of the database, just free form text. I want to populate another gridview based upon the data in...
2
by: CJM | last post by:
I have a page that allows the user to search a DB by querying one of 3 fields. When results are returned, I want the user to be able to click a value in one of three columns (that directly relate...
3
by: Ben | last post by:
Hi, i try to reach in code-behind a linkbutton embedded into an ItemTemplate element of a gridview. But i'm stuck .... <Columns> <asp:TemplateField><ItemTemplate> <asp:LinkButton ID="lb1"...
5
by: Gabe Matteson | last post by:
I would like the linkbutton when it is clicked to exectute a delete sql statement on the current page. etc. onclick="btndelete_onclick" how do i get the linkbutton to pass the database id...
4
by: Jeff | last post by:
Hi, I have a ASP.NET 2.0 Web Application. Many of the pages use the ASP.NET GridView with paging and sorting. One of the columns of this Gridview is a template column (LinkButton). The data being...
1
by: ganesh22 | last post by:
Hi, Iam using Gridview in Asp.net, for that i added one linkbutton using template field, for linkbutton iam added command argument now i want when i click that linkbutton i want that linkbutton...
4
by: jack | last post by:
Hi, Consider the following handler: protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e) { GridViewRow row = e.Row; if (row.RowType != DataControlRowType.DataRow)...
5
by: Eric | last post by:
Hi, there are several gridviews all with a linkbutton which all trigger the same procedure. I need to know from which gridview the linkbutton has been clicked. Thanks Eric Here the code:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.