2013-04-29 54 views
0

如何在ASP.NET的gridview中獲得EditTemplate控件的控件類型?如何獲取Gridview模板中的控件類型?

要獲得一個綁定的控件的類型,我只是做這個

foreach (TableCell cell in grdViewDetails.Rows[e.RowIndex].Cells) 
{    
    //set the employeeid so you can update the dataset 
    if (cell.Controls[0] is CheckBox) 
    { 
     CheckBox check = (CheckBox)cell.Controls[0]; 
     //Do stuff with the control and the text inside the control etc; 
    } 
} 

,但我不能似乎找到模板中的控件。他們只是跳過這個。

我試過無濟於事。

foreach (TableCell cell in grdViewDetails.Rows[e.RowIndex].Cells) 
{ 
    var test1 = cell.Controls[0]; 
    columnName = dsOriginal.Tables[0].Columns[startOfColumns].ColumnName; //[System.Web.UI.LiteralControl] I can find the Column Name but it't not a normal control... It's a LiteralControl? 
    var test2 = cell.FindControl("CheckWeek2"); //[System.Web.UI.WebControls.Calendar] = {SelectedDate = The name 'SelectedData' does not exist in the current context} 
} 

我的GridView控件模板

<asp:TemplateField HeaderText="week2" SortExpression="week2"> 
    <EditItemTemplate> 
     <asp:CheckBox ID="CheckWeek2" runat="server" Checked='<%# Bind("week2") %>'></asp:CheckBox> 
    </EditItemTemplate> 
    <ItemTemplate> 
     <asp:CheckBox ID="Label2" runat="server" Enabled="false" Checked='<%# Bind("week2") %>'></asp:CheckBox> 
    </ItemTemplate> 
</asp:TemplateField> 
+0

哪個事件你試圖找到編輯模板控件? – Damith 2013-04-29 07:50:14

+0

我想獲取正在編輯的控件的文本/缺陷。 – Ruan 2013-04-29 07:52:16

回答

1

嘗試下面,

if (this.grdViewDetails.EditIndex != -1) 
{ 
CheckBox b = grdViewDetails.Rows[grdViewDetails.EditIndex].FindControl("CheckWeek2") as CheckBox; 
if (b != null) 
    { 
    //do something 
    } 
} 
+0

謝謝,這確實有效。 這沒有「CheckBox c = grdViewDetails.Rows [grdViewDetails.EditIndex] .Controls [8]作爲CheckBox;」 (給null) 但你的方式,謝謝 – Ruan 2013-04-29 08:31:36

1

試試這個:

 if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      CheckBox CheckWeek2 = (CheckBox)e.Row.FindControl("CheckWeek2"); 

     }