Page Title from a User Control possible? - c#

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.

Related

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

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%)

Getting Child page title from user control on Master page

I have a user control in the Master page. In the code behind of that ascx, I want to get the title of the page. The title is being set in the head section with tag in the child pages.
Try this
var obj = this.Parent.Page;
var title= obj.Title;
Have you tried Page.Title?
However, I think the head tag needs to be ran server side to use this
I would think Page.Title would be enough. If not, you have to ride up through the object model a bit until you get to the page. Both solutions have been detailed above. The only variation may be if the ascx is set on the master page rather than the page. Worst case here is getting the title in the master page and feeding to the ascx as the page is rendered.
Now, an understanding of why this gets a bit confusing. Most people think the page sits on the master page. But, technically, the master page is set up as a control on the page. This is largely to avoid a complete rearchitecture of ASP.NET as master pages were introduced. This means the page is requested and starts to render. Then the master page linked tags are hit and that "control" is rendered, etc. In some cases, Microsoft has provided easy short cuts, in others, you have to navigate and the navigation is upside down from many people's expectation.

Object reference error

I have this piece of code:-
What I want is that in button click i want put some value in user control.
I get Object reference error.
if (Page.FindControl("pr1") is Control)
{
Control previewControl = Page.FindControl("pr1");
Label titleLabelPreview = (Label)previewControl.FindControl("titleLabel");
titleLabelPreview.Text = emptxt.Text;
However, if i used it through master page it can not intialized
but if i use parent page it work properly.
But here, one more problem persisted, suppose the scenario
use a tab container
where is 3 tab panel
in first panel i used a button
when i click that button i want to change the tab panel view
means 2nd panel
but without refresh the page. and one important thing i don't want to use update panel on tab container. I do not wish to use ajax or javascript. any insight on this.
Also, would be really great if someone gives me a deep idea how to resolve object reference errors.
I read a few blogs like:_
http://www.mikesdotnetting.com/Article/62/Object-reference-not-set-to-an-instance-of-an-object-and-INamingContainer
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.passwordrecovery.usernametemplate.aspx
however, was not fully satisfied.

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 :'(

Categories