Change Label.Text in nested Master Pages from Content on button click - c#

This is the situation. I have a masterpage Site.
Master that contains another masterpage Shop.Master.
Inside Shop.Master there is a ContentPlaceHolder that loads Something.aspx .
Inside the top-masterpage there is a label [lblText] present.
The page Something.aspx contains a textbox [boxInput] and a button [btnButton].
What I'm trying to accomplish is when I click button [btnButton] the value lblText.Text is being set to the content of [boxInput].
Here is an abstract view of the problem.
I hope you guys can help me out. Google isn't being a great help this time.
Thanks in advance.

try like this. may it helps
ContentPlaceHolder plchldr= this.Master.Master.FindControl("YourMainMasterContentID") as ContentPlaceHolder;
Label lbl = plchldr.FindControl("lblText") as Label;
if(lbl !=null)
{
lbl.Text="SomeText"
}

This is generally a bit of a weird problem. The only way I've been able to solve this in the past, is something like:
((MasterPageType)this.Master).lblText = "Whatever";
You may need to bubble-up two master pages, as per your specific situation:
((MasterPageRootType)((MasterPageType)this.Master).Master).lblText = "Whatever";
This will obviously fail if you change your Masterpage to be of a different type.
(I's been ages since I did WebForms, so forgive me if this isn't 100%)

Related

What is the best way to have a page read values posted to it from another page?

What is the best way to have a page read values posted from another page? I have something that works, but it seems rather sloppy.
I'm working on a multi-page form with the first page using PostBackUrl to go to the second.
Within page 1 is content placeholder named "CPH1". Inside that is a field named "First_Name".
I submit the first page to the second using PostBackUrl.
The code behind file of the second page then reads the value and places it into a variable using this code:
protected void Page_Load(object sender, EventArgs e)
{
NameValueCollection nvc = Request.Form;
string First_Name_Here = nvc["ctl00$CPH1$First_Name"] ;
}
As you can see, I'm getting the info by using the final name of the field, "ctl00$CPH1$First_Name", instead of "First_Name".
Is there a better way of doing this?
Edit below:
Thanks to everyone for their help. Still haven't figured this out, but have gotten closer.
Using this code:
ContentPlaceHolder CPH1 = Page.Master.FindControl("CPH1") as ContentPlaceHolder;
Response.Write(CPH1.FindControl("First_Name"));
Gives me this result: System.Web.UI.WebControls.TextBox.
What I haven't figured out is how to get the value of the TextBox as a string. It seems like anything I try results in an error.
There are a couple options you have depending on how you are posting to the other page.
You have an option to get the Previous Page controls using this syntax:
TextBox tbControl = (TextBox)PreviousPage.FindControl("First_Name");
You can also expose public properties from your Source page and access it on the target page.
Label1.Text = PreviousPage.First_Name;
MSDN article here about cross page posting: https://msdn.microsoft.com/en-us/library/ms178139.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1
Please go through the below article which contains all the information which you need.
http://www.asp.net/web-forms/overview/older-versions-getting-started/master-pages/control-id-naming-in-content-pages-cs
ContentPlaceHolder MainContent = Page.Master.FindControl("MainContent") as ContentPlaceHolder;
Label ResultsLabel = MainContent.FindControl("First_Name") as Label;
How about you Request.Redirect to go the the other page, you can pass the parameters in the same request and then access them using Request.QueryString["NameoftheParameter"].
For example:-
Calling page 2 with parameter field1:-
Response.Redirect("~/Page2.aspx?field1="xyz.Text);
Access the variable field 1 on page 2 like this:-
String field1 = Request.QueryString["field1"];
Hope this helps!

Page Title from a User Control possible?

