ASP.NET Retrieving values from dynamically loaded user control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joel Reinford

    #1

    ASP.NET Retrieving values from dynamically loaded user control

    I am working on an ASP.NET web app. One of the pages will
    be using several user controls. The controls will be
    loaded dynamically based on SQL Server data.

    I need to be able to retrieve the values from the
    individual fields in those user controls. So far I am not
    able to access the controls at postback. Any suggestions
    would be appreciated.

    Joel Reinford
  • Yan-Hong Huang[MSFT]

    #2
    RE: ASP.NET Retrieving values from dynamically loaded user control

    Hello Joel,

    Thanks for posting in the group.

    Since the control(s) are added before the page is rendered (dynamically on
    the server), they don't exist until you re-add them at some point before
    the page is rendered. Page and its controls are created/rendered/destroyed
    on every request and there is nothing in the framework that automatically
    remeber which controls were previously loaded/created. So we need to create
    it again in postback. It's just the way .NET works with dynamically added
    controls.

    Here are some sample code for your reference: (Please add control in
    Page_Init method)

    WebControl:
    using System;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Componen tModel;

    namespace DynPanel
    {
    /// <summary>
    /// Summary description for WebCustomContro l1.
    /// </summary>
    [ToolboxData("<{ 0}:WebCustomCon trol1
    runat=server></{0}:WebCustomCo ntrol1>")]
    public class WebCustomContro l1 : System.Web.UI.W ebControls.WebC ontrol,
    INamingContaine r
    {
    private RadioButtonList rblTemp = new RadioButtonList ();
    private Label lblTemp = new Label();

    protected override void CreateChildCont rols()
    {
    Controls.Add(ne w LiteralControl( "<P><B>"));

    lblTemp.ID = "lblTest";
    lblTemp.Text = "Label Test";
    Controls.Add(lb lTemp);

    Controls.Add(ne w LiteralControl( "</B>"));

    rblTemp.ID = "rblTest";
    rblTemp.Items.A dd("A");
    rblTemp.Items.A dd("B");
    rblTemp.Items.A dd("C");
    rblTemp.Items.A dd("D");
    Controls.Add(rb lTemp);

    Controls.Add(ne w LiteralControl( "<BR><HR>") );
    }
    }
    }

    Button1_Click Event:
    private void Button1_Click(o bject sender, System.EventArg s e)
    {
    WebCustomContro l1 wcc1Temp = ((WebCustomCont rol1)Panel1.Con trols[0]);
    RadioButtonList rblTemp =
    ((RadioButtonLi st)wcc1Temp.Fin dControl("rblTe st"));
    Response.Write( rblTemp.Selecte dItem.Text);
    }

    I also used the OnInit event to load the composite control as well. Here
    is the code used for that:
    override protected void OnInit(EventArg s e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeCompo nent();
    base.OnInit(e);

    WebCustomContro l1 wcc1Temp = new WebCustomContro l1();
    Panel1.Controls .Clear();
    Panel1.Controls .Add(wcc1Temp);
    }

    By the way, in the future, it would be best to post these questions in the
    following newsgroup.

    Microsoft.publi c.dotnet.aspnet

    All .NET asp.net issues, configuration and other questions are posted in
    the newsgroup above.

    The reason why we recommend posting appropriately is you will get the most
    qualified pool of respondents, and other partners who the newsgroups
    regularly can either share their knowledge or learn from your interaction
    with us. Also, this is to make sure that the responders can better track
    the problem. Thank you for your understanding.

    Have a nice day!

    Best regards,
    Yanhong Huang
    Microsoft Online Partner Support

    Get Secure! ¨C www.microsoft.com/security
    This posting is provided "AS IS" with no warranties, and confers no rights.

    Comment

    Working...