473,507 Members | 2,374 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ASP VB.NET: GridView Dynamic Link Buttons Event Handling

74 New Member
Hi there!

I have generated a GridView that looks something like:

SportName| CompanyNameX |CompanyNameY |CompanyNameZ
Hockey.....| Shipping------------ |Accounting-------- |Shipping------------
BaseBall...| Receiving-----------|Shipping------------|Accounting---------
Etc............| Accounting----------|Receiving---------- |Receiving-----------

Where there are an unknown number of Company Names and an unknown number of Sport Names....these are dynamically generated based on a database.

I've added link buttons to each of the Department Names.
When these are clicked I need to be able to pull up details on the department based on the Company Name and the Sport Name and display them in a panel below the GridView.

My problem is that I have no idea how to handle the link button clicks.
I've created a Template Class to convert the data to be displayed in the GridView into Link buttons...here's a quick snippet

Expand|Select|Wrap|Line Numbers
  1. Private Sub DisplayGV()
  2.    Dim dv As DataView = CreateDataSource()
  3.    myGV.DataSource= dv
  4.    For i As Integer = 0 To dv.Table.Columns.Count -1
  5.         Dim tf As New TemplateField
  6.         tf.ItemTemplate = New GridViewClickableTemplate(DataControlRowType.DataRow, _
  7.                 _dv.Table.Columns(i).ColumnName)
  8.             tf.HeaderTemplate = New GridViewClickableTemplate(DataControlRowType.Header, _dv.Table.Columns(i).ColumnName)
  9.          myGV.Columns.Add(tf)
  10.     Next
  11.     myGV.DataBind()
  12.  
  13. End Sub
  14.  
  15.  
  16.  Private Class GridViewClickableTemplate
  17.         Implements ITemplate
  18.         Private _templateType As DataControlRowType
  19.         Private _columnName As String
  20.  
  21.         Sub New(ByVal type As DataControlRowType, ByVal colname As String)
  22.             _templateType = type
  23.             _columnName = colname
  24.  
  25.         End Sub
  26.  
  27.   Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
  28.             Select Case _templateType
  29.                 Case DataControlRowType.Header
  30.                     Dim headerText As New Label
  31.                     headerText.Text = _columnName
  32.                     container.Controls.Add(headerText)
  33.                 Case DataControlRowType.DataRow
  34.                     Dim lnkBtn As New LinkButton
  35.                     AddHandler lnkBtn.DataBinding, AddressOf Me.BindLinkButtons
  36.  
  37.                     container.Controls.Add(lnkBtn)
  38.                 Case Else
  39.             End Select
  40.         End Sub
  41.  
  42. Private Sub BindLinkButtons(ByVal sender As Object, ByVal e As EventArgs)
  43.             Dim lnkBtn As LinkButton = CType(sender, LinkButton)
  44.             Dim row As GridViewRow = CType(lnkBtn.NamingContainer, GridViewRow)
  45.             Dim content As String = DataBinder.Eval(row.DataItem, _columnName).ToString
  46.             lnkBtn.Text = content
  47.             If String.Compare(content, "Shipping") = 0 Then
  48.                 lnkBtn.CssClass = "red"
  49.                 lnkBtn.CommandName = "DisplayInfo"
  50.                 lnkBtn.CommandArgument = 1
  51.             ElseIf String.Compare(content, "Accounting") = 0 Then
  52.                 lnkBtn.CssClass = "green"
  53.                 lnkBtn.CommandName = "DisplayInfo"
  54.                 lnkBtn.CommandArgument = 2
  55.             ElseIf content.StartsWith("Receiving") Then
  56.                 lnkBtn.CssClass = "blue"
  57.                 lnkBtn.CommandName = "DisplayInfo"
  58.                 lnkBtn.CommandArgument = 3
  59.             End If
  60.         End Sub
  61. End Class
  62.  
I have a sub named DisplayInfo and would have assumed that the CommandName would link the link buttons to this command:

Expand|Select|Wrap|Line Numbers
  1. Protected Sub DisplayInfo (ByVal sender As Object, ByVal e As EventArgs)
  2.         Dim lnkBtn As LinkButton = CType(sender, LinkButton)
  3.         DisplayTheStuff(lnkBtn.CommandArgument)
  4.     End Sub
  5.  
However DisplayInfo is never reached...

How do you handle dynamically created link buttons in a GridView through one event handler?

Thanks

-LilOlMe
Mar 7 '08 #1
9 11877
nateraaaa
663 Recognized Expert Contributor
I think you will need to use the RowCommand event of the GridView to accomplish what you are trying to do. In this event you should be able to check the value of CommandName using GridViewCommandEventArgs e parameter. Give this a try and let us know if you get stuck on something.

