473,372 Members | 991 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

Server.Transfer To Same Page Not Preserving Control Values

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

}
}
Nov 18 '05 #1
2 3339
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.

Pete wrote:
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);

}
}

Nov 18 '05 #2
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" <bl********@as.ro> wrote in message
news:OM**************@TK2MSFTNGP10.phx.gbl...
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.

Pete wrote:
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);

}
}

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: brian | last post by:
Hello I have a Server.Transfer from Page A to Page B. When a user presses 'Refresh', I don't want Server.Transfer to be executed again. What is the best way to trap this? I don't want to burn a...
4
by: Bob H | last post by:
Hi, I'm aware of the different ways to pass variables between ASP.Net pages. For pages that need to pass a variable, we're using the server.transfer method. For others, we're using...
11
by: Alexander Bosch | last post by:
Hi, I'm having a problem similar to the one that's stated in this KB http://support.microsoft.com/default.aspx?scid=kb;en-us;839521 When I'm posting a page to itself with the bool value as true it...
2
by: RJN | last post by:
Hi Is it possible to a server.transfer and open the page in a new window? I need to pass values between forms and hence using server.transfer. I do not want to use query string for passing...
2
by: David Berman | last post by:
It seems that my site is losing session information when using Server.Transfer. I have a page called PictureGallery.aspx. It takes an argument which is an index id, so it would look like...
5
by: ChaunceyMo | last post by:
Hi all, some help would be much appreciated on this issue: We have a fairly large asp.net app which until now has been using response.redirect without issue to move between pages. Unfortunately,...
8
by: p3t3r | last post by:
I am using .NET2 and have a number of aspx pages. On each page is a LinkButton that performs a server.transfer() to another page. If we use page names A,B,C,D,E as an example. I start on page A...
2
by: Totto | last post by:
Hi, I'm doing a server.Transfer to a page with http-equiv="refresh" but when the refresh occurs the page that was doing the Server.Transfer is loaded. Anyone know if there is a way round this....
3
by: MMA | last post by:
Hi All, I am trying to do a Page.Server.Transfer on a web control and am getting this error, "Error executing child request for SecureRegistration.aspx." Any suggestions. Thanks in Advance
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.