asp.net datagrid findcontrol为文本框返回null [英] asp.net datagrid findcontrol returns null for textbox

查看:159
本文介绍了asp.net datagrid findcontrol为文本框返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我的aspx代码

  ..... 
< asp:TemplateColumn>
< HeaderTemplate>
< asp:DropDownList
ID =HeaderDropDownRunat =server
AutoPostBack =True
OnSelectedIndexChanged =DropDown_SelectedIndexChanged/>
< / HeaderTemplate>
< ItemTemplate>
< asp:DropDownList
ID =ItemDropDownRunat =server/>
< / ItemTemplate>
< / asp:TemplateColumn>
< asp:TemplateColumn>
< HeaderTemplate>
详细信息
< / HeaderTemplate>

< ItemTemplate>

< asp:TextBox ID =txtDetailsrunat =serverTextMode =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)作为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


in ASP.Net datagrid I placed one dropdown and a textbox (multiline). I needs to read those values in a loop. I am getting the reference object of dropdown using findControl method but when I am trying for textbox its returns null.

Here is my aspx code

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

The C# code is

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

I am getting the lst object correctly, but txt always returns null.

解决方案

You have a typo:

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

Should be:

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

Therefore, txtDetaills should be txtDetails

这篇关于asp.net datagrid findcontrol为文本框返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