为什么我的 rowcommand 事件处理程序在这里不起作用?
Why does my rowcommand event handler not work here?
我正在尝试处理按下按钮字段中的按钮并随后从网站和数据库中删除该行的事件。我这样做了,但是在我的 .aspx.cs 文件中使用了以下代码:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
System.Diagnostics.Debug.WriteLine("testing buttons");
}
}
但是,每当我点击按钮字段中的按钮时,它都会抛出错误,指出事件未被处理。以下代码是整个数据table.
的aspx
<asp:GridView ID="gridViewStudent" runat="server" CellPadding="4" ForeColor="#333333"
emptydatatext="There are no data to display">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Delete" HeaderText="Edit" ShowHeader="True" Text="Remove" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
据我所知,您没有向 GridView
声明所有必需的事件处理程序。为相应的操作使用适当的事件处理程序和事件处理程序方法:
页面标记 (ASPX)
<asp:GridView ID="gridViewStudent" runat="server" ...
OnRowCommand="gridViewStudent_RowCommand"
OnRowDeleting="gridViewStudent_RowDeleting">
</asp:GridView>
后面的代码(ASPX.CS)
// Row command event
protected void gridViewStudent_RowCommand(object sender, GridViewCommandEventArgs e)
{
// do something
}
// Row deleting event
protected void gridViewStudent_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// do something
}
我正在尝试处理按下按钮字段中的按钮并随后从网站和数据库中删除该行的事件。我这样做了,但是在我的 .aspx.cs 文件中使用了以下代码:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
System.Diagnostics.Debug.WriteLine("testing buttons");
}
}
但是,每当我点击按钮字段中的按钮时,它都会抛出错误,指出事件未被处理。以下代码是整个数据table.
的aspx<asp:GridView ID="gridViewStudent" runat="server" CellPadding="4" ForeColor="#333333"
emptydatatext="There are no data to display">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Delete" HeaderText="Edit" ShowHeader="True" Text="Remove" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
据我所知,您没有向 GridView
声明所有必需的事件处理程序。为相应的操作使用适当的事件处理程序和事件处理程序方法:
页面标记 (ASPX)
<asp:GridView ID="gridViewStudent" runat="server" ...
OnRowCommand="gridViewStudent_RowCommand"
OnRowDeleting="gridViewStudent_RowDeleting">
</asp:GridView>
后面的代码(ASPX.CS)
// Row command event
protected void gridViewStudent_RowCommand(object sender, GridViewCommandEventArgs e)
{
// do something
}
// Row deleting event
protected void gridViewStudent_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// do something
}