473,226 Members | 1,391 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,226 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 6753
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: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.