Re: Dynamically Loaded UserControl and ViewState

Discussion in 'ASP .Net' started by S. Justin Gengo, Feb 17, 2004.

  1. Joey,

    I have some samples of how to dynamically add controls to pages on my
    website, www.aboutfortunate.com. There are a few code samples that may help
    you out. Just go to the site and click the "Code Library" link in the upper
    right corner. Then type "Dynamic" into the search box and you'll see all the
    samples there.

    The samples are each pretty short and may help you understand dynamic loads
    a little better. If you still have questions feel free to email me. (Or post
    here, but if you email I'll get back to you quicker.)

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    Free code library at:
    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzche


    "Joey G" <> wrote in message
    news:...
    > I am having a problem updating the value of a webcontrol after postback

    when the webcontrol resides in a dynamically loaded usercontrol. In stepping
    through the code in debug mode, and the dynamically loaded control is being
    loaded on postback, the value of the control is empty as if it does not have
    access to the viewstate. That's the first problem (or lack of
    understanding). The second problem is that when I updated the webcontrol on
    postback, the value of the webcontrol is reset to the value in the viewstate
    when all pages are completly loaded. Please help. Here's the code I was
    testing with.
    > DEFAULT.ASPX
    > -----------------------
    > <%@ Register TagPrefix="uc1" TagName="WebUserControl"

    Src="WebUserControl.ascx" %><%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="default.aspx.vb" Inherits="test._default"%><!DOCTYPE HTML PUBLIC
    "-//W3C//DTD HTML 4.0
    Transitional//EN"><HTML><HEAD><title>WebForm1</title></HEAD><body
    MS_POSITIONING="GridLayout"><form id="Form1" method="post"
    runat="server"><TABLE height="76" width="388"><TR><TD
    align="right">Dynamic</TD><td><asp:panel ID="bx"
    Runat="server"></asp:panel></td></TR><TR vAlign="top"><TD align="right">Not
    Dynamic</TD><td><uc1:WebUserControl id="WebUserControl"
    runat="server"></uc1:WebUserControl></td></TR><TR><TD
    align="right"></TD><td><asp:Button ID="sm" Text="Submit"
    Runat="server"></asp:Button></td></TR></TABLE></form></body></HTML>
    > ---------------------
    > DEFAULT.ASPX.VB
    > --------------------
    > Public Class _default
    > Inherits System.Web.UI.Page
    > Protected WithEvents sm As System.Web.UI.WebControls.Button
    > Protected WithEvents bx As System.Web.UI.WebControls.Panel
    >
    > #Region " Web Form Designer Generated Code "
    >
    > 'This call is required by the Web Form Designer.
    > <System.Diagnostics.DebuggerStepThrough()> Private Sub

    InitializeComponent()
    >
    > End Sub
    >
    > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As

    System.EventArgs) Handles MyBase.Init
    > 'CODEGEN: This method call is required by the Web Form Designer
    > 'Do not modify it using the code editor.
    > End Sub
    >
    > #End Region
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As

    System.EventArgs) Handles MyBase.Load
    > Dim ctl As UserControl
    > ctl = LoadControl("webUserControl.ascx")
    > bx.Controls.Add(ctl)
    > End Sub
    >
    > End Class
    > --------------------
    > WEBUSERCONTROL.ASCX
    > --------------------
    > <%@ Control Language="vb" AutoEventWireup="false"

    Codebehind="WebUserControl.ascx.vb" Inherits="test.WebUserControl"
    TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %><asp:TextBox
    id="txt" runat="server"></asp:TextBox></TD>
    > ----------------------
    > WEBUSERCONTROL.ASCX.VB
    > ----------------------
    > Public MustInherit Class WebUserControl
    > Inherits System.Web.UI.UserControl
    > Protected WithEvents txt As System.Web.UI.WebControls.TextBox
    >
    > #Region " Web Form Designer Generated Code "
    >
    > 'This call is required by the Web Form Designer.
    > <System.Diagnostics.DebuggerStepThrough()> Private Sub

    InitializeComponent()
    >
    > End Sub
    >
    > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As

    System.EventArgs) Handles MyBase.Init
    > 'CODEGEN: This method call is required by the Web Form Designer
    > 'Do not modify it using the code editor.
    > InitializeComponent()
    > End Sub
    >
    > #End Region
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As

    System.EventArgs) Handles MyBase.Load
    > If Not IsPostBack Then
    > txt.Text = "First load"
    > Else
    > txt.Text = "Post load"
    > End If
    > End Sub
    > End Class
    >
    S. Justin Gengo, Feb 17, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan
    Replies:
    0
    Views:
    388
  2. Matt Howeson
    Replies:
    0
    Views:
    1,729
    Matt Howeson
    Mar 18, 2005
  3. Rosemary
    Replies:
    5
    Views:
    327
    Rosemary
    Mar 13, 2008
  4. Urbal

    Need help persisting a dynamically loaded userControl...

    Urbal, Mar 22, 2005, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    111
    Brock Allen
    Mar 22, 2005
  5. Joey G

    Dynamically Loaded Control and ViewState

    Joey G, Feb 17, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    124
    Joey G
    Feb 17, 2004
Loading...

Share This Page