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
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
Comment