我有一个具有以下属性的 GridView:
OnRowUpdating="GridViewRowUpdateEventHandler"
在 GridView 中,我有以下控件:
<asp:TemplateField HeaderText="Progress" SortExpression="progress">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem Value="0">Incomplete</asp:ListItem>
<asp:ListItem Value="1">Complete</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
GridViewRowUpdateEventHandler
看起来像这样:
protected void GridViewRowUpdateEventHandler(object sender, GridViewUpdateEventArgs e)
{
SqlConnection connection;
SqlCommand command;
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
DropDownList ddlPriority = (DropDownList)row.FindControl("DropDownList1");
using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"]))
{
using (command = new SqlCommand(@"update table1 set priority = @priority where id = @id", connection))
{
command.Parameters.Add("@priority", SqlDbType.Int, 1).Value = ddlPriority.SelectedValue;
command.Parameters.Add("@id", SqlDbType.Int, 1).Value = row.RowIndex;
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
GridView1.DataBind();
}
我根本没有收到任何错误消息,并且数据库中的相关行没有被更新。有谁知道为什么?
会不会是我看错了单元格Cells[5]
?或者可能是因为我在 page_load 中什么都没有?或者也许是别的什么?