Gridview RowCommand 不起作用
我在尝试在网格视图中触发 rowcommand 事件时遇到问题。我按照 MSDNet 的代码示例进行操作,但我无法弄清楚为什么它不起作用。代码如下。谢谢。
<asp:GridView ID="GridViewProducts" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
CellPadding="5" CellSpacing="1" DataKeyNames="Pkey"
DataSourceID="SqlDataSourceProducts" ForeColor="Black" GridLines="Vertical">
<FooterStyle BackColor="#CCCCCC" />
<PagerSettings PageButtonCount="20" />
<Columns>
<asp:BoundField DataField="Product" HeaderText="Product" >
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField HeaderText="Interest">
<ItemTemplate>
<asp:DropDownList ID="ddlProductInterest" runat="server" SelectedValue='<%# Bind("ProductInterest") %>'>
<asp:ListItem></asp:ListItem>
<asp:ListItem>Low</asp:ListItem>
<asp:ListItem>Medium</asp:ListItem>
<asp:ListItem>High</asp:ListItem>
<asp:ListItem>None</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button runat="server" ID="TestButton" Text="Button" CommandName="Test"
CommandArgument="<%# CType(Container, GridViewRow).RowIndex %>" />
</ItemTemplate>
<HeaderStyle HorizontalAlign="center" />
<ItemStyle HorizontalAlign="center" />
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Silver" Font-Bold="True" ForeColor="Black" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>
++Code Behind +++
Sub GridViewProducts_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "Test" Then
Dim index = Convert.ToInt32(e.CommandArgument)
Dim row = GridViewProducts.Rows(index)
Dim MyString As String = row.Cells(0).Text
strSQL = "INSERT INTO tblClosedProducts (" & _
"Product, ClosedBy, DateClosed " & _
") VALUES (" & _
"@Product, @ClosedBy, @DateClosed " & _
")"
Dim MyParameters1 As SqlParameter() = { _
New SqlParameter("@Product", SqlDbType.VarChar), _
New SqlParameter("@ClosedBy", SqlDbType.VarChar), _
New SqlParameter("@DateClosed", SqlDbType.SmallDateTime) _
}
MyParameters1(0).Value = row.Cells(0).Text
MyParameters1(1).Value = GetInfo.GetFullName(UCase(Right(HttpContext.Current.User.Identity.Name.ToString(), 4)))
MyParameters1(2).Value = DateAdd("h", -1, Now())
objData.SQLExecuteNonQuery(strSQL, CommandType.Text, MyParameters1)
End If
End Sub
I am having problems trying to get a rowcommand event to fire in a gridview. I followed the code example from MSDNet but I cannot figure out why it is not working. The code is below. Thank you.
<asp:GridView ID="GridViewProducts" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
CellPadding="5" CellSpacing="1" DataKeyNames="Pkey"
DataSourceID="SqlDataSourceProducts" ForeColor="Black" GridLines="Vertical">
<FooterStyle BackColor="#CCCCCC" />
<PagerSettings PageButtonCount="20" />
<Columns>
<asp:BoundField DataField="Product" HeaderText="Product" >
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField HeaderText="Interest">
<ItemTemplate>
<asp:DropDownList ID="ddlProductInterest" runat="server" SelectedValue='<%# Bind("ProductInterest") %>'>
<asp:ListItem></asp:ListItem>
<asp:ListItem>Low</asp:ListItem>
<asp:ListItem>Medium</asp:ListItem>
<asp:ListItem>High</asp:ListItem>
<asp:ListItem>None</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button runat="server" ID="TestButton" Text="Button" CommandName="Test"
CommandArgument="<%# CType(Container, GridViewRow).RowIndex %>" />
</ItemTemplate>
<HeaderStyle HorizontalAlign="center" />
<ItemStyle HorizontalAlign="center" />
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Silver" Font-Bold="True" ForeColor="Black" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>
++Code Behind +++
Sub GridViewProducts_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "Test" Then
Dim index = Convert.ToInt32(e.CommandArgument)
Dim row = GridViewProducts.Rows(index)
Dim MyString As String = row.Cells(0).Text
strSQL = "INSERT INTO tblClosedProducts (" & _
"Product, ClosedBy, DateClosed " & _
") VALUES (" & _
"@Product, @ClosedBy, @DateClosed " & _
")"
Dim MyParameters1 As SqlParameter() = { _
New SqlParameter("@Product", SqlDbType.VarChar), _
New SqlParameter("@ClosedBy", SqlDbType.VarChar), _
New SqlParameter("@DateClosed", SqlDbType.SmallDateTime) _
}
MyParameters1(0).Value = row.Cells(0).Text
MyParameters1(1).Value = GetInfo.GetFullName(UCase(Right(HttpContext.Current.User.Identity.Name.ToString(), 4)))
MyParameters1(2).Value = DateAdd("h", -1, Now())
objData.SQLExecuteNonQuery(strSQL, CommandType.Text, MyParameters1)
End If
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
@rtpHarry 是正确的,这是连接事件的有效方法。连接事件的另一种方法是更改后面代码中方法的签名,以将
Handles Me.GridViewProducts.RowCommand
添加到末尾:@rtpHarry is correct, and that is a valid way to wire up the event. Another method of wiring the event would be to change the signature of your method in the code behind to add
Handles Me.GridViewProducts.RowCommand
to the end:启用 View State= true 将解决您的问题
Enable View State= true will solve your problem
您的 gridview 没有在其标记中连接事件。
尝试添加
onrowcommand="GridViewProducts_RowCommand"
,如下所示:Your gridview doesnt have the event wired up in its markup.
Try adding in
onrowcommand="GridViewProducts_RowCommand"
so it looks like this: