我有一個GridView它具有這樣的特性:數據庫行不會通過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中沒有任何東西?或者也許別的東西?
如果您在查看錯誤的單元格,它會引發您指向下拉列表的異常 – Divi
根據答案更新了更新代碼,但我仍然得到相同的結果,即沒有錯誤,也沒有數據庫更新。我想我必須首先在我的PC上進行VS2008調試。 – oshirowanen