user control - how to return value from user control property

testUserControl.ascx

 

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="testUserControl.ascx.cs" Inherits="testUserControl.testUserControl" %>


<asp:Label ID="Label1" runat="server" Text="User control label"></asp:Label>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

 testUserControl.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace testUserControl
{
    public partial class testUserControl : System.Web.UI.UserControl
    {

        protected string _rtnValue;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public testUserControl()
        {

            _rtnValue = string.Empty;
        }


        public string RtnValue
        {
            get { return _rtnValue; }
            set { _rtnValue = value; }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            this.Label1.Text = this.TextBox1.Text;
            RtnValue = this.TextBox1.Text;
            //_rtnValue = this.TextBox1.Text;
        }



    }
}

 Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="testUserControl._Default" %>

<%@ Register src="testUserControl.ascx" tagname="testUserControl" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Value from user control"></asp:Label>
        
        <br />
        
        <uc1:testUserControl ID="testUserControl1" runat="server" />
        
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    
    
    
    </div>
    </form>
</body>
</html>

 Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace testUserControl
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            this.Label1.Text = this.testUserControl1.RtnValue;
        }
    }
}

 

when I type something in the texbox (textbox in user control), and then click left button (button in user control)

then the label (User control label) is replaced by what I have typed in the textbox

then I click right button (button on the page), the expected result is that the label (Value from user control) is replaced by the text in the textbox (textbox in user control) [the text as same as the label in user control]

but it is changed to be null (empty string).

how to achieve what I want?

thank you very much!

and I have another problem: is there any difference when I type

_rtnValue = this.TextBox1.Text;

 instead of 

RtnValue = this.TextBox1.Text;
 

 

in testUserControl.asxs.cs   ?

 

thank you very much!

0
LonelyRonaldinho
1/11/2009 6:49:36 AM
asp.net.web-forms 93655 articles. 5 followers. Follow

1 Replies
925 Views

Similar Articles

[PageSpeed] 41

man.. the variables are not persisting the data. Your each button is causing the postback and each time the UserControl's code behind class is instantiated and also the fields inside that class. So it's obvious that after the left buttons click fired and page loaded on the client browser the variable(with the class) is vanished. then you click the right button and the user control's class is instantiated again with the fresh variable _retval; change the RetVal property as below

 

public string RtnValue
{
  get { 
     if(ViewState["RtnValue"]==null)
         return string.Empty;
     return ViewState["RtnValue"].ToString();
  }
  set { ViewState["RtnValue"]= value; }
}
 
Always remember to click “Mark as Answer” on the post that helps you. Thank you!


- Regards,
Paresh Jagatia
http://pareshjagatia.blogspot.com
0
pareshjagatiya
1/11/2009 9:38:28 AM
Reply:

Similar Artilces:

Web User Control to Web User Control...
Hi, I have 2 User controls on a page and want Control1 to call a method of Control2. Is this Possible? Example: Con1 = Datalist showing products, each with a Quantity Box and a Button. Con2 = Repeater showing Cart Preview (name, Price) in page.aspx. When I click on the button on the DataList in Con1 it adds the item into the Session based Cart. However it does not update the Cart Preview. I need to call BindPreview() in Con2 in order to update the Cart Preview... Does that make sense? argh! any help much appreciated! PeteWeb Design Nottingham, UK Well I can call BindPreview() from m...

access controls on web user control x from web user control Y
I have 2 web users controls in a page and I would like to get or set the selected values of a few dropdown boxes on web user control X from web user control Y and I also would like to know how to access functions that are on a web user control X from a web user control Y, is this possible? How can I do it? User can access and set the values of the User Control from .aspx page through properties,using javascript and in code-behind of aspx page.The details of it are shown below Using PropertiesIf the Sample.ascx control has two textboxes and submit button.You can access the valu...

