Reputation: 93
Right now I have made a registration form. I want a user to enter their name and have it save as a cookie. When they reload the website their information will still appear. As I currently have it coded that happens. However, if someone wants to enter new information they have to click my clear button. Is there anyway I can have it where if the user just types in a new name that it will automatically save that cookie and use the newest cookie that was typed. Here is some of my code
Also, to be a little bit more specific, I am wanting it where they can type in a name, register it, reload the page and the name appears, but if they type in a new name over that name and it register, that new name will appear. Right now they have to click clear before hitting register.
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["FirstName"] != null)
{
TextBox1.Text = Request.Cookies["FirstName"].Value;
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Cookies["FirstName"].Value = TextBox1.Text;
Response.Cookies["FirstName"].Expires = DateTime.Now.AddDays(30);
}
Then the way I have them enter new information is the clear button (which I don't want)
protected void Button2_Click(object sender, EventArgs e)
{
DeleteCookie();
}
private void DeleteCookie()
{
HttpCookie cookie = new HttpCookie("FirstName");
cookie.Expires = DateTime.Now.AddSeconds(-1);
Response.Cookies.Add(cookie);
TextBox1.Text = "";
}
Upvotes: 0
Views: 133
Reputation: 15797
It doesn't appear you are including all the relevant code (as you aren't even adding the cookie to the response), but going with what you posted, your problem seems to be with not understanding the ASP.NET Lifecycle.
When they click your button, the Page_Load
event is going to run first (before Button1_Click
). So that means whatever they typed gets overwritten with the cookie value when the cookie exists... so yeah, they can never set a new value until the cookie gets deleted.
So fix this by only setting the value of TextBox1
when it isn't a post back:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && Request.Cookies["FirstName"] != null)
{
TextBox1.Text = Request.Cookies["FirstName"].Value;
}
}
And your button click should look more like:
protected void Button1_Click(object sender, EventArgs e)
{
HttpCookie c = Request.Cookies["FirstName"];
if (c == null)
{
c = new HttpCookie("FirstName");
}
c.Value = TextBox1.Text;
c.Expires = DateTime.Now.AddDays(30);
Response.Cookies.Add(c);
}
Another good thing to realize is that cookies can't really be trusted, as they can be edited by the user. Things like HttpOnly help (secure cookies -- over HTTPS -- are even better, but then you can only read the cookie over HTTPS, which can be problematic unless your entire site is secure), but for sensitive info I like to encrypt the data too... you don't want someone changing the name in the cookie to "Admin" and then getting free reign over your site (assuming that is how you are handling security).
Upvotes: 1
Reputation: 940
You could use the TextChange event. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.textchanged(v=vs.110).aspx
Try:
<asp:TextBox runat="server" id="TextBox1" OnTextChanged="TextBox1_TextChanged" />
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
DeleteCookie();
}
I have never found the TextChanged event very intuitive for the end user because you still need a way for the end user to trigger a postback to get the event to fire.
There are many ways to have them trigger the postback for example OnBlur, pressing Enter, clicking a button. It all depends on what you want the user experience to be.
If it were my UI then I would probably have a hidden Save button that displays when they start typing in the TextBox via jQuery (or Javascript), when they finish typing they click the Save button to trigger the event.
Upvotes: 0