我正在编写我目前正在处理的项目,并且需要
使用gridview。
我正在使用OnRowCommand事件,删除工作正常,条目也从数据库中删除,但由于某种原因,这个错误被抛出:
“GridView'gvCourses'触发了未处理的事件RowDeleting。”
注意: - 我没有在aspx文件中使用'OnRowDeleting'。
这是奇怪的事情我作为一个初学者不明白。当我生成'OnRowDeleting'事件并且在后面的代码中将事件留空时(事件处理程序内的0行代码),错误不会再出现。
希望了解并理解为什么会这样。任何帮助将不胜感激。
.aspx代码:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Panel ID="Panel2" runat="server">
<table style="width:100%;">
<tr>
<td style="width:210px">
</td>
<td>
</td>
</tr>
<tr>
<td style="width:210px"> </td>
<td> </td>
</tr>
<tr>
<td style="width:210px; height: 331px;"></td>
<td style="height: 331px">
<asp:GridView ID="gvCourses" runat="server"
AutoGenerateColumns="False"
CssClass="table-hover table"
GridLines="None" Width="800px"
ShowFooter="True"
OnRowCommand="gvCourses_RowCommand" >
<Columns>
<asp:BoundField DataField="course"
HeaderText="Courses in Valsura"
SortExpression="DateField" />
<asp:TemplateField>
<FooterTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Students</asp:ListItem>
<asp:ListItem>Teachers</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server"
OnClick="Button1_Click"
Text="Add Course"
CssClass="btn-danger" />
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2"
runat="server">View Batches</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton3"
runat="server"
CommandName="delete"
CommandArgument='<%#Eval ("course") %>'>Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:RequiredFieldValidator ID="rfvCourse"
ControlToValidate="DropDownList1"
InitialValue="Select"
ErrorMessage="Select*"
ForeColor="Red" >
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width: 180px; height: 22px;"></td>
<td style="height: 22px"></td>
<td style="height: 22px"></td>
</tr>
<tr>
<td style="width: 180px"> </td>
<td> </td>
<td> </td>
</tr>
</table>
</asp:Panel>
</asp:Content>
.aspx.cs代码:(代码隐藏,仅包括要求)
protected void gvCourses_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "delete")
{
SqlConnection con =
new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
DataTable dt = new DataTable();
string query =
"delete from tblCourses where course='"+e.CommandArgument.ToString()+"'";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
int n = cmd.ExecuteNonQuery();
con.Close();
gvCourses.DataSource = dt;
gvCourses.DataBind();
ViewState["query"] = "select course from tblCourses";
bindgrid();
}
protected void bindgrid()
{
SqlConnection con =
new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
DataTable dt = new DataTable();
using (SqlDataAdapter sda = new SqlDataAdapter(ViewState["query"].ToString(), con))
{
sda.Fill(dt);
}
gvCourses.DataSource = dt;
gvCourses.DataBind();
}
页面看起来像这样(批次尚未编码)。