简体   繁体   English

如何从代码隐藏更改 asp.net 中文本框的文本

[英]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.单击按钮后,我正在尝试将文本框的文本更改为增加 1。

When I use this, txtcounter.Text = txtcounter.Text + 1当我使用它时, 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.该值增加 1,但是当我关闭并再次打开窗口时,它会变回原来的值。 What should I do to make it remain as the incremented value even after closing the window.即使在关闭窗口后,我应该怎么做才能使其保持为增量值。

The page language is vb页面语言是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!你的意思是当我关闭WEB页面时/之后如何保持价值!

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.如前所述,在软件中,你所做的 99% 不是关于编写代码,而是你如何构建你试图解决的问题和问题。

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).会话的含义正是 t 的含义 - 您可以保留和保留当前会话的值(当前会话表示您在该网站上花费的时间)。 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.所以,这里一个不错的选择是使用 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.如果您返回此网页,则该值将保持正确,并且只要您在该站点并访问该站点,该值就会保留在内存中 (session())。

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.另一种可能的方法是使用 cookie,并将值保存在浏览器中。 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.这也是可行的,只要您不清除浏览器中的 cookie 或清除历史记录,那么该值实际上会作为浏览器 cookie 存储在您的计算机上。

In effect, your question is a fantastic question - since a simple cookie will work, and even work after you close down your browser.实际上,您的问题是一个绝妙的问题 - 因为一个简单的 cookie 可以工作,甚至在您关闭浏览器后也可以工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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