Jump to content

error on if statement inside <script runat="server">


joecool2005

Recommended Posts

Thank you for answerfor this code

<script  runat="server">	Dim s As String 	Sub Page_Load()  		If Not IsPostBack Then			s = "hello"		End If	End Sub</script>

For the "s" variable when IsPostBack=true, is it possible to keep the old value? Because anytime IsPostBack=true the value is lost.

Link to comment
Share on other sites

How about this.I want to count the number of time the page is post back. How can I do that? This code below always shows 1 anytime the page is post back. Or, how can I pass the "x" variable to increment?

	Dim x As Integer 	Sub Page_Load()		If Not IsPostBack Then			x = 0		Else			x = x + 1		End If		Response.Write(x)	   	End Sub

Thx

Link to comment
Share on other sites

HTTP is a stateless protocol. As soon as the page is finished loading and the response is sent to the browser, the server has completely forgotten about everything that it just did. If you want it to "remember", then you're going to have to store that information somewhere.You might try using a hidden input element:

<input type="hidden" id="hid_PageLoads" value="0" runat="server" />

Then, in your code (I don't know VB, you'll have to convert it):

void Page_Load(object sender, EventArgs e){	// get the value that is currently stored in the hidden input element	int pageloads;	int.TryParse(hid_PageLoads.Value, out pageloads);	// increase that number by 1	pageloads += 1	// and reassign that new value to the element so the next time the page loads	// we'll be able to know how many times the page was loaded.	hid_PageLoads.Value = pageloads;}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...