C# 访问位于用户控件内的 DropDownList 项值

标签 c# asp.net user-controls

我有一个名为 dropdownlist.ascx 的通用控件,它根据 XML 文档和我传递给该控件的属性填充它的 ListItems。

此控件在同一 aspx 页面上多次使用。我在 aspx 页面的 Page_Load 事件中将此控件转换为 DropDownList 控件没有问题,但是当我想在 aspx 页面的 Page_Load 事件上设置此控件的 SelectedValues 时,它不能作为 Items.Count 值为 0。

我假设这里存在一些页面生命周期问题。

控制page.aspx

<triangle:DDLResponse ID="ddlHeight" runat="server" CssClass="dropdownlist ddlregister" responseId="height" mode="dropdownlist" />

page.aspx 上的代码隐藏

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        UserProfile profile = controls_session.profile;

        DropDownList _ddlHeight = (DropDownList)ddlHeight.FindControl("dropdownlist");

        _ddlHeight.SelectedValue = profile.Height;          

    }
}

ddlHeight 的列表项渲染没有问题。

有人对此有任何想法或解决方案吗?

谢谢,

最佳答案

尝试在 Page_PreRender 事件中设置 SelectedValue。它会起作用。

关于C# 访问位于用户控件内的 DropDownList 项值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7047994/

相关文章:

.net - 如何从标准的非托管非 .NET 应用程序调用 C++/CLI (.NET) DLL?

c# - C# 中 Winforms 应用程序的广告集成

c# - 如何在 C# 中打印全尺寸图像

c# - EF Core 8 无法创建 'DbContext' 类型的 ''

c# - 在 ASP.NET 中创建 default.aspx.designer.cs 文件

php - PHP 中的渲染体

linux - linux中的作业调度

c# - 如何在 C# 中使 UserControls BackColor 透明?

c# - 从值中获取键 - Dictionary<string, List<string>>

c# - 我的 Dispose 方法会在 ASP.NET 请求结束时被调用吗