473,394 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

ASPX page get a value from UserControl Public Property

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>

Nov 21 '05 #1
1 6796
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.

"Will Gillen" <g_i_l_l_e_0_0_1_@_n_s_u_o_k_._e_d_u> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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>

Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael Evanchik | last post by:
Tying not to spaghetti code which seems to be easy to do in .net, im trying to do my main .net html in index.aspx, use repeated .net html in an .ascx files and all code im doing in .vb code behind...
8
by: Jeronimo Bertran | last post by:
I have an aspx page that saves some information using the ViewState. This page has an IMG META that uses a second aspx page to render the image. <IMG src= "ImageRender.aspx"> I noticed that...
10
by: Jon Maz | last post by:
Hi, My goal is to take the entire html/javascript stream spat out by .aspx pages and save them as simple strings in a database (for caching purposes). I'm not sure how I can get hold of this...
1
by: Kruno | last post by:
Hi ! if anyone knows the answer I would appreciate it: I have a user control for the header of the page with one label in it..I want the label to change as the pages are changing.... my...
4
by: David Freeman | last post by:
Hi There! I'm just wondering if there's a way to pass parameters (as if you were passing parameters to a ASCX web control) when calling an ASPX page? e.g. MyDetailsPage.UserName = "david" ...
4
by: louise raisbeck | last post by:
Resending this as own topic as didnt get answer from original. Would be grateful for a response from anyone that knows. Thanks. Hi there, I found your post really helpful..but i wondered if, once...
2
by: theWizard1 | last post by:
Using Visual Studio.Net 2005, Asp.Net 2.0, and trying to use technique shown in the Programming Asp.Net 3rd Edition by O'Reily, page 257. On the first page, this would become the previous page, I...
2
by: badger | last post by:
Hi, I have created a usercontrol with a public property as follows: // default display type to single form. private DisplayType selectedDisplayType = LoginForm.DisplayType.SingleForm; ...
5
by: Fernando Chilvarguer | last post by:
I'm sure this has come up before but I could not find any post on it. How can I read a variable or property that has been set on a ASPX page from inside a ASCX control. ASPX code: public...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.