如何在RowCommand事件中访问行?

本文关键字:访问 事件 RowCommand | 更新日期: 2023-09-27 18:09:21

我试图在GridView_RowCommand中找到一个下拉元素,但它说GridViewCommandEventArgs不包含'行'的定义。我需要在这个事件中这样做,因为我正在评估GridView命令。参见下面的失败代码

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
 if (e.CommandName == "Add")
  {
    DropDownList Myddl = null;
    ClientClass client = new ClientClass();
    Myddl = e.Row.FindControl("ddlClients") as DropDownList;
    if (Myddl != null)
       {
         updated = client.InsertUpdateClient(ClientID, 
           int.Parse(e.CommandArgument.ToString()), departmentID);
        }
    else
      { 
        Labels.Text = "There was an error updating this client";
       }
    }
}

如何在RowCommand事件中访问行?

像这样:

GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); 

这是假设触发RowCommand的是一个LinkButton。根据。

除了@Stephen,

if (e.CommandName == "Add")
  {
    DropDownList Myddl = null;
    ClientClass client = new ClientClass();
   //Use this if button type is linkbutton
    GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
   //Use this if button type is Button
   //GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
    Myddl = row.FindControl("ddlClients") as DropDownList;
    if (Myddl != null)
       {
         updated = client.InsertUpdateClient(ClientID, 
           int.Parse(e.CommandArgument.ToString()), departmentID);
        }
    else
      { 
        Labels.Text = "There was an error updating this client";
       }
    }