如何在 ASP.NET 的网格视图中获取 EditTemplate 控件的控件类型?
要获得绑定控件的类型,我只需执行此操作
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>