0

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.

hima
  • 610
  • 3
  • 10
  • 24
  • 2
    Could you please be more clear about what do you want to do and what do you exactly mean by "session" ? Do you mean the Session State ? and how is a login-logout related to your request ? – sm_ Jul 03 '13 at 12:41
  • yes session state. this page is loaded just by the url, so no login is used to reach this page and any account can access this page. all i want to do is when account number is changed the session state should be reset. – hima Jul 03 '13 at 13:42

6 Answers6

2

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();
    }
dvjanm
  • 2,351
  • 1
  • 28
  • 42
  • 2
    The only thing to remember here is that the TextChanged event only fires when the text box has lost focus. Also to get the server side event to fire the text box needs AutoPostBack set to true. – Kevin Kunderman Jul 03 '13 at 12:56
  • No, it's not true. TextChanged event fire if the control has different value than it has in the viewstate. AutoPostBack property doesn't need to be set to true. – dvjanm Jul 03 '13 at 13:14
  • From what I have read if you want the TextChanged event to fire you need AutoPostBack set to true http://stackoverflow.com/questions/5029804/textchanged-event-function-not-working http://www.dotnetfunda.com/forums/thread11821-how-to-fire-check-text-change-event-in-aspnet.aspx – Kevin Kunderman Jul 03 '13 at 13:33
  • 1
    @KevinKunderman is right, it works only when autopostback is set to true but i dont want to go this way. i have already used onblur on this html input field which populates the account details using jquery, but i dont know how to reset session simultaneously. thank you in advance – hima Jul 03 '13 at 13:44
  • If you set AutoPostBack to true it posts back when the TextBox looses focus and the text changed in the TextBox also. But regardless of which control in the page caused the postback, the TextChanged event will fire for the TextBox. – dvjanm Jul 03 '13 at 13:51
  • @jannagy02 That’s assuming something else is posting back the page other than when the textbox text has changed. – Kevin Kunderman Jul 03 '13 at 13:59
  • I just said it is how the page life cycle works. Depending on what is the concrete purpose of the questioner the AutoPostBack can cause an unnecessary load to the server. If AutoPostBack is not set to true, when you press the button, what happens is, that the TextChanged fires, and after that the button Click event fires. – dvjanm Jul 03 '13 at 14:21
1

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.

Community
  • 1
  • 1
Jfabs
  • 543
  • 4
  • 9
  • 23
1

make ajax call on "onkeyup" event of textbox and set new value in that session, you will get new value of textbox and can set it in session.

1

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

ravidev
  • 2,708
  • 6
  • 26
  • 42
0

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
    }
sangram parmar
  • 8,462
  • 2
  • 23
  • 47
0

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
 }
Kevin Kunderman
  • 2,036
  • 1
  • 19
  • 30