Getting value of a property in Parent User control from a Child user control
I have a ChildUserControl that is loaded inside a ParentUserControl. The host page loads ParentUserControl.I would need to access properties in ParentUserControl from the ChildUserControl.Thanks for your time Here's some ideas: You can from the child access the parent and run findcontrol. child.parent.findcontroL("indyanguy1") child.parent.parent.findcontrol("indyanguy1") The trick is when you get your findcontrol working to cast the result to the object type your looking forHere I'm looking for a label so I cast to label then I can use the .Text property.Y...

Unable to access user control's user control's function\property from another user control
Hi, I used to call an user control's user control function as stated below from my user control in ASP.Net 1.1 wucCompany.wucEmployee.GetEmployeeSomething() After migrating to ASP.Net 2.0, I am unable to use any properties/functions(even the public ones) of the user control's User control's from another user control.  The way I have to do is create property\function in wucCompany which calls the wucEmployee's property\function and call the wucDepartment.GetEmployeeSomething().Since I need to do this change in too many places, I can...

Problem in accessing web form data in user controls when create user control dynamically
HI. I have a problem using web user control. I designed a user control named NewsForm. This user control contains some web form elements like <asp:textbox>. When I add it in an .ASPX page, all things may be true. I can know which user filles in textbox (for example and other controls) <%@ Register TagPrefix="uc1" TagName="NewsForm" Src="NewsForm.ascx" %> and <UC1:NewsForm id="newsForm" runat="server"></UC1:NewsForm> But when i create it in code behined dynamically, however user control has web form controls but their value are blank for example //aspx co...

User control with Property to connect to other user control
Hi,I'm a bit a newb to ASP.NET ;)I want to create a user control which can be connected to another user control. So I did the following:   Private IntBreadcrumbsControl As MenuControl <Category("CMS"), Description("Set the MenuControl that displays the breadcrumbs"), Browsable(True)> _ Public Property BreadcrumbsControl() As MenuControl Get Return IntBreadcrumbsControl End Get Set(ByVal value As MenuControl) IntBreadcrumbsControl = value End Set End Property Unfortunat...

UpdatePanel within a web user control resets data of controls outside the user control
Hello, I have a usercontrol on my page which displays a running marquee text. It makes use of an UpdatePanel (within the user control) to refresh the marquee text (within the updatepanel) at periodic intervals. For this I added a timer control and its tick event as an async postback trigger for the UpdatePanel. I have a lot of textboxes and dropdown list boxes (all with runat=server) on the page outside the above user control. These are also contained in different UpdatePanels. Problem is, when the user control does a postback to refresh the marquee text, it causes some of...

User Controls in User Controls?
I have the follow set up and it is throwing an error "Unhandled error loading module" ... is there some trick to this that I don't know?I have a project A that uses controls from project B (project B is just a project with all my user controls used through out other modules).  project A uses user control 1 from project B with out any issues at all - great.  But then I wanted to add user control 2 to user control 1 and that is when I get my error.  In fact I don't even have the user control 2 in user control 1, just when I put the register part into user control 1 is when it al...

Persisting data in a web user control, which contains a list of other web user controls.
 Hi All, I have a control I've created, which can have a number of child controls - also user controls - stored as an ArrayList, which can be added wither a design time or programmatically.My problem is when I add to the list programmatically, it doesn't persist them. So if I add 3 child controls in step 1 of a wizard, with the aim that those three will be visible inside the parent in step 3, what happens is that the parent has no child controls and the item count is 0.Can anyone help? Thanks in advance.Code:Parent Control Markup: <%@ Control Language="C#" Auto...

User control in a user control
Hello all, I have created a user control with some checkboxes, and public properties to get the values of these checkboxes. I have also created a second user control using the first user control; this second control also access the public properties of the first one. My problem is when I implement the second control in an aspx page, if I click at runtime the checkboxes in the first control, I am able to access its public properties from the methods of the second control but the values of these properties are wrong. while at design time, I do not have this problem. Than...

