从代码隐藏中查找 FormView 中的控件时出现问题
这里是后面的代码...我正在尝试检索此控件,以便我可以将项目添加到下拉列表中(我正在检索角色组以添加到代码隐藏中的下拉列表中)
Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim DDRoleGroups As DropDownList
DDRoleGroups = FormView1.FindControl("DDRoleGroup")
End Sub
这是 FormView: (我删除了大部分字段,以便更容易阅读)
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID"
DataSourceID="ObjectDataSource_Vendors"
DefaultMode="Insert" BorderColor="DarkGray"
BorderStyle="Solid" BorderWidth="1px" CellPadding="4" Visible="False">
<EditItemTemplate>
</EditItemTemplate>
<InsertItemTemplate>
<label class="form_label">Role Group:</label><br /><asp:DropDownList ID="DDRoleGroup"
runat="server" Width="175px"
EnableViewState="False">
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>
这可能与它位于 Page_Load 子项中并且控件尚未实际加载有关吗?
谢谢,
马特
Here the code behind... I'm trying to retrieve this control so I can add items to the drop down list (I'm retrieving the Role Groups to add to the drop down list in the code-behind)
Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim DDRoleGroups As DropDownList
DDRoleGroups = FormView1.FindControl("DDRoleGroup")
End Sub
Here's the FormView: (I took out most of the fields so it's easier to read)
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID"
DataSourceID="ObjectDataSource_Vendors"
DefaultMode="Insert" BorderColor="DarkGray"
BorderStyle="Solid" BorderWidth="1px" CellPadding="4" Visible="False">
<EditItemTemplate>
</EditItemTemplate>
<InsertItemTemplate>
<label class="form_label">Role Group:</label><br /><asp:DropDownList ID="DDRoleGroup"
runat="server" Width="175px"
EnableViewState="False">
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>
Could it possibly have to do with the fact that it's in the Page_Load sub and the control hasn't acctually loaded yet?
Thanks,
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的下拉菜单仅存在于插入模式下。 尝试实现 formview 的 ModeChanged 事件并检索控件,如果 CurrentMode == Insert:
您无法在 Page_Load 中处理此问题,因为表单尚未切换到插入模式。
Your dropdown only exists in Insert mode. Try to implement the formview's ModeChanged event and retrieve the control if CurrentMode == Insert:
You cannot handle this in Page_Load, as the form has not yet switched into Insert mode.
FormView 上的 FindControl 仅适用于 FormView 的“CurrentMode”属性设置为的模板。
在您的情况下,如果您的 FormView 设置为“插入”,则只能对“DDRoleGroups”执行 FindControl,因为这是您的控件所在的模板。
希望有所帮助。
FindControl on a formview will only work for the template that the FormView's "CurrentMode" property is set to.
In your case, you can only do FindControl for "DDRoleGroups" if your FormView is set to "Insert", since that's the template that your control exists in.
Hope that helps.