简体   繁体   中英

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. I dont have login to this page so cannot dump session upon logout. help is appreciated. thank you in advance.

Try this:

in 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 . 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
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

<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.

Option 2: Uses JavaScript to capture the onkeyup event and make an ajax call back to the server to update your session information

The pit fall here is that every time the user enters a character into the textbox you would be making the ajax call. 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. 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

 <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
 }

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