1
當前我正在使用哪種標準方式在GridView控件中執行編輯。但是這允許我一次只執行一個操作。在我的應用程序中,當一個字段的值變化時,當用戶點擊「更新」時。應該更新行並且還應該觸發一個刪除查詢。在編輯操作同時發生時,在ASP.NET中的GridView控件中執行刪除操作
我嘗試使用手動編輯
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="sdsample1" Visible="False" OnRowEditing="GridView1_OnRowEditing" OnRowUpdated="GridView1_OnRowUpdated">
- 想獲得在backnd當前編輯行的GridView中CheckBoxField字段值和8列
<asp:CheckBoxField DataField="Goal_Type" HeaderText="Goal_Type"
SortExpression="Goal_Type" />
protected void GridView1_OnRowEditing(object sender, GridViewEditEventArgs e)
{
((CheckBox)GridView1.Rows[GridView1.SelectedIndex].FindControl("Goal_Type"));
CheckBox chk2 = ((CheckBox)GridView1.Rows[GridView1.EditIndex].Cells[7].Controls[0]);
goal_type =Convert.ToString(chk2.Checked);
if(goal_type.Equals("False"))
Goal_flag.Value ="0";
else
Goal_flag.Value = "1";
}
然後,一旦我得到checkbox字段的值設置了一些標誌變量,我在下面的函數中執行刪除操作
protected void GridView1_OnRowUpdated(object sender, GridViewUpdatedEventArgs e)
{
CheckBox chk2 = ((CheckBox)GridView1.Rows[e.AffectedRows].Cells[7].Controls[0]);
Boolean goal_type = chk2.Checked;
if (goal_type == true && Goal_flag.Value.Equals("0"))
{
string connectionString =
WebConfigurationManager.ConnectionStrings["DataCollectionConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd;
int ID = Convert.ToInt32(((TextBox)GridView1.Rows[e.AffectedRows].FindControl("ID")).Text);
cmd =new SqlCommand("Delete from XXX WHERE (ID = " + ID + ") ", con);
cmd.CommandType =
CommandType.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
我在「GridView1_OnRowEditing」函數本身出現此錯誤 索引超出範圍。必須是非負數且小於集合的大小。 參數名稱:索引
請讓我知道如果我的方法在GridView中同時操作是正確的。