EDN Admin
Well-known member
Im having a problem asp.net front and vb.net code behind. My problem is I am dynamically adding buttons to gridview but when I click the buttons the rowCommand is not working. Ive placed the code inside the Asp.net and also inside
the vb.net code behind.
<pre>Asp.net:
<asp:GridView ID="gvReport" runat="server" HorizontalAlign=Center BorderStyle="Solid" BorderWidth="3px" CellPadding="5"
AutoGenerateColumns=true BorderColor="#4D8EB7" ShowHeader="False"
OnRowDataBound="gvReport_RowDataBound
<RowStyle BackColor="#E6F1FF" ForeColor="Black" Font-Size="12pt" BorderColor="#4D8EB7" BorderStyle="Solid" BorderWidth="8px" Height="20px"
HorizontalAlign="Center" Wrap="False" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#4D8EB7" Font-Bold="False" ForeColor="White" Font-Names="Tahoma" Font-Size="10pt" BorderColor="#4D8EB7"
BorderStyle="Solid" BorderWidth="8px" />
<Columns>
</Columns>
</asp:GridView>
vb code:
Protected Sub gvReport_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.RowIndex = 1 Then
For i As Integer = 1 To e.Row.Cells.Count - 1
Dim tb8 As New TextBox
tb8.Width = 30
tb8.Font.Size = 12
tb8.Font.Name = "Tahoma"
tb8.Text = e.Row.Cells(i).Text
e.Row.Cells(i).Controls.Add(tb8)
Next
End If
If e.Row.RowIndex = 2 Then
For i As Integer = 1 To e.Row.Cells.Count - 1
If e.Row.Cells(i).Text <> "N/A" Then
Dim btn1 As New Button
btn1.Width = 50
btn1.Font.Size = 12
btn1.BorderStyle = BorderStyle.None
btn1.Font.Underline = True
btn1.BackColor = Color.FromArgb(230, 241, 255)
btn1.Font.Name = "Tahoma"
btn1.Text = e.Row.Cells(i).Text & "%"
e.Row.Cells(i).Controls.Add(btn1)
End If
Next
End If
End If
End Sub
Public Sub gvReport_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles gvReport.RowCommand
Dim currentCommand As String = e.CommandName
Dim currentRowIndex As Integer = Int32.Parse(e.CommandArgument)
Dim ProductID As String = gvReport.DataKeys(currentRowIndex).Value
gvReport = CType(sender, GridView)
MsgBox("Command: " & currentCommand & "<br><br> Row Index: " & currentRowIndex.ToString & "Product ID: " & ProductID)
End Sub
[/code]
<br/>
<br/>
View the full article
the vb.net code behind.
<pre>Asp.net:
<asp:GridView ID="gvReport" runat="server" HorizontalAlign=Center BorderStyle="Solid" BorderWidth="3px" CellPadding="5"
AutoGenerateColumns=true BorderColor="#4D8EB7" ShowHeader="False"
OnRowDataBound="gvReport_RowDataBound
<RowStyle BackColor="#E6F1FF" ForeColor="Black" Font-Size="12pt" BorderColor="#4D8EB7" BorderStyle="Solid" BorderWidth="8px" Height="20px"
HorizontalAlign="Center" Wrap="False" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#4D8EB7" Font-Bold="False" ForeColor="White" Font-Names="Tahoma" Font-Size="10pt" BorderColor="#4D8EB7"
BorderStyle="Solid" BorderWidth="8px" />
<Columns>
</Columns>
</asp:GridView>
vb code:
Protected Sub gvReport_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.RowIndex = 1 Then
For i As Integer = 1 To e.Row.Cells.Count - 1
Dim tb8 As New TextBox
tb8.Width = 30
tb8.Font.Size = 12
tb8.Font.Name = "Tahoma"
tb8.Text = e.Row.Cells(i).Text
e.Row.Cells(i).Controls.Add(tb8)
Next
End If
If e.Row.RowIndex = 2 Then
For i As Integer = 1 To e.Row.Cells.Count - 1
If e.Row.Cells(i).Text <> "N/A" Then
Dim btn1 As New Button
btn1.Width = 50
btn1.Font.Size = 12
btn1.BorderStyle = BorderStyle.None
btn1.Font.Underline = True
btn1.BackColor = Color.FromArgb(230, 241, 255)
btn1.Font.Name = "Tahoma"
btn1.Text = e.Row.Cells(i).Text & "%"
e.Row.Cells(i).Controls.Add(btn1)
End If
Next
End If
End If
End Sub
Public Sub gvReport_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles gvReport.RowCommand
Dim currentCommand As String = e.CommandName
Dim currentRowIndex As Integer = Int32.Parse(e.CommandArgument)
Dim ProductID As String = gvReport.DataKeys(currentRowIndex).Value
gvReport = CType(sender, GridView)
MsgBox("Command: " & currentCommand & "<br><br> Row Index: " & currentRowIndex.ToString & "Product ID: " & ProductID)
End Sub
[/code]
<br/>
<br/>
View the full article