Setting a parent page's label control text from a user control

I have a .net 2.0 site where I'll have the same form for user input on a number of pages. I plan on using a user control for this.

The catch is if the user doesn't input the data properly I want to write the error message to a label control on the parent page, not in the user control. This is because the design for each parent page is different enough that we need to display the error message in a different part of the page.

I've seen a couple of websites that mention using a base page that all my pages could inherit from to have a user control interact with the parent page. This blog post is one of them.

http://blogs.neudesic.com/blogs/jason_jung/archive/2006/09/12/293.aspx 

Is this really the best way to do this?
I'm not familiar with a base page class. Is there any problem with pages inheriting from a custom base page class while at the same time using master pages?

Any other thoughts?
 

0
engwar
1/17/2008 10:28:59 PM
asp.net.getting-started 91979 articles. 3 followers. Follow

2 Replies
776 Views

Similar Articles

[PageSpeed] 45

You definately could do that.  If you do I would create a ControlBase class that inherits from System.Web.UI.UserControl.
Then create your control and replace the base class in each UserControl that you create with ControlBase.
Of course, for this to work you would have to name the label on each page the same.

Ie.  This would be your ControlBase Class: 

public class ControlBase : System.Web.UI.UserControl
{
	public Label ParentPageLabel
    {
        get 
        {
            Label lblParentPageLabel = ( Label ) Page.FindControl ( "lblParentPage" );
            return lblParentPageLabel;
        }
    }
}

 /////////////////////////////////

This would be your userControl:

public partial class UserControls_WebUserControl : ControlBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ParentPageLabel.Text = "Text from a user control";
    }
}
 

///////////////////////////////

This would be your parent page 

<form id="form1" runat="server">
<div>
	<asp:Label ID="lblParentPage" runat="server" /><br />
	<uc1:WebUserControl ID="WebUserControl1" runat="server" />
		
</div>
</form>
 
If everything happens for a reason what is the reason for this error?
0
mpaterson
1/18/2008 12:53:34 AM

Try this in the above code...

Label lblParentPageLabel = ( Label ) Page.FindControl ( "lblParentPage" ).ClientId;


Vishal Khanna
.NET Interviews & FAQs @ www.dotnetUncle.com


Click Mark as Answer if the reply helped you.
0
moredotnet
1/18/2008 5:32:30 AM
Reply:

Similar Artilces:

Label control's text doesn't appear to be set (in the page which has a user control )when trying to update the text property in the FormView's ItemUpdated event
Hi There,I have a user control which hosts a formview control, gridview control and a label control. The grid view is usedto show rows in the DB and when clicked to edit each row, a form view opens up in edit mode with that particular row info. So I have nothing in the item template.My first question is..is it mandatory to have smth in the itemtemplate of the form view.I have controls in edititemtemplate and insertitemtemplate.When a particular row is edited, I would like to show a status msg showing that the row has been updated.For this, I use the ItemUpdated event of the formview and try ...

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

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

Access parent page's controls from a user control
I have a .ascx user control that contains a button. I place this user control on a .aspx web page. The web page contains a textbox. When I click on the button in the user control I want the data in the textbox (in the web page) to be saved to the database. I want to write this code in the click event of the user control's button. Is there any way I can access the parent web page's controls from the user control? Add event to user control which fires on page when button inside user control is clicked. view post 779045Jigar Desai-----------------------Do not forget to "Mark as Answer...

Can I hide a user control's control from the hosting page's client script?
Okay, my previous question was not unswered and I guess you can't hide a control's panel with server code then show it with client code. Now this is what I try to do: on the hosting page's onload event, call a function to hide the user control's penal. Here is the deal: the user control was looped as many times as data required. So the penals will be name mypanel1, my panel2, etc... that is not even a problem if I know if the following is true: * Do all server codes finish running before the page get sent back to the client? Say the database gave me 5 penals, I can then determine how...

Can a User Control's Page_Load retrieve a value set in the parent's Page_Load?
I want to use a control to perform a specific task, but I want it to vary based on the page it is on. I was hoping to set a value in the Page_Load event of the Parent, and have that value retrieved by the Page_Load event of the user control. I am not sure what order these events fire in, Can I be certain that the parent event will fire first?...

