C# ASP.NET 学习记录

1.Calendar控件

 Label1_info.Text += "<br>你的生日:" + Calendar1.SelectedDate.ToString("D");
2.FileUpload控件

FileUpload1.SaveAs(Server.MapPath("upload/"+FileUpload1.FileName));
Label1_info.Text += FileUpload1.PostedFile.FileName + "<br>文件大小    :" + FileUpload1.PostedFile.ContentLength + "<br>文件位置    :" + FileUpload1.PostedFile.ContentType;

3.RequiredFieldValidator、RegularExpressionValidator

4.访问用户控件(xx.aspx里面加入下面代码)

<%@ Register TagPrefix="ucl" TagName="myUserControl" Src="myUserControl.ascx" %>  //注册用户控件,加在代码最前面
<ucl:myUserControl id="myControl" runat="server" OnLoad="myControl_Load"></ucl:myUserControl>     //使用用户控件,加在body里面
5.Request.Cookies(读)和 Response.Cookies(写)

                    vNum = 1;
                    strDate = "未访问过本站!";
                    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"); //定位那个页面

7.Session

Session["name"] = TextBox1.Text; //写入信息
TextBox1.Text = Session["name"].ToString() //读取信息
8.Application

Application.Lock();
Application["show"] =TextBox1.Text + "-----" + Session["name"].ToString() +"/n" + Application["show"];	//读写
Application.UnLock();
9.数据库连接

String strCon = "Data Source=WYZ-PC\\SQL2005;Integrated Security=True;Initial Catalog=mytest"; 
         SqlConnection con = new SqlConnection(strCon);
         con.Open();	//打开
         String strSql = "insert into userinfo Values(1,'wyz','wyz','fujian','nan',15,'222222','2013-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();	//命令执行

10.数据库插入、删除、查询、更新

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 Button1_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);
        //执行删除操作
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        MyBind("select * from userinfo");//调用MyBind()子程序
    }


    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);
        //执行删除操作
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

        GridView1.EditIndex = -1;
        MyBind("select * from userinfo");//调用MyBind()子程序
    }
    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.xml基础操作

XmlDocument xDoc;
    FileStream fStr;
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }
    protected void Button1_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"));  //载入xml文档

        for (int i = 0; i < xDoc.DocumentElement.ChildNodes.Count; i++) //遍历第一层子节点
        {
            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++) //第二层字节点遍历
                {
                    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 = TextBox1_name.Text;
        string strEle = TextBox2_elem.Text;

        //新建一个Element节点,将插入到STUDENT节点的子节点中。 			
        XmlElement elem = xDoc.CreateElement(strName);
        //新建一个Text节点,将其作为PRIZE的值。 			
        XmlText text = xDoc.CreateTextNode(strEle);
        //将text节点添加作为elem节点的值。 			
        elem.AppendChild(text);
        //将elem节点插入到STUDENT节点的第一个子节点处。			
        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");  //保存为另一个xml文档
        
        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];
            //删除LOVE元素
        myPrice.RemoveChild(myEle);
       
        Session["xdoc"] = xDoc;
        fStr.Close();
    }

<?xml version="1.0" encoding="utf-8" ?>
<CLASS>
	<STUDENT NO="202092133">
		<NAME>王娜</NAME>
		<SEX>女</SEX>
		<AGE>25</AGE>
		<LOVE>乒乓球</LOVE>
	</STUDENT>
	<MONITOR NO="202092115">
		<NAME>李兵</NAME>
	</MONITOR>
</CLASS>












你可能感兴趣的:(C# ASP.NET 学习记录)