C # ASP. NET learning records

Source: Internet
Author: User

1. Calendar Control

Labeldeskinfo.text + = "<br> your birthday:" + Calendar1.SelectedDate. ToString ("D ");

2. FileUpload Control

FileUpload1.SaveAs (Server. mapPath ("upload/" + FileUpload1.FileName); labelemediinfo.text + = FileUpload1.PostedFile. fileName + "<br> file size:" + FileUpload1.PostedFile. contentLength + "<br> File Location:" + FileUpload1.PostedFile. contentType;

3. RequiredFieldValidator, RegularExpressionValidator

4. Access the user control (Add the following code in xx. aspx)

<% @ Register TagPrefix = "ucl" TagName = "myUserControl" Src = "myUserControl. ascx" %> // Register the user control and add it to the beginning of the Code.
<Ucl: myUserControl id = "myControl" runat = "server" OnLoad = "myControl_Load"> </ucl: myUserControl> // use the user control to add it to the body.

5. Request. Cookies (read) and Response. Cookies (write)

VNum = 1; strDate = "not visited this site! "; Response. cookies ["visit"] ["vNum"] = vNum. toString (); Response. cookies ["visit"] ["myDate"] = strDate; Response. cookies ["visit"]. expires = DateTime. now. addDays (1 );
                    vNum = Int32.Parse(Request.Cookies["visit"]["vNum"]) + 1;                    strDate = Request.Cookies["visit"]["myDate"].ToString();

6. Response. Redirect ("Regist. aspx ");
// Locate the page

7. Session

Session ["name"] = TextBox1.Text; // write information
TextBox1.Text = Session ["name"]. ToString () // read information

8. Application

Application. lock (); Application ["show"] = TextBox1.Text + "-----" + Session ["name"]. toString () + "/n" + Application ["show"]; // read and write Application. unLock ();

9. Database Connection

String strCon = "Data Source = WYZ-PC \ SQL2005; Integrated Security = True; Initial Catalog = mytest"; SqlConnection con = new SqlConnection (strCon); con. open (); // Open String strSql = "insert into userinfo Values (1, 'wyz', 'wyz', 'fujiian ', 'nan', 15, '123 ', '2017-4-29') "; // String strSql =" insert into userinfo Values (, '"+ TextBox1.Text +"', '"+ TextBox2.Text + //"', '"+ DropDownList1.SelectedItem. text + "','" + RadioButtonList1.SelectedItem. text + // "'," + Convert. toInt32 (TextBox4.Text) + ", '" + TextBox5.Text + "'," + System. dateTime. now + ")"; SqlCommand cmd = new SqlCommand (strSql, con); cmd. executeNonQuery (); // Command Execution

10. Database insertion, deletion, query, and update

Protected void page_load (Object sender, eventargs e) {If (! Page. ispostback) {mybind ("select * From userinfo") ;}} void mybind (string strsql) {string strcon = @ "Data Source = lab307-17 \ sql2005; Integrated Security = sspi; initial catalog = mytest; "; sqlconnection con = new sqlconnection (strcon); sqldataadapter da = new sqldataadapter (strsql, con); dataset DS = new dataset (); DA. fill (DS); gridview1.datasource = Ds; gridview1.databind ();} protected void button#click (Object sender, eventargs E) {string strsql = "select * From userinfo where username = '" + textbox1.text + "'"; mybind (strsql);} protected void gridview1_rowdeleting (Object sender, gridviewdeleteeventargs E) {string strcon = @ "Data Source = lab307-17 \ sql2005; Integrated Security = sspi; initial catalog = mytest;"; sqlconnection con = new sqlconnection (strcon ); string SQL = "delete from userinfo where userid =" + gridview1.datakeys [E. rowindex]. value; sqlcommand cmd = new sqlcommand (SQL, con); // execute the delete operation con. open (); cmd. executenonquery (); con. close (); mybind ("select * From userinfo"); // call mybind () subroutine} protected void gridview1_rowupdating (Object sender, gridviewupdateeventargs E) {string strcon = @ "Data Source = lab307-17 \ sql2005; Integrated Security = sspi; initial catalog = mytest;"; sqlconnection con = new sqlconnection (strcon); textbox name, PWD, prov, sex, age, intro, date; name = (textbox) gridview1.rows [E. rowindex]. cells [3]. controls [0]; Pwd = (textbox) gridview1.rows [E. rowindex]. cells [4]. controls [0]; prov = (textbox) gridview1.rows [E. rowindex]. cells [5]. controls [0]; Sex = (textbox) gridview1.rows [E. rowindex]. cells [6]. controls [0]; age = (textbox) gridview1.rows [E. rowindex]. cells [7]. controls [0]; intro = (textbox) gridview1.rows [E. rowindex]. cells [8]. controls [0]; Date = (textbox) gridview1.rows [E. rowindex]. cells [9]. controls [0]; string strsql = "Update userinfo set username = '" + name. text + "', [Password] ='" + PWD. text + "', Province ='" + Prov. text + "', sex ='" + sex. text + "', age =" + convert. toint16 (age. text) + ", Intro = '" + intro. text + "', register_time ='" + date. text + "'where userid =" + gridview1.datakeys [E. rowindex]. value; sqlcommand cmd = new sqlcommand (strsql, con); // execute the delete operation con. open (); cmd. executenonquery (); con. close (); gridview1.editindex =-1; mybind ("select * From userinfo"); // call the mybind () subroutine} protected void gridview1_rowediting (Object sender, gridviewediteventargs E) {gridview1.editindex = E. neweditindex; string strsql = "select * From userinfo where userid =" + gridview1.datakeys [E. neweditindex]. value; mybind (strsql);} protected void gridview1_rowcancelingedit (Object sender, gridviewcancelediteventargs e) {gridview1.editindex =-1; mybind ("select * From userinfo ");}

11. Basic xml operations

XmlDocument xDoc; FileStream fStr; protected void Page_Load (object sender, EventArgs e) {} protected void button#click (object sender, EventArgs e) {// DataSet ds = new DataSet (); // ds. readXml (Server. mapPath ("class. xml "); // GridView1.DataSource = ds. tables [1]. defaultView; // GridView1.DataBind (); xDoc = new XmlDocument (); xDoc. load (Server. mapPath ("class. xml "); // load the xml document for (int I = 0; I <xDoc. D OcumentElement. childNodes. count; I ++) // traverse the first layer of sub-nodes {XmlElement xmlE = (XmlElement) xDoc. documentElement. childNodes [I]; for (int j = 0; j <xmlE. attributes. count; j ++) {Label_msg.Text + = xmlE. attributes [j]. name + ":" + xmlE. attributes [j]. value + "<br>"; for (int k = 0; k <xmlE. childNodes. count; k ++) // L2 byte point traversal {Label_msg.Text + = xmlE. childNodes [k]. firstChild. value + "<br>" ;}} Session ["xdoc"] = XDoc;} protected void Button2_Click (object sender, EventArgs e) {xDoc = new XmlDocument (); fStr = new FileStream (Server. mapPath ("class. xml "), FileMode. open); xDoc. load (fStr); int iLocation = Convert. toInt32 (TextBox_location.Text); string strName = textboxw.name.text; string strEle = TextBox2_elem.Text; // create an Element node and insert it into the subnode of the STUDENT node. XmlElement elem = xDoc. CreateElement (strName); // create a Text node and use it as the value of PRIZE. XmlText text = xDoc. CreateTextNode (strEle); // Add the text node as the elem node value. Elem. AppendChild (text); // Insert the elem node to the first subnode of the STUDENT node. XDoc. documentElement. childNodes [iLocation]. appendChild (elem); Session ["xdoc"] = xDoc; fStr. close ();} protected void Button4_Click (object sender, EventArgs e) {xDoc = new XmlDocument (); xDoc = (XmlDocument) Session ["xdoc"]; xDoc. save (Server. mapPath (". ") +" // class2.xml "); // save as another xml document Response. redirect ("class2.xml");} protected void Button3_Click (object sender, EventArgs e) {xDoc = new XmlDocument (); fStr = new FileStream (Server. mapPath ("class. xml "), FileMode. open); xDoc. load (fStr); int i1 = Convert. toInt32 (TextBox3_delPlace.Text); int i2 = Convert. toInt32 (TextBox4_delEle.Text); XmlElement myPrice = (XmlElement) xDoc. documentElement. childNodes [i1]; XmlElement myEle = (XmlElement) myPrice. childNodes [i2]; // Delete the LOVE element myPrice. removeChild (myEle); Session ["xdoc"] = xDoc; fStr. close ();}

<? Xml version = "1.0" encoding = "UTF-8"?> <CLASS> <student no = "202092133"> <NAME> Wang na </NAME> <SEX> female </SEX> <AGE> 25 </AGE> <LOVE> Table Tennis </LOVE> </STUDENT> <monitor no = "202092115"> <NAME> Li Bing </NAME> </MONITOR> </CLASS>


Related Article

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.