简体   繁体   English

如何在Gridview模板中获取控件类型?

[英]How to Get the Control Type in a Gridview Template?

How would you get the control type of an EditTemplate control in a gridview in ASP.NET? 如何在ASP.NET中的gridview中获取EditTemplate控件的控件类型?

To get a bound control's type I simply do this 要获得绑定控件的类型,我只需执行此操作

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;
    }
}

but I cant seem to find the controls in the templates. 但我似乎无法找到模板中的控件。 They just skip this if. 他们只是跳过这个。

What I've tried to no avail. 我试图无济于事。

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}
}

My Gridview control Template 我的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>

Try this: 尝试这个:

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

        }

try below, 试试下面,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM