473,609 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4930
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:ButtonFiel d CommandName="Se lect" 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.W ebControls.Grid ViewCommandEven tArgs) 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 SelectedIndexCh anged 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_RowCo mmand( object sender, GridViewCommand EventArgs
e )
{
int index = Convert.ToInt32 ( e.CommandArgume nt );
myFunction( index );
}

This grabs the index of the row from the CommandArgument property of the
GridViewCommand EventArgs 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.ToStri ng());
}

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.co mwrote in message
news:11******** *************@m 79g2000cwm.goog legroups.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_RowCo mmand( object sender, GridViewCommand EventArgs
e )
{
int index = Convert.ToInt32 ( e.CommandArgume nt );
myFunction( index );
}

This grabs the index of the row from the CommandArgument property of the
GridViewCommand EventArgs 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.ToStri ng());
}

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.co mwrote in message
news:11******** *************@m 79g2000cwm.goog legroups.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="M yTableKey">
<Columns>
<asp:CommandFie ld SelectText="Sel ect" ShowSelectButto n="True" />
</Columns>
</asp:GridView>

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

Protected Sub MyList_Selected IndexChanged(By Val sender As Object, ByVal
e As System.EventArg s) Handles MyList.Selected IndexChanged
' Obtain the database key defined in the DataKeyNames attribute of
the GridView tag
Dim theKey As Int32 = MyList.Selected DataKey.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:TemplateFi eld ShowHeader="Fal se" >
<ItemStyle width="50px" HorizontalAlign ="Center"
VerticalAlign=" Middle" />
<ItemTemplate >
<asp:LinkButt on ID="lnkScrap" CommandName="Sc rap" runat="server"
CommandArgument ='<%# Bind("CuttingID ")%>' Text="Scrap It"
></asp:LinkButton>
</ItemTemplate>
</asp:TemplateFie ld>
Protected Sub grdCuttings_Row Command(ByVal sender As Object, ByVal e
As System.Web.UI.W ebControls.Grid ViewCommandEven tArgs) Handles
grdCuttings.Row Command
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="M yTableKey">
<Columns>
<asp:CommandFie ld SelectText="Sel ect" ShowSelectButto n="True" />
</Columns>
</asp:GridView>

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

Protected Sub MyList_Selected IndexChanged(By Val sender As Object, ByVal
e As System.EventArg s) Handles MyList.Selected IndexChanged
' Obtain the database key defined in the DataKeyNames attribute of
the GridView tag
Dim theKey As Int32 = MyList.Selected DataKey.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
6866
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 fields). I have set the AllowPaging property to true and Pagesize property to 5. While running the application, in the first page of the gridview things are working fine. ie. On licking the linkbutton values are passed correctly to the target page...
0
1483
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 my linkbutton (which comes from my Eval). If I do this as a hyperlink with querystring instead of a linkbutton, everythings works. My ObjectDataSource was modified to look for a querystring and it works. If I do the same thing witha...
2
2504
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 to the 3 searchable fields) and have the page postback and requery given the selected criterion. For example, the user searches for retainer #1, which has a Tip Width of 5. So he clicks on the 5 and it searches again for all retainers that have a...
3
2014
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" runat="server" OnClientClick="return confirm(Sure?');"
5
5667
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 column value to the sub procedure so it can delete the record? thanks for any help
4
11894
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 retrieved and showed in this GridView produce more than one page of data. A given user clicks on the first row hyper link on the Grid on the first page, then the first row hyper link color changes to look as "visited". Then the user navigates to...
1
2478
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 text value or that command argument value how to get that?
4
17900
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) return;
5
6076
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
8083
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8573
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8547
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8224
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8406
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6062
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2535
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1676
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1393
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.