default.asp code
<%
@dotnet.itags.org.PageLanguage="VB"AutoEventWireup="true"CodeFile="Default.aspx.vb"Inherits="_Default" %><!
DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><
htmlxmlns="http://www.w3.org/1999/xhtml"><
headrunat="server"><title>Untitled Page</title></
head><
body><formid="form1"runat="server"><div><asp:ScriptManagerID="ScriptManager1"runat="server"EnablePartialRendering="True"></asp:ScriptManager><asp:UpdatePanelID="UpdatePanel1"runat="server"><ContentTemplate><asp:LabelID="Label1"runat="server"Text="Label"></asp:Label></ContentTemplate><Triggers><asp:AsyncPostBackTriggerControlID="Timer1"EventName="Tick"/></Triggers></asp:UpdatePanel><asp:TimerID="Timer1"runat="server"Interval="2000"></asp:Timer></div><br/><asp:TextBoxID="TextBox1"runat="server"AutoPostBack="True">Input Test</asp:TextBox><asp:ButtonID="Button1"runat="server"Text="Button"/></form></
body></
html>
vb code
Partial
Class _DefaultInherits System.Web.UI.PageDim tempAsStringProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)HandlesMe.LoadIf Page.IsPostBackThenLabel1.Text = temp
EndIfEndSubProtectedSub Button1_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles Button1.ClickDim iAsIntegerFor i = 0To 100temp =
String.Format("{0} {1} {2}", TextBox1.Text, i,"<br />")NextEndSubEnd
Class
The problem im getting is each time the page do a postback the temp's value is nothing. What do i need to look into to make it work?
Thanks!!
ASP.NET creates a new page object to service each request. This typically makes the application faster, more scalable and more reliable.
Therefore, you cannot maintain state in a field within a class (as you're attempting to do above).
You either need to store the data on the page (using ViewState, which generates a hidden field in the HTML) or in a Session object.
Both of these use simple indexing to put data in and get data out, as shown below:
ViewState("TempValue") = 10 ' put a value into the ViewState collection for this page
Dim i As Integer = CInt( ViewState("TempValue" ) ) ' get the value back out of the ViewState collection
Normally you should add code to make sure that the value you're reading is not nothing.
Be aware that ViewState data is present in the client HTML and can thus be read (and altered) unless you turn on ViewState protection. ViewState can really only be used on a single page.
Session data, on the other hand, is maintained on the Server and is thus more secure. Session can also be used across multiple pages.
You might want to check out some of the posts in the State Management forum (and in the ASP.NET documentation) for more details on ASP.NET state management technologies.
hi,
is it the case that you want to display value of temp in label if button is clicked,
simple after your for loop write this Label1.Text = temp in button click.
regards,
satish.
I'll go read on that topic some more .. Thanks
What i want to do is to have the temp value update each time from the loop
hi,
in your button click you've written temp = some value in for i=1 to 100, ok. so what will happen is at last it will contain values for i=100.
to persist value of temp you could store it in viewstate or a hidden field with runat=server. then in page load set temp = viewstate("temp")
then set lable.text = temp.
regards,
satish.
0 comments:
Post a Comment