How to a call a function from a user control's parent user control ?
 hiHow to a call a function from a user control's parent user control ? for example i have one user control an tree in user control i add other user control in place holder for add node for tree Now how i can rebind tree when i add new node thank a lot  You can rebind the whole page if you do a Page.DataBind(). Alternatively, if you need to bind only one specific control in the parent, you can create an interface such as IMyParentUserControl, create a method definition within it such as:public interface IMyParentUserControl{     void DoMyBinding();} a...

Setting Label's text attribute to the page title's text.
Is there a way to programmatically set a label's text attribute to the current page's title attribute? Such as (pseudo code):  private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here lblPageTitle.Text = PageTitle.Text.Attribute(); }   lblPageTitle.Text = Page.Title();  Mike Banavige~~~~~~~~~~~~Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/ Thank you for the reply, however, Page.Title is not available. Perhaps this is a asp.net 2.0 feature...

Getting User Control's Control Data
I am building a wizard that will pull opt reports.  Page 1 let's you pick a website you want to work with based on listings in a table.  Page 2 displays which options you can include in your opt report by bit fields in the same table that fills the dropdown on Page1.  Based on which fields get  a 1, user controls are loaded to display the options.  My question is this: How can I get the information on the options the user selects from the individual user controls? I've read up on serialization but I don't know how to capture an event that fires when a page submits a...

