WebForm response and request and cookies

Source: Internet
Author: User

Built-in objects:

Response object: Responding to a request
Response.Write ("<script>alert (' Add success! ') </script> ");
Response.Redirect ("default.aspx");//Redirect to Default.aspx

Request object: Get Requests
request["Key"] to get the value passed in.

QueryString: Address bar data transfer? Key=value&key=value
Note: Things that do not need to be kept secret can be transmitted
Do not pass long things, because the length is limited, too long will cause data loss
Increase:
First write an Add method in the data connection class
public bool Insert (Users u)
{
BOOL isOK = false;
Cmd.commandtext = "INSERT into Users values (@a,@b,@c,@d,@e,@f)";
Cmd. Parameters.clear ();
Cmd. Parameters.Add ("@a", u.username);
Cmd. Parameters.Add ("@b", U.password);
Cmd. Parameters.Add ("@c", u.nickname);
Cmd. Parameters.Add ("@d", u.sex);
Cmd. Parameters.Add ("@e", u.birthday);
Cmd. Parameters.Add ("@f", u.nation);

Conn. Open ();
Try
{
Cmd. ExecuteNonQuery ();
isOK = true;
}
Catch {}
Conn. Close ();
return isOK;
}
Then, build the form in the Web Form
<form id= "Form1" runat= "Server" >
User name: <asp:textbox id= "TextBox1" runat= "Server" ></asp:textbox><br/>
<br/>
Password: <asp:textbox id= "TextBox2" runat= "Server" textmode= "Password" ></asp:textbox><br/>
<br/>
Repeat Password: <asp:textbox id= "TextBox3" runat= "Server" textmode= "Password" ></asp:textbox><asp:label id= " Label1 "runat=" Server "text=" "></asp:label><br/>
<br/>
Nickname: <asp:textbox id= "TextBox4" runat= "Server" ></asp:textbox><br/>
<br/>
Gender: <asp:radiobuttonlist id= "RadioButtonList1" runat= "server" repeatdirection= "horizontal" repeatlayout= "Flow" >
<asp:listitem value= "true" selected= "true" > Male </asp:ListItem>
<asp:listitem value= "False" > Women </asp:ListItem>
</asp:radiobuttonlist><br/>
<br/>
Birthday: <asp:dropdownlist id= "DropDownList1" runat= "Server" ></asp:DropDownList> year
<asp:dropdownlist id= "DropDownList2" runat= "Server" ></asp:DropDownList> month
<asp:dropdownlist id= "DropDownList3" runat= "Server" ></asp:DropDownList> Day
<br/>
<br/>
Ethnicity: <asp:dropdownlist id= "DropDownList4" runat= "Server" ></asp:dropdownlist><br/>
<br/>
<asp:button id= "Button1" runat= "Server" text= "add"/>
</form>
Add the controls to the form form, and then we'll bind the data to DropDownList and delegate the button's Click event.
if (! IsPostBack)
{
for (int i = DateTime.Now.Year; I >= 1900; i--)
{
ListItem li = new ListItem (i.ToString (), i.tostring ());

DropDownList1.Items.Add (LI);
}

for (int i = 1; i <=; i++)
{
ListItem li = new ListItem (i.ToString (), i.tostring ());

DropDownList2.Items.Add (LI);
}

for (int i = 1; i <=; i++)
{
ListItem li = new ListItem (i.ToString (), i.tostring ());

DropDownList3.Items.Add (LI);
}

Dropdownlist4.datasource = new Nationdata (). Select ();
Dropdownlist4.datatextfield = "Nationname";
Dropdownlist4.datavaluefield = "Nationcode";
Dropdownlist4.databind ();
}

Button1.Click + = button1_click;
Then we'll write the click event again.
Users u = new users ();
U.username = TextBox1.Text;
U.password = TextBox3.Text;
U.nickname = Textbox4.text;
U.sex = Convert.toboolean (RadioButtonList1.SelectedItem.Value);
String date = Dropdownlist1.selectedvalue + "-" + Dropdownlist2.selectedvalue + "-" + dropdownlist3.selectedvalue;
U.birthday = convert.todatetime (date);
U.nation = DropDownList4.SelectedItem.Value;
bool OK = new Usersdata (). Insert (U);
if (OK)
{
Response.Write ("<script>alert (' Add success! ') </script> ");
Response.Write ("<script>this.opener.location.href= ' default.aspx '; This.close ();</script>");
}
Else
{
Response.Write ("<script>alert (' Add failed! ') </script> ");
}
OK, add finish
Delete:
First of all, write a method.
public void Delete (string Uname)
{
Cmd.commandtext = "Delete from Users where [email protected]";
Cmd. Parameters.clear ();
Cmd. Parameters.Add ("@a", Uname);
Conn. Open ();
Cmd. ExecuteNonQuery ();
Conn. Close ();

}
Create a new page delete, add a hyperlink to the main page when the button
<a href= "delete.aspx?un=<% #Eval (" UserName ")%>" > Delete </a>
On the delete page, we use request to pass the username over, and then adjust the method
1. Get the primary key value to delete, username
String Uname = request["un"]. ToString ();
2. Delete
New Usersdata (). Delete (Uname);
3. Redirect Back to display page
Response.Redirect ("default.aspx");

Modify:
Main interface and add roughly the same, the main interface does not want to code, good trouble = =
or write a modified method first
public bool Update (Users u)
{
BOOL isOK = false;
Cmd. CommandText = "Update Users set [email protected],[email protected],[email protected],[email protected],[email protected ] where [email protected] ";
Cmd. Parameters.clear ();
Cmd. Parameters.Add ("@a", u.username);
Cmd. Parameters.Add ("@b", U.password);
Cmd. Parameters.Add ("@c", u.nickname);
Cmd. Parameters.Add ("@d", u.sex);
Cmd. Parameters.Add ("@e", u.birthday);
Cmd. Parameters.Add ("@f", u.nation);

Conn. Open ();
Try
{
Cmd. ExecuteNonQuery ();
isOK = true;
}
Catch {}
Conn. Close ();
return isOK;
}
Here we use and delete the same hyperlink when the button is linked to the Modify page
<a href= "default6.aspx?un=<% #Eval (" UserName ")%>" target= "_blank" > Modify </a>
Modify the interface and add roughly the same, the difference is that the changes need to read data from the database and bound to the control, and here I do not write


Login status remains:
Cookies

A piece of text saved on the hard drive of the user's computer

The HTTP protocol includes a browser that allows the site to temporarily store data in the form of cookies on the user's computer.

If no save time is set, session cookies
1, if you do not refresh the page in 20 minutes, then this cookie will be automatically deleted
2, when the current access connection is interrupted, such as closing the browser, then the cookies will be automatically deleted

Role:
Keep the user's login status

WebForm response and request and cookies

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.