程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# ASP.NET 學習記錄

C# ASP.NET 學習記錄

編輯:C#入門知識

1.Calendar控件


[csharp]

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

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


[cpp]

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

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裡面加入下面代碼)


[cpp]

<%@ Register TagPrefix="ucl" TagName="myUserControl" Src="myUserControl.ascx" %>  //注冊用戶控件,加在代碼最前面 

<%@ Register TagPrefix="ucl" TagName="myUserControl" Src="myUserControl.ascx" %>  //注冊用戶控件,加在代碼最前面[cpp] view plaincopyprint?
<ucl:myUserControl id="myControl" runat="server" OnLoad="myControl_Load"></ucl:myUserControl>     //使用用戶控件,加在body裡面 

<ucl:myUserControl id="myControl" runat="server" OnLoad="myControl_Load"></ucl:myUserControl>     //使用用戶控件,加在body裡面5.Request.Cookies(讀)和 Response.Cookies(寫)


[csharp]

vNum = 1; 
strDate = "未訪問過本站!"; 
Response.Cookies["visit"]["vNum"] = vNum.ToString(); 
Response.Cookies["visit"]["myDate"] = strDate; 
Response.Cookies["visit"].Expires = DateTime.Now.AddDays(1); 

                    vNum = 1;
                    strDate = "未訪問過本站!";
                    Response.Cookies["visit"]["vNum"] = vNum.ToString();
                    Response.Cookies["visit"]["myDate"] = strDate;
                    Response.Cookies["visit"].Expires = DateTime.Now.AddDays(1);[csharp] view plaincopyprint?
vNum = Int32.Parse(Request.Cookies["visit"]["vNum"]) + 1; 
strDate = Request.Cookies["visit"]["myDate"].ToString(); 

                    vNum = Int32.Parse(Request.Cookies["visit"]["vNum"]) + 1;
                    strDate = Request.Cookies["visit"]["myDate"].ToString();6. Response.Redirect("Regist.aspx");//定位那個頁面

7.Session


[csharp]

Session["name"] = TextBox1.Text; //寫入信息 

Session["name"] = TextBox1.Text; //寫入信息[csharp] view plaincopyprint?
TextBox1.Text = Session["name"].ToString() //讀取信息 

TextBox1.Text = Session["name"].ToString() //讀取信息8.Application


[csharp]

Application.Lock(); 
Application["show"] =TextBox1.Text + "-----" + Session["name"].ToString() +"/n" + Application["show"];<SPAN style="WHITE-SPACE: pre">   </SPAN>//讀寫  
Application.UnLock(); 

Application.Lock();
Application["show"] =TextBox1.Text + "-----" + Session["name"].ToString() +"/n" + Application["show"]; //讀寫
Application.UnLock();9.數據庫連接


[csharp]

String strCon = "Data Source=WYZ-PC\\SQL2005;Integrated Security=True;Initial Catalog=mytest";  
         SqlConnection con = new SqlConnection(strCon); 
         con.Open();<SPAN style="WHITE-SPACE: pre"> </SPAN>//打開  
         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();<SPAN style="WHITE-SPACE: pre">  </SPAN>//命令執行 

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.數據庫插入、刪除、查詢、更新


[csharp]

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"); 
    } 

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基礎操作


[cpp]

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(); 
    } 

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();
    }
[html]

<?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> 

<?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>

 

 

 

 


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved