gpt4 book ai didi

asp.net - 从代码隐藏中查找 FormView 中的控件时出现问题

转载 作者:行者123 更新时间:2023-12-02 12:14:01 25 4
gpt4 key购买 nike

这里是后面的代码...我正在尝试检索此控件,以便可以将项目添加到下拉列表中(我正在检索角色组以添加到代码隐藏中的下拉列表中)

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 子组件中并且控件尚未实际加载有关吗?

谢谢,
马特

最佳答案

您的下拉列表仅存在于插入模式下。尝试实现 formview 的 ModeChanged 事件并检索控件(如果 CurrentMode == Insert):

protected void FormView1_ModeChanged(object sender, EventArgs e)
{
if (FormView1.CurrentMode == FormViewMode.Insert)
{
DropDownList DDRoleGroups = FormView1.FindControl("DDRoleGroup");
// fill dropdown
}
}

您无法在 Page_Load 中处理此问题,因为表单尚未切换到插入模式。

关于asp.net - 从代码隐藏中查找 FormView 中的控件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1039172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com