2012-10-22 165 views
0

有#2和其他网站类似的问题,但我似乎错过的东西。GridView'触发事件RowUpdating未处理。 C#代码背后asp.net

我有一个绑定到来自数据库的DataTable一个GridView。我的目标是用同一行中调用以下方法的按钮同时更新一行。

 protected void TestsToBeDone_RowUpdating(object sender, GridViewEditEventArgs e) 
    { 
     SqlConnection myConnection = getConnection(); 
     myConnection.Open(); 

     int index = Convert.ToInt32(e.NewEditIndex); 

     GridViewRow selectedRow = TestsToBeDone.Rows[index]; 
     TableCell LABAVIDc = selectedRow.Cells[1]; 
     int LABAVID = Convert.ToInt32(LABAVIDc.Text); 

     TableCell assistentc = selectedRow.Cells[5]; 

     string query = "UPDATE Labaanvraag " + 
         "SET Status='4' "+ 
           "WHERE LABAVID ='"+LABAVID+"'"; 
     } 

     SqlCommand commandZ = new SqlCommand(query, myConnection); 
     commandZ.ExecuteNonQuery();    

     myConnection.Close(); 

     SetPatientTestGrid(Session["PATID"], e); 
    } 

它被从这里叫:

<asp:GridView ID="TestsToBeDone" runat="server" EnableModelValidation="True" OnRowEditing="TestsToBeDone_RowUpdating"> 
    <Columns> 
     <asp:ButtonField ButtonType="Button" CommandName="Update" 
      HeaderText="Afgenomen" ShowHeader="True" Text="Afgenomen" /> 
    </Columns> 
</asp:GridView> 

在我第一次尝试,我与在后面的代码GridViewCommandEventArgs一个OnRowCommand。

但它仍然抛出同样的异常,虽然我的一对夫妇,它更改为OnRowEditing和GridViewEditEventArgs会解决这个问题的网站阅读。

由于提前,

回答

4

GridView的ASP.NET中的一些已建成的命令 - DeletedUpdate等发生了什么事是你的按钮有Update命令名称,以及Gridview正在劫持你并发射RowUpdating事件,而不是你的RowEditing事件。将您的按钮命令名称更改为Edit,并使用RowEditing事件。

+0

我已经试过了。但是现在更新方法不再被按钮调用。但它也没有抛出异常。 –

+0

啊,明白了。你是对的地方,但我改变了'编辑',现在它的工作:)谢谢指出我在正确的方向!如果你改变你的答案,我将设置就是答案:) –

+0

很抱歉的响应晚了,但我的意思的CommandName =“编辑” :) –

0

如果您使用的是Row_Command事件进行编辑,然后不使用按钮命令名称“更新”的更新和“删除”的删除。相反,分别使用Edit或UP和Del。

相关问题