简体   繁体   中英

How to change the text of a textbox in asp.net from code-behind

I'm trying to change the text of a textbox to increase by 1 after I click a button.

When I use this, txtcounter.Text = txtcounter.Text + 1

The value increase by 1 but, it changes back to its original value when i close and open the window again. What should I do to make it remain as the incremented value even after closing the window.

The page language is vb

Well, it depends on what you mean by close the web page?

I mean, if I open up note pad on my desktop, type some text in and then close it and don't save the text then next time I open the notepad file, the text will not have been saved.

So, like all software? The MOST imporant lesson here is what we call context. So, when you say you close the window? Well, you need to be MUCH more specific here. You mean how can I keep the value when/after I close the WEB page!

And if you don't mean close the web page, then your question is not well formed. As noted, in software, 99% of what you do is NOT about writing code but HOW you frame the question and problem you are attempting to solve.

So, there is a HUGE amount of context required here.

Just like when I close down a desktop applcation, unless that information is saved before I close that applcation, then the information will be gone.

Same goes for that web form.

So, if you want to KEEP the value, and then say navagate to other web pages, and then when you come back to that web page, and want the value (the counter) to remember this then fine.

However, if you have web site to keep track of your tea cup collection, and you close the web page, and want the information to re-appear tomorrow when you add more tea cups to your web applcation that keeps track of your tea cup colleciton?

Then you need to file away that information in a database.

So, for what we call the current session - the time from when you enter the web site and start using web pages?

Then there is what call "session()". Session means exactly what t means - you can keep and persit values for the current session (current session means the time you spending on that web site). Session can save values, keep values, but once you close down the web site (or even go to some other web site) then the session is gone.

so, a good choice here would be to use session.

When the page first loads, we can set the value of a counter - say a integer value, place the value into a text box. since the value is saved in session, then we can move around on the web site, but WHEN we return to that web page, our counter value will still exist.

However, if you talking about actually closing down the web page or leaving the web site, then session would not work anymore, and we would need a database to keep that value for the next day when we return.

So, for session based, your web page could look like this:

        <h3>My Counter</h3>
        <asp:Label ID="lblCounter" runat="server" Text="0" Font-Bold="true" Font-Size="Large">
        </asp:Label>
        <br />
        <br />

        <asp:Button ID="Button1" runat="server" Text="click me to increment counter" />

And code behind would look like this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        ' first page load - add counter to session
        If Session("MyCounter") Is Nothing Then
            ' create a session value
            Session("MyCounter") = 0
        End If
    End If

    lblCounter.Text = Session("MyCounter")

End Sub

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Session("MyCounter") = Session("MyCounter") + 1
    lblCounter.Text = Session("MyCounter")

End Sub

So, you can now jump to other pages in your web site, do whatever. If you return to this one web page, then the value will remain correct, and remain in memory (session()) as long as your at the site and visiting the site.

However, if you REALLY going to close the window (the browser page), then you would have to adopt a database system, or some other kind of data storage system, since when closing down the web page and leaving the site, the values are going to disappear and be gone.

The other possible approach would be to use a cookie, and save the value in the browser. This also would work, and as long as you don't clear out your cookies in the browser, or clear history, then the value would actually be stored as a browser cookie on your computer.

In effect, your question is a fantastic question - since a simple cookie will work, and even work after you close down your browser.

So, this code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Request.Cookies("MyCounter2") Is Nothing Then
        Dim MyCookie As New HttpCookie("MyCounter2")
        MyCookie.Value = 0
        lblCounter.Text = 0
        Response.Cookies.Add(MyCookie)
    Else
        lblCounter.Text = Request.Cookies("MyCounter2").Value
    End If

End Sub

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim intCount As Integer = Request.Cookies("MyCounter2").Value
    intCount += 1

    ' note the use of "response" here!!!! - not request!!!!
    Response.Cookies("MyCounter2").Value = intCount

    lblCounter.Text = intCount


End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM