I have an ASP.NET website application on .NET 4.0. There is one Masterpage that contains the header and footer for all the aspx pages. The content comes from the individual aspx pages. I have BasePage.cs which all the aspx pages inherit from.
Now to the problem:
I have a HTML Select control on the masterpage, whose value I am trying to retrieve in the BasePage.cs using the below code
string language = ((System.Web.UI.HtmlControls.HtmlSelect)Master.FindControl("cmbLanguage")).Value;
I am using this inside the InitializeCulture method, which would set the Culture info for the website.
protected override void InitializeCulture()
{
string language = ((System.Web.UI.HtmlControls.HtmlSelect)Master.FindControl("cmbLanguage")).Value;
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(language);
Thread.CurrentThread.CurrentUICulture = new
CultureInfo(language);
base.InitializeCulture();
}
While debugging, I can see that the expected value is set in the language variable. The problem is when the page renders, the content inside the ContentPlaceHolder for the aspx page is not being rendered.
I can see that it is the code involving FindControl which is the cause, because if I set the language to a string, everything works as expected.
string language = "de-DE";
What am I doing wrong?
UPDATE:
If there is some content on the ContentPlaceHolder on the MasterPage, then it gets rendered instead of the page ContentPlaceHolder.
InitializeCulture is called before even PreInit in the page life cycle, which means the controls haven't been setup, and the Value of that control is likely coming through as an empty string.
You need to likely change how the culture is read, through a cookie, session value, or some other method. I'm not familiar with doing it, so i don't have a great suggestion or best practice.
As Doozer correctly noted that control is unlikely to have the correct value set up into it at the time of InitializeCulture. I will suggest that you read the value from POST data in the request and back it via some default value. For example,
string language = Request.Form[Master.FindControl("cmbLanguage")).UniqueID];
language = string.IsNullOrWhiteSpace(language) ? "de-DE" : language;
In order to access MasterPage controls its better to use MasterType directive. When used you are going to be able to access master page in a strongly typed way. In this case you will be able to create a property on master page like this:
public string SelectedCulture
{
get
{
return cmbLanguage.Value
}
}
And on page itself you will be able to run code like this:
protected override void InitializeCulture() {
string language = this.Master.SelectedCulture;
}
Related
How to access the web page public variable from user control in asp .net ?
Your page class derived from asp.net Page has this variable.
Every control has a Page property. If you know that this control will be placed on a particular class derived from Page, you can always cast it to the proper type. Then you have access to the public variable.
var propValue = ((MyControlBasePage)Page).MyProperty;
Ensure that the base class for the pages on which you use this control is Page derived form MyControlBasePage.
If you plan to use this on only one Page you could always directly use
var propValue = ((MyPage)Page).MyProperty;
But then, whats the user of a UserControl that you use on only one page.
you can do like this:
<%= this.usercode %>
At the moment I have a master page and a content page. My master page consists of a dropdownlist, from which I need to select a child from. The content page loads according the child chosen. How should I store the dropdownvalue chosen?
I don't think I can use the session, since I would like the user to be able to open multiple tabs and watch different children contents at the same time. If I can use the session in this case, I'm not sure how.
I don't think I can use the viewstate since, although it solves my multiple tabs problem, the master page and content page have a different view state.
At the moment I am using a public static variable on the content page, and I set it in the master page. But from what I've heard static variables have their values stored throughout ALL the current sessions on the site.
Question:
Can any one help me by suggesting which technology should I use?
I have also heard about the 'Application' object but I don't think it makes sense to use it.
Current working Code:
(content page)
public static string Child
{
get
{
if (child == null)
return "-1";
return child;
}
set
{
child = value;
}
}
(master page)
protected void ddlChooseChild_IndexChanged(object sender, EventArgs e)
{
ContentPage.Child = ddlChooseChild.SelectedValue;
}
The best way to share data between different controls is to make use of the "Items" collection (which is a property of the HttpContext class). The collection is a Hashtable and can be accessed from your Page and User Controls like so:
Context.Items["Child"] = ddlChooseChild.SelectedValue;
Unless you are restricting session by using PageID or something similar, Sessions are available on multiple tabs.
Check out this article, Master Content Page interaction
I am writing a web app in asp.net I have a master page that contain a ContentPlaceHolder
and a form that wrapper the ContentPlaceHolder, In a aspx page I realize the ContentPlaceHolder and have some controls in this page.
Now when I Trying to use Request.Form.Get("my control name") (from the aspx behind code), I get null.
If I try to add a form in the aspx page I get an error that say you can have only one form in a page.
How can i get the values in my controls??
thanks for the help.
Request.Form("YourControlName") will not work with server controls because ASP.NET adds some extra stuff to the name of your control when it outputs it to the page. It does this to make sure that the name remains unique across all the controls on the page. So, for example, your control might actually be named something like "ctl00_maincontent_placeholder1_YourControlName" when it gets created on the page.
In ASP.NET this is not usually a problem because you typically do NOT use Request.Forms to get your control values. Instead you use the server control's methods to get the values. So, for a textbox, you would use YourControlName.Text to get the value that was entered into the textbox.
If you are just trying to communicate a value between the master and page, assuming the value is on the master you can cast Page.Master to the correct type. On the master page you can wrap controls on the master.
MasterPage
public string MyControlText
{
get
{
return myControl.Text;
}
set
{
myControl.Text = value;
}
}
On the page
((MyMasterPage)this.Page.Master).MyControlText = "To master from page";
string fromMasterToPage = ((MyMasterPage)this.Page.Master).MyControlText;
I'm working on a asp.net web application and one of the requirements is that the user has to be able to select the language they want. I"m using Resx files to store the locals. What my question is do I need to change the CurrentCulture of the thread every time a page is loaded or is there a way to have it handled automatically when a logged in user moves from one page to the next.
Yes, I believe you need to set it every time. To make it even worse, you have to do it by overriding the InitializeCulture method of the Page class. I created a SitePage that all pages in my project inherit from instead of Page to do this.
public class SitePage : Page
{
protected override void InitializeCulture()
{
base.InitializeCulture();
// Set both the CurrentCulture (for currency, date, etc) conversion, and the CurrentUICulture for resource file lookup.
Thread.CurrentThread.CurrentCulture = whatever;
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
}
}
Further reading: http://msdn.microsoft.com/en-us/library/bz9tc508.aspx
I am creating a user control in ASP.NET (using VB) that uses the autocomplete ajax control on a textbox to get a value. Then I want the page to post back and run some code according to whatever value is passed to it from this control. Problem is, I'm not exactly sure how to do this. I'm sure it's easy and I should know, but I don't.
Thanks in advance!
In your user control expose a property for the value
Public Property SomeValue() As String
Get
Return textbox1.Text
End Get
End Property
Then in your aspx page load, just reference the user control's value.
userControl1.SomeValue
Edit, I just tried changing my syntax to vb.net, I don't actually know vb, so that syntax may or may not be right.
((NameOfPage)this.Page).VariableOnPage = this.Foobar;
In the code-behind on your user-control expose a property e.g.
public TextBox UserControlTextBox
{
return this.TextBoxInstance;
}
Then from you page just call
UserControlInstance.UserControlTextBox.Text;