How to set a Master Page's control's property through content page on a postback event?
Hi all, i m using a master page in my application. i have a panel on master page and a tab container with three tab panels on content page. i want to set/change  the visibility of the panel control when the tab is changed on the content page. By default the panel is invisible. To implement this i executed the following steps: 1. Set AutoPostBack of tacontainer to true 2. Wrote an event handler for ActiveTabIndexChanged event of the tab container 3 Wrote the following code on Tab changed event   if (TabContainerEmpPerformance.ActiveTabIndex == 1) { ...

Can I have a web user control call a method on the page it's parent page?
I'm building an application where I'm using serveral different controls on a page and as one of them causes a postback I may want the page to make changes to other controls.  Is there a way to either fire and event off from the control that the page could then capture or simply have the control call a method of it's parent page? Obviously the Page object is there to work with, but I can't seem to figure out how to specifically cast it as a specific page.  For real man, In your user control you need to create a public event which is raised by the event of the...

User control can't access the parent page's public properties
Hi Guys I have built a page that is contained within a master page and it holds 2 user controls Control A contains a DataListControl B contains a FormView I want to pass the selected index from the DataList on Control A to the FormView on Control B so it acts like a master / detail scenario I created a public property on the page that contains both controls called DataListSelectedIndex A also created a public property for DataListSelectedIndex on the Control  I populate the DataListSelectedIndex property of the Control when the DataList_SelectedIndexChanged...

Accessing Parent's control form User Control?
Hi, I have a Webform and a User Control in it. The form has a panel and textbox in it. The User control has a button in it. Using that Button, I want to show/hide the panel. But, my issue is how to get access to Parent's controls (in this case, the parent control is Panel). Please advice. Thanks in advance. Easy... just make the controls you want to access in the Page public and then cast the Page in the control to the appropriate type and access the now public controls.... uhh??? got that ;-) Example: Page: Name MyPage Controls in MyPage: public System.Web.UI.WebCo...

accessing a dynamically loaded child's page user controls(ascx) from a parent page(aspx)
hi all,i've search the forums and i still can't get it to work.ok here goes...i've created a dropdownlist in the parent(aspx) page, this dropdown will post-back on and load a different child(ascx) page onto a Placeholder each time the user chooses a different selection in the dropdownlist.in the child(ascx) page, say i have a textbox. in the parent(aspx) page, i have a submit button.the placeholder loads accordingly to the dropdownlist's selection, so that is ok.so now the problem is, how do i capture the text in the textbox inside the child(ascx) page from the parent(aspx) p...

Setting a panel control's visibility and it's affect in nested controls.
I am trying to debug a page using the panel control.  The code in the final submission of the form has logic to get data from a dropdown based on the control's visibility.  If the control's visibility is set to true, the the data is retrieved and sent to the database.The problem appears to be the dropdown control's visibility is set to false when it should not.  The dropdown is nested in a panel control.  While doing a trace, it appears when a panel control's visibility is set to false, the visibility for all controls nested under this panel are set to false as well.You're probably asking why use a control's visibility as a condition on what to do with the data.  The answer is I didn't write this page and I intend to change the logic.  Can anyone confirm that when setting the panel control's visibility, you also set the visibility of any nested controls?Thanks,Mike  Yes it will set all the child controls visibility to false.. Think of it this way.. when you are hiding a container.. all the contents will be hidden too.. Here the panel is nothing but a container.. so the contents, which are the child controls will be hidden too when its container is hiddenKumar Reddi Thanks.  I've used panels before to hide controls based on certain conditions.  Until now I had not realized when setting the visibility of the container you're setting that state to all the child controls....

Setting a panel's height to the height of it's parent control
How do I go about getting a panel to be the same height and width of the cell of the GridView or table that it is in?  I was under the impression that if you set the height of the panel to be 100%, then it would become 100% of the parent control.  In this case, I would assume that the parent control would be the cell that it is housed in. When I do this, it makes my entire GridView or table (I've tried both) grow in heighth by a dramatic amount.  I just want the panel to be the same height as the cell when they are each at their minimum height (based on the lines of text ...

Accessing user control values from it's parent page
Hi, I'm trying to solve a problem which I know is pretty simple, yet I can't get to work.  Basically I'm trying to access values which were entered in a user control from it's parent page. My parent page contains a user control (entitled login.ascx), a button and a label. To simplify things, let's say my user control only contains one text box, entitled txtName I want the user to enter some text in my text box, hit the 'Submit' button on my page and the label should display what the user entered.  I can't get this to work no matter what I try and it's driving me mad !! I'm cod...

Accessing the parent page's DataGrid from a user control?
Hello everyone, Greetings from Mr. Newbie, who is in the early stages of an ASP.NET project and wondering about some basic concepts and techniques. I am developing a UserControl that will contain navigational controls (next, previous, first, last) used for DataGrid custom paging. Within the UserControl, I need access to the DataGrid on the page that hosts the UserControl (to determine the DataGrid page size) and to also invoke the method that retrieves the requested page. (The method is in the code-behind page of the form that hosts the UserControl.) If I could pass (to the UserCont...

Rating Control: cannot set currentrating in page's onload event if control is not initially visible
I have a page which contains a rating control which is initially hidden.  On the initial page load (!Page.PostBack) the CurrentRating property is set to 2.  When the page posts back the rating control is made visible, but CurrentRating property has no value.  It loses the value 2.  If the rating control is initially visible the CurrentRating is set properly and maintained properly across postbacks.  The same problem occurs if you include the rating control in a view that is not initially active in it's multiview.<atlastoolkit:rating id="rat...

Set Parent Control from User Control
I have two web forms, one is .aspx and the other is .ascx.  I have no problems getting the user control inside the .aspx file.  This all works fine.  But I have a label control inside the .aspx (parent) that I'd like to set from the Page_Load sub of the user control.  Can anyone help me out in VB?  (Also, this is NOT 2.0)Thanks,Mrp You could use  Parent object in user control to get the reference of the aspx page.e.g.aspx:<form id="Form1" method="post" runat="server"> <asp:Label id="Label1" ru...

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

how to get reference to parent page and/or parent user control
I have a server control that needs to access either the immediate user control that contains it, if the control is directly contained in a usercontrol, or get the parent page. The reason it needs to do so is to determine whether these implement a particular Interface that exposes certain methods that determine the server control behaviour. By using "this.Page" it is easy to gain access ot the page, but I am not sure how one goes about getting the parent usercontrol. Is looping up the parent control hierarchy the only option? Thanks, Olja olja:Is looping up the parent control hierarchy t...

VS.Net 2002 Munging label and textbox id's in a user control
When I am editing labels and textboxes in a user control when I end template editing the labels and textboxes are all renamed to label1, label2, etc. WHY???? All I do is move a couple labels and copy and paste a button and then click end template editing! Here is the original code followed by the munged code: <%@ Control Language="vb" AutoEventWireup="false" Codebehind="NewCaseAddress.ascx.vb" Inherits="NYSIF.WCClaim.NewCase.Web.UserControls.NewCaseAddress" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %> <s...

Updating a Label control in one user control from another user control
Hi all, I have a main page in my music store site that has 5 user controls on it with 1 always showing main content and the others being things like a shopping cart, "best sellers", genre list etc (a la amazon). From the main content control I want to be able to set some label controls in my shopping basket control but I just can't seem to work out the syntax for how to do this: Here's the code I'm using - I'm trying to go to the 4th user control on the page (my shopping basket) then find the appropraite label, cast it locally as a Label then update the value: Label _so...

Web resources about - Setting a parent page's label control text from a user control - asp.net.getting-started

Resources last updated: 2/16/2016 5:36:04 PM