473,326 Members | 2,196 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,326 software developers and data experts.

Postback problem: Variable lost

I am having problem postback, (code shown below) the variable "my_str" is
lost after clicking the "submit" button. Isn't it suppose to display
"changed" after posting back? Is there anyway I can maintain the state of
a variable?

Imagine the variable is a Class, not String. After clicking the submit
button, I updated some properties in the instance of the class. How do I use
the updated class instance after submitting the form? Help...
<%@ Page %>
<script language="c#" runat="server">
String my_str;
void Page_Load () {
if (!Page.IsPostBack)
my_str = "hello";
else
debug.Text = "debug=" + my_str;
}
public void submit_me (object sender, EventArgs e) {
my_str = "changed";
}
</script>
<form runat="server">
<asp:button runat="server" onClick="submit_me" text="submit"/>
<asp:literal runat="server" id="debug"></asp:literal>
</form>

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.591 / Virus Database: 374 - Release Date: 2/18/2004
Nov 18 '05 #1
1 1883
It is because postback events are raised after Page_Load. E.g on postback
the Page_load happens first (displaying old value) and after that ithe
variable is actually changed. Refer straight to the Label on postback event
and you should be fine (or do processing after postback events say in
PreRender)

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
"Joseph Luner" <J0******************@yahoo.com> wrote in message
news:rK***************@twister01.bloor.is.net.cabl e.rogers.com...
I am having problem postback, (code shown below) the variable "my_str" is
lost after clicking the "submit" button. Isn't it suppose to display
"changed" after posting back? Is there anyway I can maintain the state of a variable?

Imagine the variable is a Class, not String. After clicking the submit
button, I updated some properties in the instance of the class. How do I use the updated class instance after submitting the form? Help...
<%@ Page %>
<script language="c#" runat="server">
String my_str;
void Page_Load () {
if (!Page.IsPostBack)
my_str = "hello";
else
debug.Text = "debug=" + my_str;
}
public void submit_me (object sender, EventArgs e) {
my_str = "changed";
}
</script>
<form runat="server">
<asp:button runat="server" onClick="submit_me" text="submit"/>
<asp:literal runat="server" id="debug"></asp:literal>
</form>

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.591 / Virus Database: 374 - Release Date: 2/18/2004

Nov 18 '05 #2

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

Similar topics

1
by: Joseph Luner | last post by:
I am having problem postback, (code shown below) the variable "my_str" is lost after clicking the "submit" button. Isn't it suppose to display "changed" after posting back? Is there anyway I...
2
by: Ashish | last post by:
Hi All, I have a server runnable textbox control. I also have a server side button object but I hook up javascript to it to show a modal dialog. Upon return it places the value in the textbox...
1
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at...
3
by: Ed Chiu | last post by:
Hi, I plan to use panel to store multiple HTML text in a web form page, here is the code snippet that add HTML into panel: Dim lit1 As New Literal lit1.Text = strHTML...
2
by: summer00 | last post by:
Hi everyone, I found that the value of a variable(string type for example) is lost after the aspx page postback. E.G: private void Page_Load(object sender, System.EventArgs e) {
1
by: ree32 | last post by:
I have a placeholder and depending on a user input(a drop downlist) when the user clicks a button I dynamically create a number of datagrids and fill them with data from a database. But the problem...
1
by: harryaya | last post by:
i separate my application into business layer and presentation layer. After a user logged in, i fetched his/her user information and store them in session object. And then the first page is loaded...
4
by: Frank | last post by:
Hello everyone, I have a problem that the window.opener variable is lost once my popup page has a postback. On multiple pages they address this problem but I cannot find a correct answer. ...
6
by: Dmitry Duginov | last post by:
Hi, I have the following label markup (label is inside FormView): <asp:Label ID="lblIndicatorReady" runat="server" Text="RE" ToolTip="Ready" BackColor='<%#...
1
by: mercea | last post by:
Hi guys, I am taking a column from a table and comparing each row in that table with a similar column in a gridview row by row. For each identical answer, grade increments by 1. after all the rows...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.