Dynamically Loaded Control and ViewState

J

Joey G

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.ASP
----------------------
<%@ 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.V
-------------------
Public Class _defaul
Inherits System.Web.UI.Pag
Protected WithEvents sm As System.Web.UI.WebControls.Butto
Protected WithEvents bx As System.Web.UI.WebControls.Pane

#Region " Web Form Designer Generated Code

'This call is required by the Web Form Designer
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent(

End Su

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Ini
'CODEGEN: This method call is required by the Web Form Designe
'Do not modify it using the code editor
End Su

#End Regio

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loa
Dim ctl As UserContro
ctl = LoadControl("webUserControl.ascx"
bx.Controls.Add(ctl
End Su

End Clas
-------------------
WEBUSERCONTROL.ASC
-------------------
<%@ 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.V
---------------------
Public MustInherit Class WebUserContro
Inherits System.Web.UI.UserContro
Protected WithEvents txt As System.Web.UI.WebControls.TextBo

#Region " Web Form Designer Generated Code

'This call is required by the Web Form Designer
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent(

End Su

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Ini
'CODEGEN: This method call is required by the Web Form Designe
'Do not modify it using the code editor
InitializeComponent(
End Su

#End Regio

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loa
If Not IsPostBack The
txt.Text = "First load
Els
txt.Text = "Post load
End I
End Su
End Clas
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top