如何访问RowCommand事件中的行

时间:2011-09-23 03:40:30

标签: c# asp.net

我试图在GridView_RowCommand中找到一个DropDown元素,但它说GridViewCommandEventArgs不包含'Row'的定义。我需要在此事件中执行此操作,因为我正在评估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";
       }
    }
}

2 个答案:

答案 0 :(得分:2)

这样的事情:

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

这假设发布的RowCommand是一个LinkBut​​ton。根据改变。

答案 1 :(得分:1)

除了@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";
       }
    }