C# Accessing DropDownList item values that sit within a user control
I have a a generic control called dropdownlist.ascx that populates it's ListItems based on a XML document and a property that I pass to this control.
This control is used multiple times on the same aspx page. I have no 开发者_如何学JAVAproblem 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.
I assume there are some Page Lifecyle issues going on here.
Control on page.aspx
<triangle:DDLResponse ID="ddlHeight" runat="server" CssClass="dropdownlist ddlregister" responseId="height" mode="dropdownlist" />
Codebehind on 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.
Anyone have any idea or solution to this?
Thanks,
Try to set the SelectedValue
in Page_PreRender
event. It will work.
精彩评论