Nathan
Mar 7 '08 #2
lilOlMe
74 New Member
I think you will need to use the RowCommand event of the GridView to accomplish what you are trying to do. In this event you should be able to check the value of CommandName using GridViewCommandEventArgs e parameter. Give this a try and let us know if you get stuck on something.

Nathan
The RowCommand event did not fire.
Mar 7 '08 #3
nateraaaa
663 Recognized Expert Contributor
The RowCommand event did not fire.
Do you have the RowCommand event wired up for the GridView? You can right click the GridView in Design View and click Properties then click the lightning bolt at the top of the properties window. Now double click on the RowCommand event. This will create the event in your code behind file. Set a breakpoint in the Page_Load event and also in the RowCommand event. Tell me where your breakpoint hits after the Page_Load event.

Nathan
Mar 7 '08 #4
lilOlMe
74 New Member
Do you have the RowCommand event wired up for the GridView? You can right click the GridView in Design View and click Properties then click the lightning bolt at the top of the properties window. Now double click on the RowCommand event. This will create the event in your code behind file. Set a breakpoint in the Page_Load event and also in the RowCommand event. Tell me where your breakpoint hits after the Page_Load event.

Nathan
It doesn't hit a break point after the Page_Load break point.
Expand|Select|Wrap|Line Numbers
  1.    <asp:GridView ID="MyGV" runat="server" CellPadding="4" 
  2.               BorderColor="Black" BorderWidth="1" GridLines="both"
  3.               ForeColor="#333333" PagerSettings-Position=top
  4.               allowpaging=true PageSize=10 PagerSettings-Mode=NumericFirstLast
  5.               autogeneratecolumns="false"
  6.               onrowcommand="MyGV_RowCommand">
  7.  
  8.                  <FooterStyle BackColor="#104E8B" Font-Bold="True" ForeColor="White" />
  9.                  <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
  10.                  <PagerStyle BackColor="#5D7B9D" ForeColor="White" HorizontalAlign="Center" />
  11.                  <HeaderStyle BackColor="#104E8B" Font-Bold="True" ForeColor="White" />
  12.             </asp:GridView>   
  13.  
Expand|Select|Wrap|Line Numbers
  1.     Protected Sub MyGV_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles MyGV.RowCommand
  2.         Dim lnkBtn As LinkButton = CType(sender, LinkButton)
  3.         ShowDetails(lnkBtn.CommandArgument)
  4.     End Sub
  5.  
Mar 7 '08 #5
lilOlMe
74 New Member
I've had something similar happen once before...when I had given more than one ListItem the same Value property in a DropDownList....the SelectedIndexChanged never fired in that case.

I don't know why it would seem to be the same problem with LinkButtons though, each of them has a unique name, the CommandName and the Arguments may be duplicated...but the are individual LinkButtons...so I'm not sure it's the same problem.

Thanks a lot for your help...it's greatly appreciated.
-LilOlMe
Mar 7 '08 #6
nateraaaa
663 Recognized Expert Contributor
Try using the e parameter of the RowCommand event to assign the parameter value of ShowDetails.

ShowDetails(e.CommandArgument);

Nathan
Mar 7 '08 #7
lilOlMe
74 New Member
  1. <LI style="FONT-SIZE: 8pt; BACKGROUND: #fcfcfc">[font='Courier New', Courier, monospace] [/font]
