I have website that has some issue to work with.
One of the issues is loading 5x.
I have been trying to set language speficic for the page.
My problem is as follows:
While page is refreshing n times it does somewhere overrides page language settings. I had as test modified global.asax page in Application_Start method. This method gets called only once.
After this page goes into default.aspx page and hits the page ntimes, when the page gets initialised it change culture to default culture en-GB instead to the one I have set in Page_Load event.
I have set the thread . current . UI and Culture to my specific culture. But this does't work.
I have tried:
Set language in global.asax
Set language in global.asax + onload page for my default.aspx
Use session to set language and after reload the page to display in correct page.
Any ideas?
If you add this to Global.asax.cs it should work. It works for me.
protected void Application_BeginRequest(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("de-ch");
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("de-ch");
}
But your description of the issue is a bit confusing...
Related
This is either a weird behavior or I am doing something wrong here.
I have an aspx page with an associated Master page. I want to replace this master page with a new one.
The following steps I followed:
I created a new master page and added the same html from the old one.
I replaced MasterPageFile attribute in the page directive.
I thought this should work but it is not. It is still showing the old master page.
Now, when I replace the MasterPageFile from code it works.
public DefaultNew()
{
this.PreInit += new EventHandler(DefaultNew_PreInit);
}
void DefaultNew_PreInit(object sender, EventArgs e)
{
MasterPageFile = "~/_Master/MasterPageNew.Master";
}
I have rebuilt the code, closed Visual Studio and restarted but in Vain.
An ideas? Please help.
Check to see if you have a base class for the page that happens to sets the master page for all pages that derive from it.
I am using this code to check if the request came from a page , if not then redirect somewhere.
string referer = Request.ServerVariables["HTTP_REFERER"];
if (string.IsNullOrEmpty(referer))
{
Response.Redirect("/UnauthorizedAccess.aspx");
}
It is working , I don't know whether it is perfect the solution.However I am checking this on load event of one of my page.How can I make it check on every request.Should I check this for all my pages.Also it is a good approach.Can anybody point me in the right direction.Any suggestion is welcome.
If you have logic that you would like to be run on the OnLoad of a bunch of your pages. You should probably create a BasePage that derives from Page and have the logic inside. Then all the pages you want that logic in can derive from BasePage instead of the regular Page.
Another approach can be using Master Pages
Note: After reading OPs additional comments. One thing to look out for when using a Master Page is that the Master Page's Page_Load event happens AFTER the Content Page's Page_Load event.
In other words the lifecycle is like this:
Master Page Init Event
Content Page Init Event
Content Page Load Event
Master Page Load Event
If your response.redirect moves the user to another page with the same master page (and same "validation" check) you might find yourself in an endless loop :)
If you have lot of pages, with these kind of common codes, than one possible solution is creating your own MyPage class as a child of the standard Page class. In your MyPage you can use something like:
Page_Load(object sender, EventArgs e)
{
string referer = Request.ServerVariables["HTTP_REFERER"];
if (string.IsNullOrEmpty(referer))
{
Response.Redirect("/UnauthorizedAccess.aspx");
}
base.Page_Load(sender, e);
}
Then any of your pages can inherit from this own MyPage class instead of the .NET's standard one.
In this way the common code reside in one place. In case of any change you have to modify that only there.
Or another possibility, you can consider using Master Pages.
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;
}
I have a small web app being written in ASP.NET, VB.NET , .NET 3.5
I pass a value from default.aspx to demo.aspx using query string. When Go button is clicked the URL will be like this : localhost/demo.aspx?id=4
But I want URL to be sth like this when Go button is clicked : localhost/?id=4 and the id value is passed to demo.aspx so the expected result can be shown. How to get this done if possible without using the routing technique.
This will work only if the web server considers demo.aspx to be the default document. When you load a page without stating the page name (such as you localhost/?id=4 example), the web server will locate a file with a pre-defined name and use it as the default document. Often this file is called default.aspx (or .asp, .htm, .html, .php or something like that). So if you want to load any other page you will need to either state the name, or use URL rewriting techniques.
Modify your go button to link button and set herf = "?id=[id]"
Modify Page_Load event of Default.aspx by using the following code.
public Page_Load(object sender,EventArg e)
{
if(!string.IsNullOrEmpty(Request["id"]))
{
// Using Server.Transfer() function to call Demo.aspx page.
// Client still see "localhost/?id=[id]" at address bar.
Server.Transfer("Demo.aspx?id=" + Request["id"]);
}
}
Something like this?
protected void btnGo_Click (Object sender, EventArgs e)
{
Response.Redirect("localhost/?" + Request.QueryString);
}
Not sure when you want the url to change? Not sure what the routing technique is either. Sorry.
if you want it to work with postbacks, you still need to make sure the webserver is configured with a default document like default.aspx.
in 3.5 sp1 MS finally allows us to set the action attribute of the form.
so on page init you can explicitly set it now
Form1.Action = "/mydirectory/"
Caveat: Some versions of IIS do not allow posting to a directory. (XP IIS for example)
In an ASP.NET 3.5 site we have a relatively standard payment checkout progess that contains a number of pages that need to be visited in sequence (shopping basket, payment details etc)
Each page has a "Continue" button that redirects to the next page in the sequence.
I would like a way of managing the page sequence so that:
I can have a Master page that defines the "Continue" button and its code-behind OnClick event handler
If the user attempts to visit a page out of sequence (by typing the URL directly into their browser, for example) they get redirected to the correct page
This page sequence is nicely defined in one place in my code (in an enum for example)
Why not use the ASP.NET Wizard control?
Alternatively (and I haven't tried it so I can't say how well it works), you could use Windows Workflow to define a sequential workflow and let that control the order pages come up in. There's an article at http://www.devx.com/dotnet/Article/34732 that takes you through doing it this way.
Check the HttpRequest.UrlReferrer variable in each Page_Load method...
http://msdn.microsoft.com/en-us/library/system.web.httprequest.urlreferrer.aspx
... and don't forget to check for nulls! You can bounce them to where they are supposed to be, based on where they came from.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["PreviousPage"] = Request.UrlReferrer.ToString();
...
}
else
{
...
}
}