c# - asp.net datagrid findcontrol 为文本框返回 null

标签 c# asp.net datagrid

在 ASP.Net 数据网格中,我放置了一个下拉菜单和一个文本框(多行)。我需要循环读取这些值。我正在使用 findControl 方法获取下拉列表的引用对象,但是当我尝试文本框时,它返回 null。

这是我的aspx代码

.....
 <asp:TemplateColumn>
            <HeaderTemplate>
                <asp:DropDownList 
                    ID="HeaderDropDown" Runat="server" 
                    AutoPostBack="True" 
                     OnSelectedIndexChanged="DropDown_SelectedIndexChanged" />
            </HeaderTemplate>
            <ItemTemplate>
                <asp:DropDownList 
                    ID="ItemDropDown" Runat="server"/>
            </ItemTemplate>
        </asp:TemplateColumn>
          <asp:TemplateColumn >
          <HeaderTemplate>
            Details
            </HeaderTemplate>

        <ItemTemplate>

        <asp:TextBox ID="txtDetails" runat="server"  TextMode="MultiLine"></asp:TextBox>

        </ItemTemplate>       
         </asp:TemplateColumn>
...............

C#代码是

 for (int i = 1; i < DataGrid1.Items.Count; i++)
        {
            DropDownList lst = DataGrid1.Items[i].Cells[1].FindControl("ItemDropDown") as DropDownList;
            String value = lst.SelectedValue;
            String StaffId = DataGrid1.Items[i].Cells[0].Text;
            TextBox txt= DataGrid1.Items[i].Cells[2].FindControl("txtDetaills") as TextBox;
         }

我正在正确获取 lst 对象,但 txt 总是返回 null。

最佳答案

你有一个错字:

TextBox txt= DataGrid1.Items[i].Cells[2].FindControl("txtDetaills") as TextBox;

应该是:

TextBox txt= DataGrid1.Items[i].Cells[2].FindControl("txtDetails") as TextBox;

因此,txtDetaills应该是txtDetails

关于c# - asp.net datagrid findcontrol 为文本框返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7119573/

相关文章:

c# - 删除所有 DataGrid 行和单元格边框

silverlight - 我可以检测到在 Silverlight 中排序的数据网格列吗?

c# - 如何使 COM ActiveX 对象在 IE 64 位中工作?

c# - 如何将基于 swift 的 cocoapods 移植到 Xamarin

c# - 当用户从 DropDownList 中选择不同选项时如何更改内容

c# - ASP .Net Core 在运行时更改路由

c# - 将 IoC 容器引入遗留代码

c# - StackOverflow 与 TryValidateObject 如果对象是有效的

c# - 如何检测当前的URL,然后根据它进行转发

c# - 为什么单元格标题和单元格内容在数据网格中不对齐?