如何从code-behind中更改asp.net中的文本框的文本

发布于 2025-02-09 02:45:09 字数 162 浏览 2 评论 0原文

我正在尝试将文本框的文本更改为单击按钮后增加1个。

当我使用这个时, txtcounter.text = txtcounter.text + 1

值增加1个,但是当我关闭并再次打开窗口时,它会更改为原始值。 即使关闭窗口后,我该怎么做才能使其保持为增量值。

页面是VB

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笙痞 2025-02-16 02:45:09

好吧,这取决于您关闭网页的意思吗?

我的意思是,如果我在桌面上打开Note Pad,请输入一些文本,然后将其关闭并保存文本,然后下次我打开记事本文件时,文本将无法保存。

那么,像所有软件一样?这里最重要的教训是我们所说的上下文。那么,当您说您关闭窗户时?好吧,您需要在这里更具体。您的意思是,当我关闭网页时/之后,我该如何保持价值!

而且,如果您不是要关闭网页,那么您的问题就不会很好。如前所述,在软件中,您所做的99%不是编写代码,而是您如何尝试解决问题。

因此,这里需要大量上下文。

就像我关闭桌面应用时一样,除非在关闭该应用程序之前保存该信息,否则信息将消失。

该网络表单也是如此。

因此,如果您想保留该值,然后说纳入其他网页,然后当您返回该网页并希望值(计数器)记住这一点时,然后就可以了。

但是,如果您有网站可以跟踪您的茶杯收藏,并关闭网页,并希望明天在网络应用中添加更多茶杯时,请重新出现信息,以跟踪您的茶杯colleciton ?

然后,您需要在数据库中提交该信息。

因此,对于我们称为当前会话的内容 - 输入网站并开始使用网页的时间?

然后就是这样称为“ session()”。会话准确的含义是t的含义 - 您可以保留当前会话的值并持久值(当前会话意味着您在该网站上花费的时间)。会话可以保存值,保持值,但是一旦您关闭了网站(甚至转到其他网站),则会将消失。

因此,这里的一个不错的选择是使用会话。

当页面首次加载时,我们可以设置计数器的值 - 例如整数值,将值放入文本框中。由于该值保存在会话中,因此我们可以在网站上四处走动,但是当我们返回该网页时,我们的计数器值仍然存在。

但是,如果您谈论实际关闭网页或离开网站,那么会话将不再起作用,并且我们需要一个数据库来保留第二天我们返回时的值。

因此,对于基于会话的,您的网页看起来像这样:

        <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" />

背后的代码看起来像这样:

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

因此,您现在可以跳到网站中的其他页面,请执行任何操作。如果您返回一个网页,则该值将保持正确,并且只要您在网站上并访问网站,就可以保持在内存(Session())。

但是,如果您真的要关闭窗口(浏览器页面),则必须采用数据库系统或其他类型的数据存储系统,因为关闭网页并离开网站时,值为要消失并走了。

另一种可能的方法是使用cookie,并将值保存在浏览器中。这也可以工作,只要您在浏览器中清除饼干或清晰的历史记录,那么该值实际上将作为浏览器cookie存储在计算机上。

实际上,您的问题是一个很棒的问题 - 因为一个简单的饼干将起作用,甚至在关闭浏览器后工作。

因此,此代码:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文