DropDownList insideTemplateField is not accessible from code-behind

≯℡__Kan透↙ 提交于 2019-12-12 02:23:19

问题


i am updating my table by placing datailsview in the webpage and giving it a datasource,

i have introduced a dropdownlist in the middle of bounded control and want it to be filled according to my given instruction, but the DropDownList is not accessible via ID in code-behind, e.g.:

<asp:TemplateField>
    <ItemTemplate>
        <asp:DropDownList ID="drpdownlist" DataSourceID="deptsource" Width="150px" DataTextField="deptname" DataValueField="deptid" runat="server" />
    </ItemTemplate>
</asp:TemplateField>

回答1:


First you need to use the FindControl() method after rows are databound as abatishchev mentioned. Also, you need to remember that this wont be possible until there are in fact some rows in the grid. But you need to also remember that you will not get just one DropDownList, but instead there will be one DropDownList for each row in your GridView.




回答2:


See

  • Finding user control in TemplateField of DetailsView
  • How to find control in TemplateField of GridView?
  • FormView.FindControl() returns null until DataBind()

and many similar.

Call FindControl() after data-binding occurs.



来源:https://stackoverflow.com/questions/8655048/dropdownlist-insidetemplatefield-is-not-accessible-from-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!