ASPX page get a value from UserControl Public Property

W

Will Gillen

I know this has probably been asked before, but I can't seem to find a solid
answer in any of the archives. First, before my question, please forgive my
limited knowledge of the event lifecycle and page loading/rendering
lifecycle....

Ok, now for the question:

I have an ASPX page (page.aspx), and I have a UserControl (control.ascx).
The UserControl has one textbox on the control, and one button control.
I have added a public property (with GET and SET) to the UserControl called
"MyVal".
Inside the Page_Load method for the UserControl, I have the following:
MyVal = TextBox1.Text 'To put the value of the TextBox1.Text into the
Property.


What I would like to have happen is this:
I would like for the ASPX page to be able to "get" the Public Property value
"MyVal" from the UserControl either on Page_Load, or Page_Init, or
Page_PreRender, just after a PostBack event.

However, no matter what example I try, I cannot seem to retrieve the value
from the UserContorl, the CodeBehind in Page_Load for the ASPX page does not
recognize the object "MyControl1", so I am unable to "get" the property.

Here's the ideal scenario:
1. Page initially loads showing the textbox and the button (from the
usercontrol).
2. User types some value into the textbox.
3. User clicks on Submit.
4. UserControl updates the "MyVal" property with whatever they typed into
the box.
5. ASPX Page takes the value from the Property "MyVal" and adds the value
to a text file.

Can anyone point me to some examples of how this can be done? I have seen
MANY examples of how an ASPX page can SET a property before the control is
rendered, but none for a way to GET a property from a control on postback...

Thanks.


CODE:
=====================================================
Page.ASPX:
----------------------------------------------------------------------------
----
Public Class Page
Inherits System.Web.UI.Page

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
If Page.IsPostBack Then
Response.Write(MyControl1.MyVal) 'MyControl1 is underlined, and
gives compile error
End If
End Sub

End Class

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Page.aspx.vb"
Inherits="bbservices.Page"%>
<%@ Register TagPrefix="uc1" TagName="MyControl" Src="MyControl.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD> </HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<uc1:MyControl id="MyControl1" runat="server"></uc1:MyControl>
</form>
</body>
</HTML>


=====================================================
MyControl.ASCX:
----------------------------------------------------------------------------
----
Public Class MyControl
Inherits System.Web.UI.UserControl

Private strValue As String
Public Property MyVal()
Get
Return strValue
End Get
Set(ByVal Value)
strValue = Value
End Set
End Property

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
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MyVal = TextBox1.Text
End Sub
End Class

<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="MyControl.ascx.vb" Inherits="bbservices.MyControl"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<P>&nbsp;</P>
<P><asp:textbox id="TextBox1" runat="server"></asp:textbox></P>
<P><asp:button id="Button1" runat="server" Text="Go"></asp:button></P>
 
W

Will Gillen

Ok, I think I figured it out??
I added this statement to my Page.ASPX:
Protected WithEvents MyControl1 As MyControl

That seems to have worked. Now, why VB.NET doesn't automatically add this
declaration to the codebehind I have no idea???

-- Thanks.
 

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

Top