P
Pete
Hi all...
I sincerly hope one of the MS guys can clear this up for me...
First some background...
Ok, I have a web site which is fully translatable into several
languages. All the strings for the web site are held in a database and
all the labels, buttons etc are populated at run time in the Page_Load
handler. The retreval of the strings from the database is all
encapsulated and the current language is held in session.
There are buttons to change the language on each page.
Historically, the change of language has been done by a
Response.Redirect to another page which then changes the "Language"
session var and then Response.Redirect's back to the UrlReferrer page.
This page renders nothing.
In this way, you can click to change language at any point in the site
and the page just changes in-front of you.
And now to the problem.
Doing things this way obviously looses any form vars on the page. If
you've typed in loads of stuff, checked boxes etc, if you then change
language, all this is gone. This is because of the Redirect. Fine I
accept that.
I recoded this mechanism to change the session var in the page you are
currently using. No Response.Redirect at all, just a normal postback,
so form vars are preserved. However, because the Labels, Buttons are
populated in the Page_Load handler all the page has been set up BEFORE
the Language change event handler was run. So, although the Language
has been changed in session you dont see it until the NEXT postback,
when Page_Load is run again.
I then added a Server.Transer(PageName.Aspx, true) just after the
language change. This works wonderfully, the language change if shown
immediatly. But I'm back to form vars disapearing.
When you click btnFR or btnUK the session var is changed and the page
is tranfered to itself, hopefully, preserving form vars. I shouldn't
run into the EnableViewStateMac bug because I am transfering to the
same page.
When debuging, I can see the Request.Form collection is populated,
E.g. the text from txtData is in there, but when the page is rendered,
txtData does NOT have what i typed in it.
I have knocked together a demo page to show you exactly what I mean.
Can someone please tell me why Server.Transfer is not working? Of why
my textbox does not repopulate itself after the Server.Transfer.
Thanks for any help you can offer.
Test.Aspx
<body>
<form id="Test" method="post" runat="server">
<asp:Label id="lblMessage" runat="server">Label</asp:Label>
<asp:TextBox id="txtData" runat="server"></asp:TextBox>
<asp:Button id="btnUK" runat="server" Text="UK"></asp:Button>
<asp:Button id="btnFR" runat="server" Text="FR"></asp:Button>
</form>
</body>
Text.Aspx.cs
....
public class Test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.TextBox txtData;
protected System.Web.UI.WebControls.Button btnUK;
protected System.Web.UI.WebControls.Button btnFR;
private string Lang
{
get
{
if(Session["Lang"] == null)
Session["Lang"] = "UK";
return (string)Session["Lang"];
}
set
{
Session["Lang"] = value;
}
}
private void Page_Load(object sender, System.EventArgs e)
{
lblMessage.Text = GetMessage();
}
private string GetMessage()
{
if(Lang == "FR")
return "FRENCH";
else if(Lang == "UK")
return "ENGLISH";
else
return "UNKNOWN";
}
private void btnFR_Click(object sender, System.EventArgs e)
{
if(Lang != "FR")
{
Lang = "FR";
Server.Transfer("Test.aspx", true);
}
}
private void btnUK_Click(object sender, System.EventArgs e)
{
if(Lang != "UK")
{
Lang = "UK";
Server.Transfer("Test.aspx", true);
}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.btnUK.Click += new System.EventHandler(this.btnUK_Click);
this.btnFR.Click += new System.EventHandler(this.btnFR_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
}
I sincerly hope one of the MS guys can clear this up for me...
First some background...
Ok, I have a web site which is fully translatable into several
languages. All the strings for the web site are held in a database and
all the labels, buttons etc are populated at run time in the Page_Load
handler. The retreval of the strings from the database is all
encapsulated and the current language is held in session.
There are buttons to change the language on each page.
Historically, the change of language has been done by a
Response.Redirect to another page which then changes the "Language"
session var and then Response.Redirect's back to the UrlReferrer page.
This page renders nothing.
In this way, you can click to change language at any point in the site
and the page just changes in-front of you.
And now to the problem.
Doing things this way obviously looses any form vars on the page. If
you've typed in loads of stuff, checked boxes etc, if you then change
language, all this is gone. This is because of the Redirect. Fine I
accept that.
I recoded this mechanism to change the session var in the page you are
currently using. No Response.Redirect at all, just a normal postback,
so form vars are preserved. However, because the Labels, Buttons are
populated in the Page_Load handler all the page has been set up BEFORE
the Language change event handler was run. So, although the Language
has been changed in session you dont see it until the NEXT postback,
when Page_Load is run again.
I then added a Server.Transer(PageName.Aspx, true) just after the
language change. This works wonderfully, the language change if shown
immediatly. But I'm back to form vars disapearing.
When you click btnFR or btnUK the session var is changed and the page
is tranfered to itself, hopefully, preserving form vars. I shouldn't
run into the EnableViewStateMac bug because I am transfering to the
same page.
When debuging, I can see the Request.Form collection is populated,
E.g. the text from txtData is in there, but when the page is rendered,
txtData does NOT have what i typed in it.
I have knocked together a demo page to show you exactly what I mean.
Can someone please tell me why Server.Transfer is not working? Of why
my textbox does not repopulate itself after the Server.Transfer.
Thanks for any help you can offer.
Test.Aspx
<body>
<form id="Test" method="post" runat="server">
<asp:Label id="lblMessage" runat="server">Label</asp:Label>
<asp:TextBox id="txtData" runat="server"></asp:TextBox>
<asp:Button id="btnUK" runat="server" Text="UK"></asp:Button>
<asp:Button id="btnFR" runat="server" Text="FR"></asp:Button>
</form>
</body>
Text.Aspx.cs
....
public class Test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.TextBox txtData;
protected System.Web.UI.WebControls.Button btnUK;
protected System.Web.UI.WebControls.Button btnFR;
private string Lang
{
get
{
if(Session["Lang"] == null)
Session["Lang"] = "UK";
return (string)Session["Lang"];
}
set
{
Session["Lang"] = value;
}
}
private void Page_Load(object sender, System.EventArgs e)
{
lblMessage.Text = GetMessage();
}
private string GetMessage()
{
if(Lang == "FR")
return "FRENCH";
else if(Lang == "UK")
return "ENGLISH";
else
return "UNKNOWN";
}
private void btnFR_Click(object sender, System.EventArgs e)
{
if(Lang != "FR")
{
Lang = "FR";
Server.Transfer("Test.aspx", true);
}
}
private void btnUK_Click(object sender, System.EventArgs e)
{
if(Lang != "UK")
{
Lang = "UK";
Server.Transfer("Test.aspx", true);
}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.btnUK.Click += new System.EventHandler(this.btnUK_Click);
this.btnFR.Click += new System.EventHandler(this.btnFR_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
}