joecool2005 Posted July 11, 2007 Share Posted July 11, 2007 HiOn this code <script runat="server">If Not IsPostBack Then Dim s As Stringend if</script> Why there is an error on the if statement inside <script runat="server"> ?I want the "s" variable as a global variable. I don't want to reinitialize.Thx Link to comment Share on other sites More sharing options...
aspnetguy Posted July 11, 2007 Share Posted July 11, 2007 try this <script runat="server"> Dim s As String void Page_Load() { If Not IsPostBack Then s = "" End If }</script> Link to comment Share on other sites More sharing options...
joecool2005 Posted July 11, 2007 Author Share Posted July 11, 2007 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 More sharing options...
aspnetguy Posted July 11, 2007 Share Posted July 11, 2007 <script runat="server"> Dim s As String Sub Page_Load() s = "hello" End Sub</script> this will set the value whether it is post back or not. Link to comment Share on other sites More sharing options...
joecool2005 Posted July 11, 2007 Author Share Posted July 11, 2007 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 More sharing options...
jesh Posted July 11, 2007 Share Posted July 11, 2007 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 More sharing options...
aspnetguy Posted July 12, 2007 Share Posted July 12, 2007 or you could use sessions although there are some issues with application pool recycling (this depends on your hosts server settings) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.