ASP VB.NET: GridView Dynamic Link Buttons Event Handling

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lilOlMe
    New Member
    • May 2007
    • 74

    ASP VB.NET: GridView Dynamic Link Buttons Event Handling

    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

    [code=vbnet]
    Private Sub DisplayGV()
    Dim dv As DataView = CreateDataSourc e()
    myGV.DataSource = dv
    For i As Integer = 0 To dv.Table.Column s.Count -1
    Dim tf As New TemplateField
    tf.ItemTemplate = New GridViewClickab leTemplate(Data ControlRowType. DataRow, _
    _dv.Table.Colum ns(i).ColumnNam e)
    tf.HeaderTempla te = New GridViewClickab leTemplate(Data ControlRowType. Header, _dv.Table.Colum ns(i).ColumnNam e)
    myGV.Columns.Ad d(tf)
    Next
    myGV.DataBind()

    End Sub


    Private Class GridViewClickab leTemplate
    Implements ITemplate
    Private _templateType As DataControlRowT ype
    Private _columnName As String

    Sub New(ByVal type As DataControlRowT ype, ByVal colname As String)
    _templateType = type
    _columnName = colname

    End Sub

    Public Sub InstantiateIn(B yVal container As System.Web.UI.C ontrol) Implements System.Web.UI.I Template.Instan tiateIn
    Select Case _templateType
    Case DataControlRowT ype.Header
    Dim headerText As New Label
    headerText.Text = _columnName
    container.Contr ols.Add(headerT ext)
    Case DataControlRowT ype.DataRow
    Dim lnkBtn As New LinkButton
    AddHandler lnkBtn.DataBind ing, AddressOf Me.BindLinkButt ons

    container.Contr ols.Add(lnkBtn)
    Case Else
    End Select
    End Sub

    Private Sub BindLinkButtons (ByVal sender As Object, ByVal e As EventArgs)
    Dim lnkBtn As LinkButton = CType(sender, LinkButton)
    Dim row As GridViewRow = CType(lnkBtn.Na mingContainer, GridViewRow)
    Dim content As String = DataBinder.Eval (row.DataItem, _columnName).To String
    lnkBtn.Text = content
    If String.Compare( content, "Shipping") = 0 Then
    lnkBtn.CssClass = "red"
    lnkBtn.CommandN ame = "DisplayInf o"
    lnkBtn.CommandA rgument = 1
    ElseIf String.Compare( content, "Accounting ") = 0 Then
    lnkBtn.CssClass = "green"
    lnkBtn.CommandN ame = "DisplayInf o"
    lnkBtn.CommandA rgument = 2
    ElseIf content.StartsW ith("Receiving" ) Then
    lnkBtn.CssClass = "blue"
    lnkBtn.CommandN ame = "DisplayInf o"
    lnkBtn.CommandA rgument = 3
    End If
    End Sub
    End Class
    [/code]

    I have a sub named DisplayInfo and would have assumed that the CommandName would link the link buttons to this command:

    [code=vbnet]
    Protected Sub DisplayInfo (ByVal sender As Object, ByVal e As EventArgs)
    Dim lnkBtn As LinkButton = CType(sender, LinkButton)
    DisplayTheStuff (lnkBtn.Command Argument)
    End Sub
    [/code]

    However DisplayInfo is never reached...

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

    Thanks

    -LilOlMe
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    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 GridViewCommand EventArgs e parameter. Give this a try and let us know if you get stuck on something.

    Nathan

    Comment

    • lilOlMe
      New Member
      • May 2007
      • 74

      #3
      Originally posted by nateraaaa
      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 GridViewCommand EventArgs e parameter. Give this a try and let us know if you get stuck on something.

      Nathan
      The RowCommand event did not fire.

      Comment

      • nateraaaa
        Recognized Expert Contributor
        • May 2007
        • 664

        #4
        Originally posted by lilOlMe
        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

        Comment

        • lilOlMe
          New Member
          • May 2007
          • 74

          #5
          Originally posted by nateraaaa
          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.
          [code=asp]
          <asp:GridView ID="MyGV" runat="server" CellPadding="4"
          BorderColor="Bl ack" BorderWidth="1" GridLines="both "
          ForeColor="#333 333" PagerSettings-Position=top
          allowpaging=tru e PageSize=10 PagerSettings-Mode=NumericFir stLast
          autogeneratecol umns="false"
          onrowcommand="M yGV_RowCommand" >

          <FooterStyle BackColor="#104 E8B" Font-Bold="True" ForeColor="Whit e" />
          <RowStyle BackColor="#F7F 6F3" ForeColor="#333 333" />
          <PagerStyle BackColor="#5D7 B9D" ForeColor="Whit e" HorizontalAlign ="Center" />
          <HeaderStyle BackColor="#104 E8B" Font-Bold="True" ForeColor="Whit e" />
          </asp:GridView>
          [/code]
          [code=vbnet]
          Protected Sub MyGV_RowCommand (ByVal sender As Object, ByVal e As System.Web.UI.W ebControls.Grid ViewCommandEven tArgs) Handles MyGV.RowCommand
          Dim lnkBtn As LinkButton = CType(sender, LinkButton)
          ShowDetails(lnk Btn.CommandArgu ment)
          End Sub
          [/code]

          Comment

          • lilOlMe
            New Member
            • May 2007
            • 74

            #6
            I've had something similar happen once before...when I had given more than one ListItem the same Value property in a DropDownList... .the SelectedIndexCh anged 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...bu t the are individual LinkButtons...s o I'm not sure it's the same problem.

            Thanks a lot for your help...it's greatly appreciated.
            -LilOlMe

            Comment

            • nateraaaa
              Recognized Expert Contributor
              • May 2007
              • 664

              #7
              Try using the e parameter of the RowCommand event to assign the parameter value of ShowDetails.

              ShowDetails(e.C ommandArgument) ;

              Nathan

              Comment

              • lilOlMe
                New Member
                • May 2007
                • 74

                #8
                Originally posted by nateraaaa
                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 GridViewCommand EventArgs 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]GridViewCommand EventArgs[/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?

                Comment

                • lilOlMe
                  New Member
                  • May 2007
                  • 74

                  #9
                  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.
                  Last edited by lilOlMe; Mar 7 '08, 09:17 PM. Reason: Correction

                  Comment

                  • lilOlMe
                    New Member
                    • May 2007
                    • 74

                    #10
                    I solved the problem...thoug h 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:

                    [code=vbnet]
                    Private Sub BindLinkButtons (ByVal sender As Object, ByVal e As EventArgs)
                    Dim lnkBtn As LinkButton = CType(sender, LinkButton)
                    Dim row As GridViewRow = CType(lnkBtn.Na mingContainer, GridViewRow)
                    Dim content As String = DataBinder.Eval (row.DataItem, _columnName).To String
                    lnkBtn.Text = content
                    If String.Compare( content, "Shipping") = 0 Then
                    lnkBtn.CssClass = "red"
                    lnkBtn.Attribut es.Add("onclick ", "javascript:Set DisplayCode('0' );")
                    ElseIf String.Compare( content, "Accounting ") = 0 Then
                    lnkBtn.CssClass = "green"
                    lnkBtn.Attribut es.Add("onclick ", "javascript:Set DisplayCode('25 5');")
                    ElseIf content.StartsW ith("Receiving" ) Then
                    lnkBtn.CssClass = "blue"
                    lnkBtn.Attribut es.Add("onclick ", "javascript:Set DisplayCode('2' );")
                    End If
                    End Sub
                    [/code]

                    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

                    Comment

                    Working...