2011-01-31 57 views
1

我想這樣的事情,但沒有奏效:如何在GridView RowCommand事件中查找控件?

GridViewRow row = (GridViewRow)(((Repeater)e.CommandSource).NamingContainer); 
Repeater _rpt1 = row.Cells[8].FindControl("rptReg") as Repeater; 

錯誤:

Unable to cast object of type 'System.Web.UI.WebControls.Button' to type 'System.Web.UI.WebControls.Repeater'. 

是有辦法,我可以使用的GridView中OnRowCommand事件下面的代碼?

其實我試圖從GridView控件刪除該行並在其上的用戶點擊,我抓住了多張標識和傳遞給SP和更新表和databind the gridview

GridViewRow row = gv.SelectedRow;   
    Repeater _rpt = gv.Rows[e.RowIndex].Cells[8].FindControl("rptReg") as Repeater; 
    Repeater _rpt1 = gv.Rows[e.RowIndex].Cells[9].FindControl("rptVisitor") as Repeater; 

    foreach (RepeaterItem item in _rpt.Items) 
    { 
     TextBox _txt = item.FindControl("txtId") as TextBox; 
     TextBox _txt1 = item.FindControl("txtName") as TextBox; 
     //update db 
    } 
+0

我不知道我按照但在OnRowCommand你可以檢查e.CommandName ==「刪除」 – Malk 2011-01-31 22:32:00

+0

多數民衆贊成在正確的,但我有一種情況,我正在尋找一種方式來做RowCommand的FindControl? – 2011-01-31 22:33:17

回答

0

你的錯誤告訴我名爲「rptReg」的控件是一個按鈕。

GridView控件有一個屬性叫做DataKeys可容納多個ID 所以,你會拉出「PERSONID」,如:

 string personId = GridView1.DataKeys[e.RowIndex]["PersonId"].ToString(); 
3

請試試這個:

var viewRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer); 
foreach (GridViewRow gvr in gvDetails.Rows) 
{ 
    var btnSelect = (ImageButton)viewRow.FindControl("btnSelect"); 
    if (gvr == viewRow) 
    { 
     if (btnSelect.ImageUrl.ToLower() == "~/images/rbnormal.jpg") 
     { 
      btnSelect.ImageUrl = "~/Images/rbSelected.jpg"; 
      btnSelect.Enabled = false; 
     } 
    } 
    else 
    { 
     btnSelect.ImageUrl = "~/Images/rbnormal.jpg"; 
     btnSelect.Enabled = true; 
    } 
} 
相關問題