2012-01-12 74 views
1

在web應用程序中,我試圖在RowDataBound事件中查找網格控件。但它提供對象的實例的對象參考,這是我的代碼:在gridview中查找控件?在Asp.net中?

 protected void mygrid_RowDataBound(object sender, GridViewRowEventArgs e) 
      { 

       string empid = "";   

       empid = ((Label)e.Row .FindControl("lblname")).Text; 
      } 

您可以傳喚我找到控件,謝謝。

+0

試試這個'標籤lblprop_img_id =(標籤)e.Row.Cells [2 ] .FindControl(「lblprop_img_id」);' – Murtaza 2012-01-12 09:36:41

回答

1

雅,我得到了答案,我必須把

​​

然後我我們得到了控制

1

的數據行只喜歡尋找控制:

protected void mygrid_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 

    if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      string empid = "";   

      empid = ((Label)e.Row .FindControl("lblname")).Text; 
     } 
} 
+0

ya謝謝你,pankaj,我明白了,我發佈了我的答案,謝謝你的回覆 – 2012-01-12 09:39:48

1

「對象引用對象的實例」,因爲沒有名爲lblname控制被發現的當前行錯誤可能是。

也許你需要檢查行的類型,e.Row.RowType = DataControlRowType.DataRow,這樣你就不會在標題行中搜索控件。

1
Label lbl = (Label)e.Row.Cells[2].FindControl("lblCreatedBy"); 
      lbl.Text = "ABC";