User Control in a User Control
I have a first user control that contain a second user control. If I cahe the first user control can I still dynamically control the second one or it is cached with the first one therefore can not control it anymore? Thanks When your first control is cached, its rendered out and its output is stored in the cache. On every request to the page containing your control that can satisfy it from cache, the output will just simply be inserted - your first control will never be instantiated or run in that case, so the second one wont be either. Hope this helps,Mike VolodarskyProgram Manager...

Using property values of user control between two web forms.
Hi,I am stuck with a problem.I have a user control on more than one web form( say webform1 and webform2 ).The user control consists of several hyperlinks(say hyp1 and hyp2 ).When I enter values in webform1 and then click 'save' button , one of the property of user control (say ID ) is assigned some value. I want to use that value 'ID' in webform2 after the user clicks on hyp2. I dont think using Querystring will be useful because hyp1 is selected be default. It is not a compulsion for the user, that he has to click on hyp2..he can also click on other hyperlinks of the user control also...Ple...

user control to user control
I have an aspx page that have a "Register" directive to two usercontrol called rptMaster.ascx and rptDetail.ascx. I'm trying to set up a Master-Detail relationship and wish for the rptMaster.ascx to trigger and event from a dropdownlist and for the rptDetail to be listening for DropDownList event. I believe I have gotten the events part okay with help from this articel http://codeproject.com/aspnet/Page_UserControl.asp but my declaration for a private statement to my rptMaster class is producing an error. I'm using beta 2 of asp.net and I 'm having trouble getting a reference TO ...

User Control Within UserControl
If I have a user control A and then inside it a user control B embedded/referenced within it.  User Control A has a panel defined (cpeNewUser) that I want to manipulate from the code behind of User Control B based on some event that happens.So putting this in my code behind of User Control B won't work as it has no idea of cpeNewUser:cpeNewUser.Collapsed = true;So is it because only at runtime they are married up and because of this you cannot access each other's objects at compile time?  How would I get this to work becuase I'm using all user controls inside a AJAX Too...

Web resources about - user control - how to return value from user control property - asp.net.web-forms

Property - Wikipedia, the free encyclopedia
This article may require copy editing for grammar, style, cohesion, tone, or spelling . You can assist by editing it . In abstract, property ...

Property - Wikipedia, the free encyclopedia
In abstraction, property is that which belongs to or with something, whether as an attribute or as a component of said thing. For the significant ...

Chinese property firm Yuhu hires ex-deputy premier Andrew Stoner
A Chinese-based property firm that has donated hundreds of thousands of dollars to Australian political parties has added former deputy premier ...

How property developers make windfall millions
Buy one thing, use spin and insider knowledge to get planning approval to turn it into something completely different and, hey presto, you've ...

$1M transformation takes property from hoarding hell to heavenly home in Brisbane’s inner city - The ...
A HOARDER’s hovel has been transformed in seven months from a stinking mess to a dream family home — because of the efforts of two determined ...

Real estate: Couples opting for discount weddings to get a foot on property ladder
WHEN it comes to saving up for a home, life can get in the way.

Property estates on former Perth school sites popular with buyers - PerthNow Search Search
CRAIGIE, about 24km from the CBD, is the site of Perth’s latest residential school conversion. The first 22 blocks at the site of the former ...

Chinese property firm Yuhu hires ex-deputy premier Andrew Stoner
A Chinese-based property firm that has donated hundreds of thousands of dollars to Australian political parties has added former deputy premier ...

Esperance Bushfire: Reports of property losses as fires rage in south WA
Fast-moving, out-of-control bushfires threaten lives and homes in the Esperance region in southern Western Australia.

Seattle startup ranks every property in King County based on its redevelopment potential
... best properties to buy and what to build there. The 1-year-old company, Everyhome, already operates a website where buyers can bid on any property, ...

Resources last updated: 11/21/2015 4:11:52 AM