468,733 Members | 1,474 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,733 developers. It's quick & easy.

ASP VB.NET: GridView Dynamic Link Buttons Event Handling

74
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 11331
nateraaaa
663 Expert 512MB
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
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 Expert 512MB
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
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
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 Expert 512MB
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
  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
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
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

Post your reply

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

Similar topics

1 post views Thread by brian | last post: by
3 posts views Thread by WebBuilder451 | last post: by
reply views Thread by NateDawg | last post: by
reply views Thread by Achim Domma (SyynX Solutions GmbH) | last post: by
4 posts views Thread by Bishop | last post: by
6 posts views Thread by Kevin Attard | last post: by
1 post views Thread by Evan M. | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.