473,722 Members | 2,285 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.P age

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
If Page.IsPostBack Then
Response.Write( MyControl1.MyVa l) 'MyControl1 is underlined, and
gives compile error
End If
End Sub

End Class

<%@ Page Language="vb" AutoEventWireup ="false" Codebehind="Pag e.aspx.vb"
Inherits="bbser vices.Page"%>
<%@ Register TagPrefix="uc1" TagName="MyCont rol" 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:MyContro l id="MyControl1 " runat="server"> </uc1:MyControl>
</form>
</body>
</HTML>
=============== =============== =============== ========
MyControl.ASCX:
----------------------------------------------------------------------------
----
Public Class MyControl
Inherits System.Web.UI.U serControl

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.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
End Sub

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

<%@ Control Language="vb" AutoEventWireup ="false"
Codebehind="MyC ontrol.ascx.vb" Inherits="bbser vices.MyControl "
TargetSchema="h ttp://schemas.microso ft.com/intellisense/ie5" %>
<P>&nbsp;</P>
<P><asp:textb ox id="TextBox1" runat="server"> </asp:textbox></P>
<P><asp:butto n id="Button1" runat="server" Text="Go"></asp:button></P>

Nov 21 '05 #1
1 6866
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******** ********@TK2MSF TNGP12.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.P age

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
If Page.IsPostBack Then
Response.Write( MyControl1.MyVa l) 'MyControl1 is underlined, and gives compile error
End If
End Sub

End Class

<%@ Page Language="vb" AutoEventWireup ="false" Codebehind="Pag e.aspx.vb"
Inherits="bbser vices.Page"%>
<%@ Register TagPrefix="uc1" TagName="MyCont rol" 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:MyContro l id="MyControl1 " runat="server"> </uc1:MyControl>
</form>
</body>
</HTML>
=============== =============== =============== ========
MyControl.ASCX:
-------------------------------------------------------------------------- -- ----
Public Class MyControl
Inherits System.Web.UI.U serControl

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.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
End Sub

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

<%@ Control Language="vb" AutoEventWireup ="false"
Codebehind="MyC ontrol.ascx.vb" Inherits="bbser vices.MyControl "
TargetSchema="h ttp://schemas.microso ft.com/intellisense/ie5" %>
<P>&nbsp;</P>
<P><asp:textb ox id="TextBox1" runat="server"> </asp:textbox></P>
<P><asp:butto n 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
2907
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 files. I have no problem using my .vb code behind file for my .aspx pages i just have to say <%@ Page Language="vb" Inherits="myCode" src="index.vb" %> in my aspx file and use Inherits Page in my .vb code behind file and everything is cool ...
8
1900
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 in the ImageRender.aspx, the "sender" parameter in the Page_Load referes to the page that has the IMG tag. The main page saves State information using the ViewState. Can this information be accessed from the ImageRender.aspx?
10
1429
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 html stream, though - does anyone have any strategies / code samples to get me going? Thanks,
1
271
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 question: I want to reference a text property of a label in the control
4
7157
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" OR... the only way to do it is to use the QueryString or Session object?
4
2514
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 I have exposed a public property containing the value of a textbox in a user control..how do I grab this from the calling page? I cant think of the syntax, since my page doesnt know the contents of the class (and therefore, the public...
2
2415
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 have 2 dropdowns. According to their technique, I should create a public property on my previous page for these 2 dropdowns, and then I would would be able to get their settings on the target aspx page. I have added the 2 properties on the...
2
2787
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; public enum DisplayType {
5
14340
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 partial class MyClass: System.Web.UI.Page { protected string _companyID;
0
9234
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9150
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9086
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8028
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6678
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5994
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2146
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.