Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# FindControl在GridView中返回NULL_C#_Asp.net_Gridview_Code Behind_Findcontrol - Fatal编程技术网

C# FindControl在GridView中返回NULL

C# FindControl在GridView中返回NULL,c#,asp.net,gridview,code-behind,findcontrol,C#,Asp.net,Gridview,Code Behind,Findcontrol,我试图从ASP.NET中的代码隐藏操作DropDownList,它位于GridView字段中。为此,我使用FindControl方法将引用添加到本地DDL。但是,它似乎不起作用,我尝试了多种方法(Load,Init事件),但我总是得到NullReferenceException 这是我的密码: <asp:TemplateField HeaderText="Zuständige Führungskraft"> <ItemTemplate>

我试图从ASP.NET中的代码隐藏操作
DropDownList
,它位于
GridView
字段中。为此,我使用
FindControl
方法将引用添加到本地DDL。但是,它似乎不起作用,我尝试了多种方法(
Load
Init
事件),但我总是得到
NullReferenceException

这是我的密码:

 <asp:TemplateField HeaderText="Zuständige Führungskraft">
           <ItemTemplate>
           <!-- <%# Eval("ZustaendigeFuehrungskraft")%> -->
                <asp:DropDownList AppendDataBoundItems="true" Enabled="false" runat="server" ID="ddwnFK" >

                </asp:DropDownList>
           </ItemTemplate>
           <EditItemTemplate>
                <asp:DropDownList AppendDataBoundItems="true" runat="server" ID="ddwnFK" >

                </asp:DropDownList>
           </EditItemTemplate>
</asp:TemplateField> 

你能试着像我一样访问它吗

DropDownList ddwnFK = (DropDownList)row.Cells[0].Controls[0];

或者在“快速观察”中查看该行的内容。

似乎您正在尝试在网格的每一行中查找下拉列表(即使页眉/页脚行也不包含下拉列表)

您可以使用
GridViewEditEventArgs.NewEditIndex
属性访问正在编辑的行:

var row = grdBenutzer.Rows[e.NewEditIndex];
var ddwnFK = (DropDownList)row.FindControl("ddwnFK");

MSDN中关于FindControl的注释:仅当控件直接包含在指定容器中时,此方法才会找到该控件;也就是说,该方法不会在控件中的整个控件层次结构中进行搜索。尝试浏览第行下的控件树。可能是,drop down不是一个直接的孩子。谢谢……这个盒子里的容器是什么?gridview还是单电池?我认为最合理的说法是单电池是一个容器。谢谢,但这是我已经尝试过的,它也给了我一个NullReferenceException@LeonidasFett:然后我会尝试类似于
row.Cells[cellIndex].FindControl(…)
var row = grdBenutzer.Rows[e.NewEditIndex];
var ddwnFK = (DropDownList)row.FindControl("ddwnFK");