I have a Search.aspx page which calls UCSearch control. UCSearch control does everything like getting what is being searched and what should be displayed, etc. I am trying to give the title to the page. As i dont have any info to write the code in the aspx page, i am thinking to write it in the control. But it is not displaying me when i tried using Page.Title in control. What am i doing wrong?? This is in Asp.net and C#.
Page.Title = "Search Results for Newark, NY";
Thanks in advance!!
Does not:
this.Page.Title = "My beautiful title";
work?
You should be able to get to the ASPX using the Parent property of the control. Cast that property to a Page (it's a WebControl or something similarly generic), then set its Title property. If you have a hierarchy of master pages or are nesting this control in other controls, you may need to traverse the Parent hierarchy for a few more levels.
You could also fire an event from your user control, passing the Title that you would like to display. You could then handle this event on the page, and set the title.
This does require a small amount of code in your aspx page, however, at least now the user control does not care where the title goes, the parent of the control can worry about it. If ever you want to change where this title goes, or even put it in multiple places, you don't have to change the user control. Let me know if you want a sample, I'll add it.
If you don't like that idea, then cederlof's answer will work, I just tested it.
protected void helloBtn_Click(object sender, EventArgs e)
{
this.Page.Title = "hello from control";
}
** You can use naming container to find the parent control of the current control. Through this way you can move through the page hierarchy.
Quickwatch will help you a lot in figuring out the things and building the statement for quick casting. Do some more research on naming container.
var container = userControl.NamingContainer;
if(container is Page)
{
Page p = container as Page;
p.Title = "Your Title";
}
**
Above is not the exact solution, but can help you it you can usercontrol directly on the page. Unless you need to iterate through the page controls. This was just for an quick help.

Remove statically added controls at runtime

The Scenario: I have an asp.net website where I show a div popup on page load for taking a few user details. When a user inputs the details, or closes the popup, I set up a flag cookie so that the popup is not displayed again for the user. The div is in the MasterPage so that it is displayed no matter on which page a user lands first time. The div contains an UpdatePanel which has all the controls required for taking the details. This whole functionality is working fine.
The Problem: Now this div popup is not showing(by setting display:none) on subsequent postbacks(which I want), but the html markup is still loading with the page unnecessarily adding to the page size. What I would idealy want to do is: Check if flag cookie is set. If no, show the popup, else remove the popup's markup from the page.
Now since the div is not a server control, I cannot possibly remove it and the all the controls inside it. So, I thought of removing the UpdatePanel from the page:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["flag"] != null)
{
if (Page.Controls.Contains(updpnl_contact))
{
Page.Controls.Remove(updpnl_contact);
updpnl_contact.Dispose();
}
}
}
But I guess this tends to work with dynamically added controls only, and since the control is added at Design Time, it is not being removed.
Is there any way I can achieve this?
If you add a runat="server" attribute to your <div> element, it will be available in the code-behind. You'll need an id on it as well. Then you can just toggle the Visible property. If this property is false, the control won't be rendered to the client (i.e. no HTML markup).
What you're trying to do is not at all the usual workflow. I tend to think that it will not work as it would mess up control tree, maybe even corrupt the viewstate and so on.
As a possible solution, you can put it's visibility to hidden in the code behind. This, in the contrary to the usual 'gut feeling', doesn't work like the css propery 'display:none' for example - instead the control will not even be rendered into the page when it's not visible. This may be the workaround for you.
Happy coding.
A more efficient approach would be to create the panel as a UserControl and load it dynamically in codebehind when it's needed, then add it to your page. E.g, in code:
MyPopupControl popup = (MyPopupControl)Page.LoadControl("/path/to/usercontrol.ascx");
PopupPanel.Controls.Add(popup);
Where PopupPanel is an empty <asp:Panel>. Then, not even the markup will need to be loaded/processed except when its needed.
There is no reason that all the code you use to display and process this panel couldn't also be in the usercontrol, isolating it from the master page.
Can you build the panel dynamically, based on the cookie setting?

Referencing an ASP control within a LoginView

Another total newb question, I'm afraid: I have a LoginView with some HyperLinks inside it, but when I try to reference the HyperLink in the code behind it tells me that it doesn't exist in the "current context".
eg. hypLink1.NavigateUrl = "some/link/on/my/site.aspx"
I've figured out that it's only because it's in the LoginView that it can't find it... so what can I do to tell it to look inside the LoginView?
I thought it might be something intuitive like:
LoginView1.hypLink1.NavigateUrl = "some/link/on/my/site.aspx"
But to no avail.
Thanks for any help with this (most likely) really obvious problem!
I'm guessing that you're trying to reference the hyperlink from outside the loginview control?
At that point, you could try a FindControls operation on the LoginView:
HyperLink hypLink1 = (HyperLink)LoginView1.FindControls("hypLink1");
UPDATE
Ok, so I was confused as to what you were asking. The LoginView control only allows FindControls, and so you have to use the code snippet up above in order to reference controls internal to it.
Since the LoginView control uses templates, different controls will exist under different circumstances. As such, the code cannot ensure any given control inside the template will be alive at compile time.
So you'll have to FindControls every time you want to get a child control :'(

Can I change the text of a label in a masterpage when loading a content page?

I have a label in a master page (sample.master) called lblHeading.
I want to dynamically change the text of the label when I load the content page.
I need to do this because I want to change the heading to something meaningful but only after I know about the content of the page.
Is this possible?
yes, you can in this very simple way........
((Label)Master.FindControl("lblHeading")).Text = "your new text";
Yes.
You want to create a strongly-type master page and you can then access it's properties from your content page during Page_Load or wherever else.
Yes, it is possible. MasterPage behaves just like UserControl in your page.
Possible steps to implement this:
Create a property or method on the MasterPage that enables you to make changes to the Label. E.g.:
public void ChangeLabel(string label) {
lblHeading.Text = label;
}
From your Page, get the reference to the MasterPage by using the Page.Master property.
Call the method defined in step 1 to change the MasterPage contents.
Additional info: You may need to cast Page.Master into your MasterPage type, try Coding the Wheel's link for instructions on how to do that.
Do as stated above. Then, e.g., from your page do this (master page has label with ID="Label_welcome"):
Label mpLabel = (Label)Page.Master.FindControl("Label_welcome");
if (mpLabel != null)
{
mpLabel.Text = "Welcome Fazio!";
}
You can create a public property in the masterpage that will change the label.
public string Heading
{
set
{
lblHeading.text = value;
}
}
It also depends on how deep your controls inside the Master page are. In my case, I had a Label control inside a ContentPlaceHolder control... so I had to do this...
Label myLBL = this.Master.FindControl("HeaderContent").FindControl("myLabel") as Label;

Categories