简体   繁体   English

获取文本框的先前值asp.net vb.net

[英]Get previous value of textbox asp.net vb.net

I am working on a project and people are allowed to "update" categories, like changing the name of the category. 我正在开发一个项目,人们可以“更新”类别,比如更改类别的名称。 With that I have the following message This is called after clicking on the update button with the SQL statement : 有了这个,我有以下消息这是在使用SQL语句单击更新按钮后调用的:

    Dim cs As ClientScriptManager = Page.ClientScript
            cs.RegisterStartupScript(Me.GetType(), "Message", "alert
(' The category has been changed to:" + TextBoxCategorie.Text + "')", True)

Which display: The category has been changed to: [Value of the textbox] 哪个显示:该类别已更改为:[文本框的值]

But is it possible to store the previous value, so you get something like: 但是是否可以存储先前的值,所以您将得到如下内容:

"Sand" has been succesfully updated to "Mud" “沙”已成功更新为“泥”

Thanks in advance 提前致谢

Create a HiddenField to go along with the TextBox. 创建一个HiddenField以与TextBox一起使用。 When the page initially loads, fill the HiddenField with the initial value. 页面最初加载时,使用初始值填充HiddenField。 On the PostBack, compare the TextBox value to the HiddenField value. 在PostBack上,将TextBox值与HiddenField值进行比较。 You'll have access to both the old and the new. 您将可以同时使用新旧版本。

Just make sure you're not refilling the HiddenField on PostBack - though if you're filling from the database it probably wouldn't matter. 只是确保你没有在PostBack上重新填充HiddenField - 尽管如果你从数据库填充它可能没关系。 But if you fill only on (Not Me.IsPostBack), then on the PostBack, it should be delivering you the value it had when the page was first loaded. 但是如果你只填写(Not Me.IsPostBack),那么在PostBack上,它应该为你提供首次加载页面时的值。

element.defaultValue gives you the original value on clientside. element.defaultValue为您提供客户端上的原始值。

Dim script As String = "var txt=document.getElementById('" & TextBoxCategorie.ClientID & "');if(txt!=null)alert(txt.defaultValue + ' has been changed to: " + TextBoxCategorie.Text + "')"
cs.RegisterStartupScript(Me.GetType(), "Message", script, True)

However, that'll work only if it was changed in this request. 但是,只有在此请求中更改了它才会起作用。 If it was already changed before the page was sent to the client(as RegisterStartupScript suggests) you have to store the old value somewhere else or load it from database. 如果在将页面发送到客户端之前已经更改过(如RegisterStartupScript建议的那样),则必须将旧值存储在其他位置或从数据库加载。

Take a look at the Page Life Cycle Overview and you can grab the value from the TextBox before it is overwritten. 查看页面生命周期概述 ,您可以在覆盖之前从TextBox中获取值。 There is an event named OnTextChanged that would seem to be useful but the issue is that the old value is overwritten by this time (the Page_Load already fires by this point). 有一个名为OnTextChanged的事件似乎很有用,但问题是此时会覆盖旧值(此时Page_Load已经触发)。 I would look into simply using the Page_Init and assigning the TextBox value to a variable prior to overwriting it. 我会简单地使用Page_Init并在覆盖它之前将TextBox值分配给变量。

protected void Page_Init(object sender, EventArgs e)
{
     String oldValue = TextBox.Text;
}

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

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