Server.Transfer To Same Page Not Preserving Control Values

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

}
}
 
B

bloomfield

Make a method with that load translated strings to the controls like
TranslateAll(). Call this method from the Page_Load (you can test for
IsPostBack to make the translation here only for the first time). Also
call this method from the btnFR_Click and btnUK_Click after you set the
Lang property. No redirect or transfer so it should work as you want.
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);

}
}
 
C

Cosmin Marin

Hi,

In Page_Load, you could also check if the postback was caused by one of the
language change buttons by looking either in the submitted form or in
Page.DeterminePostBackMode(). If for instance btnUK was pressed (and
Request.Form["btnUK"] is not null) you could change the language accordingly
and continue the execution. This method needs further improvements if your
buttons live inside other controls (like a user control).

Cosmin

bloomfield said:
Make a method with that load translated strings to the controls like
TranslateAll(). Call this method from the Page_Load (you can test for
IsPostBack to make the translation here only for the first time). Also
call this method from the btnFR_Click and btnUK_Click after you set the
Lang property. No redirect or transfer so it should work as you want.
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);

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top