简体   繁体   English

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

[英]C# Accessing DropDownList item values that sit within a user control

I have aa generic control called dropdownlist.ascx that populates it's ListItems based on a XML document and a property that I pass to this control.我有一个名为 dropdownlist.ascx 的通用控件,它根据 XML 文档和我传递给该控件的属性填充它的 ListItems。

This control is used multiple times on the same aspx page.此控件在同一个 aspx 页面上多次使用。 I have no problem casting this control as a DropDownList control in the Page_Load event of the aspx page, however when I want to set the SelectedValues of this control on the Page_Load event of the aspx page it doesn't work as the Items.Count value is 0.我在 aspx 页面的 Page_Load 事件中将此控件转换为 DropDownList 控件没有问题,但是当我想在 aspx 页面的 Page_Load 事件上设置此控件的 SelectedValues 时,它不能作为 Items.Count 值为 0。

I assume there are some Page Lifecyle issues going on here.我认为这里发生了一些页面生命周期问题。

Control on page.aspx page.aspx 上的控件

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

Codebehind on page.aspx 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;          

    }
}

The List Items of ddlHeight render without issue. ddlHeight 的列表项呈现没有问题。

Anyone have any idea or solution to this?有人对此有任何想法或解决方案吗?

Thanks,谢谢,

Try to set the SelectedValue in Page_PreRender event.尝试在Page_PreRender事件中设置SelectedValue It will work.它会起作用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM