none
Load user control dynamically RRS feed

  • Question

  • I am using the following code to dynamically load a user control from a HttpHandler.

    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    Page newPage = new Page();
    newPage
    .EnableEventValidation = false;
    newPage
    .LoadControl("~/App_Controls/Site/Player/PlayerProfile2.ascx").RenderControl(htw);
    var content = sb.ToString();

    The PlayerProfile user control is successfully being loaded but it does not appear to be executing code that sets the values of fields in the user control i.e.

    <div class="content">

    <div>
    <p>Use the fields below to change your personal details.</p>
    <label for="email">Name:</label>
    <asp:TextBox CssClass="long" ID="txtProfileName" runat="server" />
    <label for="age">Age:</label>
    <asp:TextBox ID="txtAge" runat="server" class="tiny" />
    <label for="phone">Phone number:</label>
    <asp:TextBox ID="txtPhone" runat="server" />
    <label for="gender">Gender:</label>
    <br />
    <asp:DropDownList ID="ddlGender" />
    </div>

    txtProfileName
    .Text = "Test";
    txtProfileEmail
    .Text = player.Email;
    var genders = new Dictionary<string, string>();
    genders
    .Add("0", "Male");
    genders
    .Add("1", "Female");
    ddlGender
    .DataSource = genders;
    ddlGender
    .DataBind();

    I have tried run in debug mode with breakpoints and it's hitting PageLoad for the user control that I'm loading dynamically.

    Thanks,

    Sean

    • Moved by Mike Dos Zhang Monday, March 19, 2012 9:18 AM asp.net topic (From:Visual C# Language)
    Thursday, March 15, 2012 12:13 AM

All replies