[font='Courier New', Courier, monospace]Try changing your RowCommand event to use the GridViewCommandEventArgs parameter.[/font]
[font='Courier New', Courier, monospace] [/font]
  1. <LI style="FONT-SIZE: 8pt; BACKGROUND: #fcfcfc">[font='Courier New', Courier, monospace]Protected [color=#0600ff]Sub[/color] MyGV_RowCommand[color=#000000]([/color][color=#ff8000]ByVal[/color] sender [color=#ff8000]As[/color] [color=#ff0000]Object[/color], [color=#ff8000]ByVal[/color] e [color=#ff8000]As[/color] System.[color=#0000ff]Web[/color].[color=#0000ff]UI[/color].[color=#0000ff]WebControls[/color].[color=#0000ff]GridViewCommandEventArgs[/color] [color=#000000])[/color] [color=#ff8000]Handles[/color] MyGV.[color=#0000ff]RowCommand[/color][/font]
    <LI style="FONT-SIZE: 8pt; BACKGROUND: #fcfcfc">[font='Courier New', Courier, monospace] ShowDetails[color=#000000](e[/color].[color=#0000ff]CommandArgument[/color][color=#000000])[/color][/font]
  2. [font='Courier New', Courier, monospace] [color=#0600ff]End[/color] [color=#0600ff]Sub[/color][/font]
[font='Courier New', Courier, monospace][color=#0600ff]Nathan[/color][/font]
I don't understand....
Could you please repost?
Mar 7 '08 #8
lilOlMe
74 New Member
This really isn't a "Row Command" that I'm issuing
I'm issuing a LinkButton to mimic what I would call a "Cell Command"......

Each Cell contains a LinkButton (not ButtonFields...)

Seeing as the RowCommand Event is not fired when I click these link buttons, I'm still wondering how to capture the click events for each of these link buttons.

There's a Lot of them:
There can be 2 - 40 columns, and 1-511 rows....that's a lot of link buttons...
up to (40 X 511) 20440 link buttons.
<edit> correction: there can only be 40X10 at one time because the GridView pages...so 400 link buttons...still a lot</edit>

I just need to check the value of the cell that's been selected...Just need to check the Command Argument of the specific link button clicked.
Mar 7 '08 #9
lilOlMe
74 New Member
I solved the problem...though it feels like somewhat of a workaround.
I never did get the RowCommand event to fire.

When I converted the GridView's content into LinkButtons I added a JavaScript call:

Expand|Select|Wrap|Line Numbers
  1. Private Sub BindLinkButtons(ByVal sender As Object, ByVal e As EventArgs)
  2.            Dim lnkBtn As LinkButton = CType(sender, LinkButton)
  3.            Dim row As GridViewRow = CType(lnkBtn.NamingContainer, GridViewRow)
  4.             Dim content As String = DataBinder.Eval(row.DataItem, _columnName).ToString
  5.             lnkBtn.Text = content
  6.             If String.Compare(content, "Shipping") = 0 Then
  7.                 lnkBtn.CssClass = "red"
  8.                 lnkBtn.Attributes.Add("onclick", "javascript:SetDisplayCode('0');")
  9.             ElseIf String.Compare(content, "Accounting") = 0 Then
  10.                 lnkBtn.CssClass = "green"
  11.                 lnkBtn.Attributes.Add("onclick", "javascript:SetDisplayCode('255');")
  12.             ElseIf content.StartsWith("Receiving") Then
  13.                 lnkBtn.CssClass = "blue"
  14.                 lnkBtn.Attributes.Add("onclick", "javascript:SetDisplayCode('2');")
  15.             End If
  16. End Sub
  17.  
The JavaScript function sets a hidden field with the "display code" and I check that in the PageLoad and automatically display the thing that needs to be displayed.

Thanks again for your help!

-LilOlMe
Mar 7 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
447
by: brian | last post by:
'visual studio 2002 pro 1.0 framwork' I am creating dynamic Link buttons on my intranet site and assigning a click_event to them. A user clicks a link and I call the method to recreate the...
3
1844
by: WebBuilder451 | last post by:
I have a series of dynamic link buttons created based upon a datareader. I've added a click event and it calls the sub ok: example: "while loop through the reader" Dim ltrCtrl As New...
0
524
by: NateDawg | last post by:
Ok, I’ve noticed a few gridview problems floating around the forum. Everyone wants to do a java confirmation box when a user clicks the delete button. Fair enough, basic user design rules state...
1
9331
by: Miguel Dias Moura | last post by:
Hello, I have a GridView in my page which is created in runtime. It works fine. My page has 2 Asp Buttons: - The HIDE button makes GridView.Visible = False; - The SHOW button makes...
0
1185
by: Achim Domma (SyynX Solutions GmbH) | last post by:
Hi, in a repeater control I can create a postback link like this: <a href=<%# DataBinder.Eval(Container.DataItem, "EditLink") %>></a> The EditLink property of the data item uses...
4
7332
by: Bishop | last post by:
I have a number of simple select queries that a user needs to be able to execute and display the data on the screen in a gridview. My thought was that I could use a Gridview set to dymanically...
6
28099
by: Kevin Attard | last post by:
I am using a GridView inside a UserControl which has a template column for deleting the rows. Before databinding the gridview i am attaching the RowCommand and RowDataBound event. I am using the...
1
10380
by: Evan M. | last post by:
Here's my GridView and my SqlDataSource <asp:GridView ID="ContactHistoryGrid" runat="server" AutoGenerateColumns="False" DataSourceID="ContactHistoryDS" DataKeyNames="JobHistoryID"...
0
3154
by: jrnail23 | last post by:
I have a user control which contains an UpdatePanel, which contains a MultiView inside, with a GridView in one of the views. In my GridView, I have a ButtonField which is supposed to trigger a...
0
7223
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
7319
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,...
1
7031
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...
0
7485
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...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5042
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...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1542
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 ...

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.