Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User control losing state after postback

Status
Not open for further replies.

Eli20

Programmer
Oct 30, 2003
119
0
0
MX
hi, i have a web user control with some properties, all the properties are set when clicking on a button thats part of the control.
However, when i try to get the value of the properties in my main page, they are null.

i set the eneable view state of my control to true, and the textboxes and labels work fine, but im losing the properties.
can somebody tell me how can i fix it?

thank you very much

Eli
 
YOUR ASPX FILE:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="Examples.WebForm1"%>
<%@ Register TagPrefix="uc1" TagName="MyControl" Src="MyControl.ascx" %>
<HTML>
<body>
<form runat="server">
<uc1:MyControl id="MyControl1" MyText="Hello" runat="server" />
</form>
</body>
</HTML>





YOUR CODE BEHIND:
Public Class WebForm1
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

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

End Sub

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

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
Protected WithEvents MyControl1 As MyControl
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
MyControl1.MyText = MyControl1.MyText & " Again"
End Sub

End Class






YOUR USER CONTROL ASCX:
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="MyControl.ascx.vb" Inherits="Examples.MyControl" %>
<asp:Label ID="Label1" Runat="server" />








YOUR USER CONTROL CODE BEHIND:
Public Class MyControl
Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

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

End Sub
Protected WithEvents Label1 As System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

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 _MyText As String
Public Property MyText() As String
Get
Return _MyText
End Get
Set(ByVal Value As String)
_MyText = Value
End Set
End Property
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label1.Text = _MyText
End Sub

End Class
 
Maintains State even when you alter it like this:

ASCX CONTROL:
If Not Page.IsPostBack Then
Label1.Text = _MyText
End If


ASPX PAGE:
If Not Page.IsPostBack Then
MyControl1.MyText = MyControl1.MyText & " Again"
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top