2011-09-23 73 views
0

我想在GridView_RowCommand中查找DropDown元素,但它表示GridViewCommandEventArgs不包含「行」的定義。我需要在這個事件中這樣做,因爲我正在評估一個GridView命令。見沒有下面的代碼如何訪問RowCommand事件中的行

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

事情是這樣的:

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

這是假設什麼的射擊關閉RowCommand是一個LinkBut​​ton。根據改變。

+0

對不起,我有點新本..它在哪兒找到這裏滴下來?我的下拉是在ItemTemplate – edcod81

+0

哦,我看到像這樣的GridView行=(GridView)(((DropDownList)e.CommandSource).NamingContainer); Myddl = row.FindControl(「ddlClients」)爲DropDownList; – edcod81

+0

@ edcod81 - 不!它不會投!您必須使用LinkBut​​ton或Button或ImageButton。您使用了哪種按鈕類型? – adatapost

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"; 
     } 
    } 
+0

非常感謝...我希望我可以給你兩個答案...我試圖投票但沒有足夠的特權 – edcod81

+0

@ edcod81好友!它不是關於投票。我很高興你能工作。 – adatapost

+0

是的謝謝:) – edcod81

相關問題