简体   繁体   English

在ASP .NET C#中更改文本框输入后重置会话

[英]Reset session upon textbox input change in asp .net c#

I have session in a page and would like to reset this session when the user text input changes. 我在页面中有会话,并且想在用户文本输入更改时重置此会话。 For example: account number xxx will have a session, once account number changes to yyy i would like to reset session. 例如:帐号xxx将有一个会话,一旦帐号更改为yyy,我想重置会话。 I dont have login to this page so cannot dump session upon logout. 我没有登录到此页面,因此无法在注销后转储会话。 help is appreciated. 帮助表示赞赏。 thank you in advance. 先感谢您。

Try this: 尝试这个:

in aspx: 在aspx中:

    <asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" />

in code behind: 在后面的代码中:

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        Session.Clear();
    }

So you'll need an event handler that is fired when the account number changes (or whenever the user text input changes), and then according to this post you'll need to either use Session.Clear() which just removes all values from the object or Session.Abondon() which will destroy the session and trigger the Session_OnEnd event . 因此,您将需要一个事件处理程序,该事件处理程序在帐户号更改时(或在用户文本输入更改时)触发,然后根据此帖子,您将需要使用Session.Clear() ,该操作从中删除所有值对象或Session.Abondon() ,它将破坏会话并触发Session_OnEnd事件 Whichever you use will depend on what you want to accomplish. 无论使用哪种方式,都取决于您要完成什么。

在文本框的“ onkeyup”事件上进行ajax调用并在该会话中设置新值,您将获得文本框的新值并可以在会话中进行设置。

Use TextChanged event of TextBox and destroy the session using Session.Abandon() or Session.Clear() method This will help you 使用TextBox TextChanged事件并使用Session.Abandon()Session.Clear()方法销毁会话。
If you write down code here, then we can give you exact answer 如果您在此处写下代码,那么我们可以为您提供准确的答案

try this 尝试这个

  <asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>

code behind 背后的代码

protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        Session["your_sesion_name"]=TextBox1.Text;//or what ever you want to update value
    }

There are a couple of ways to do what you are asking. 有两种方法可以完成您要问的事情。

Option one: 选项一:

You could uses the text boxes ontextchanged event 您可以在ontextchanged事件中使用文本框

<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>

in code behind: 在后面的代码中:

 protected void TextBox1_TextChanged(object sender, EventArgs e)
 {
        Session.Clear();
 }

The pit fall here is the OnTextChanged event is only fired once the textbox has lost focus. 此处的陷阱是仅在文本框失去焦点后才触发OnTextChanged事件。

Option 2: Uses JavaScript to capture the onkeyup event and make an ajax call back to the server to update your session information 选项2:使用JavaScript捕获onkeyup事件并向服务器进行Ajax调用以更新您的会话信息

The pit fall here is that every time the user enters a character into the textbox you would be making the ajax call. 这里的陷阱是,每当用户在文本框中输入一个字符时,您都将进行ajax调用。 Although you could add checks so that the call is only made if the account number is a certain length. 尽管您可以添加支票,以便仅在帐号一定长度的情况下才可以拨打电话。

Option 3: You could add a button to your form for the user to click once they have entered the account number. 选项3:您可以在表单中添加一个按钮,供用户在输入帐号后单击。 This would avoid needless calling the server on every key press as in option 2. Then within you buttons click event you could do the work with your session 这样可以避免在每次按键时都像选项2那样不必要地调用服务器。然后在按钮内单击事件,就可以完成会话工作

 <asp:TextBox ID="TextBox1" runat="server"  />
 <asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" Text="Submit" />

Code behind 后面的代码

 protected void btnSubmit_Click(object sender, EventArgs e)
 {
     // do session work
 }

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

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