ASP.NET c#學習經驗

來源:互聯網
上載者:User

標籤:

1.DataGrid自訂欄位.
<Column
  <asp:BoundColumn DataField="khbh" HeaderText="客戶編碼"></asp:BoundColumn>
  <asp:BoundColumn DataField="khjc" HeaderText="客戶簡稱"><ItemStyle BackColor="#CCFFFF"></ItemStyle></asp:BoundColumn>
  <asp:BoundColumn DataField="dh" HeaderText="電話"></asp:BoundColumn>
  <asp:BoundColumn DataField="cjri" HeaderText="建立日期" DataFormatString="{0:yyyy-MM-dd}"></asp:BoundColumn>
  <asp:BoundColumn DataField="bycgl" HeaderText="月均出櫃量"></asp:BoundColumn>
  <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="編輯">
   <ItemStyle Font-Names="楷體_GB2312" Wrap="False" ForeColor="#339966"></ItemStyle>
   </asp:EditCommandColumn>
  <asp:ButtonColumn Text="刪除" CommandName="Delete"><ItemStyle Font-Names="楷體_GB2312" Wrap="False" ForeColor="#339966"></ItemStyle>
   </asp:ButtonColumn>
</Columns>
(DataGrid1.AutoGenerateColumns=False)

2.按一個控制項後出一提示框選擇是否執行.
private void Page_Load(object sender, System.EventArgs e)
{
   // 在此處放置使用者代碼以初始化頁面
   Button1.Attributes["onClick"]="javascript:return confirm(‘確定嗎?‘);";

}

3.一般提示框.
private void Button1_Click(object sender, System.EventArgs e)
{
    Response.Write("<script language=‘javascript‘>alert(‘hellow‘);</script>");
}

4.一般的DataGrid顯視資料過程:
SqlConnection SqlConn;
DataSet objDataSet=new DataSet();
string Connstr="server=bserver;user=sa;database=SFA";
string SQLstr="select * from khxx";
SqlConn=new SqlConnection(Connstr);
SqlDataAdapter objAdapter=new SqlDataAdapter(SQLstr,SqlConn);
objAdapter.Fill(objDataSet,"khxx");
DataGrid1.DataSource=objDataSet.Tables["khxx"].DefaultView;
DataGrid1.DataBind();

5.在Web.config裡設好串連資料庫的字串.
//web.config
</system.web>
  <appSettings>
  <add key="strConn" value="server=bserver;database=SFA;user=sa;"/>
  </appSettings>
</configuration>
//WebForm1.aspx
using System.Configuration;
string Connstr=ConfigurationSettings.AppSettings["strConn"];

6.分頁顯視資料:
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
    DataGrid1.CurrentPageIndex=e.NewPageIndex;
    BindGrid();
}

7.統一顯視資料函數:
public void BindGrid()
{
    SqlConnection SqlConn;
    DataSet objDataSet=new DataSet();
    string Connstr=ConfigurationSettings.AppSettings["strConn"];
    string SQLstr="select * from khxx";
    SqlConn=new SqlConnection(Connstr);
    SqlDataAdapter objAdapter=new SqlDataAdapter(SQLstr,SqlConn);
    objAdapter.Fill(objDataSet,"khxx");
    DataGrid1.DataSource=objDataSet.Tables["khxx"].DefaultView;
    DataGrid1.DataBind();
}

回複:
8.DataGrid資料編輯處理(一)_直接編輯
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
   DataGrid1.EditItemIndex=(int)e.Item.ItemIndex;
   BindGrid();
}

private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
   DataGrid1.EditItemIndex=-1;
   BindGrid();
}

private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
  DataGrid1.EditItemIndex=-1;    
           
  SqlConnection SqlConn;
  DataSet objDataSet=new DataSet();
  string Connstr=ConfigurationSettings.AppSettings["strConn"];
  string SQLstr="select * from khxx";
  SqlConn=new SqlConnection(Connstr);
  SqlDataAdapter objAdapter=new SqlDataAdapter(SQLstr,SqlConn);
  objAdapter.Fill(objDataSet,"khxx");

  TextBox CurrentText;
  CurrentText = (TextBox)e.Item.Cells[1].Controls[0];//取得文字框
  objDataSet.Tables["khxx"].Rows[(int)e.Item.ItemIndex]["khjc"]=CurrentText.Text;

  SqlCommandBuilder cb=new SqlCommandBuilder(objAdapter);
  SqlConn.Open();
  objAdapter.Update(objDataSet,"khxx");
  SqlConn.Close();

  DataGrid1.DataSource=objDataSet.Tables["khxx"];
  DataGrid1.DataBind();

  }
  在PageLoad()裡一定要if(Page.IsPostBack){....};
  DataGrid1.DataKeyField="khbh";

8.排序:
private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
   DataGrid1.EditItemIndex=-1;
   SqlConnection SqlConn;
   DataSet objDataSet=new DataSet();
   string Connstr=ConfigurationSettings.AppSettings["strConn"];
   string SQLstr="select * from khxx";
   SqlConn=new SqlConnection(Connstr);
   SqlDataAdapter objAdapter=new SqlDataAdapter(SQLstr,SqlConn);
   objAdapter.Fill(objDataSet,"khxx");

   DataTable objDataTable=objDataSet.Tables["khxx"];
   DataView objDataView=new DataView(objDataTable);
   objDataView.Sort=e.Sort;

   DataGrid1.DataSource=objDataView;
   DataGrid1.DataBind();
}
DataGrid1.AllowSorting=True;
<asp:BoundColumn DataField="khbh" ReadOnly="True" HeaderText="客戶編碼" Sort="khbh">
<ItemStyle Wrap="False"></ItemStyle></asp:BoundColumn>

9.刪除一條記錄:
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
   SqlConnection SqlConn;
   DataSet objDataSet=new DataSet();
   string Connstr=ConfigurationSettings.AppSettings["strConn"];
   SqlConn=new SqlConnection(Connstr);

   string bh=DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
   string SQLstr="delete from khxx where khbh=‘"+bh+"‘";
    //int ProductID =(int)MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
    //string SQLStatement="Delete Products Where ProductID="+ProductID;(當欄位為整數時)
           
   SqlCommand myCommand = new SqlCommand(SQLstr,SqlConn);

   myCommand.Connection.Open();
   myCommand.ExecuteNonQuery();
   myCommand.Connection.Close();

   BindGrid();
}

10.刪除時提示是否刪除:
<HEAD>
   .....    
   <script language="javascript">
   function delete_confirm(e) {
      if (event.srcElement.outerText=="刪除")
      event.returnValue=confirm("確認刪除否?");
          }
      document.onclick=delete_confirm;
   </script>
</HEAD>

11.在DataGrid裡加複選框.
<Columns>
   <asp:TemplateColumn HeaderText="" ItemStyle-HorizontalAlign="Center">
       <ItemTemplate>
           <asp:CheckBox runat="server" ID="del" />
       </ItemTemplate>
   </asp:TemplateColumn>
    ......
</Columns>

12.選中刪除多條記錄:
private void Button1_Click(object sender, System.EventArgs e)
{
   SqlConnection SqlConn;
   string Connstr=ConfigurationSettings.AppSettings["strConn"];
   SqlConn=new SqlConnection(Connstr);

   string DelString,SIDString="";
   for(int i=0;i<DataGrid1.Items.Count;i++)
   {
       CheckBox h;
       h=(CheckBox)DataGrid1.Items.Cells[0].Controls[1];
       if(h.Checked == true)
       { //取得已選取的主鍵
           SIDString +=" or (khbh =‘" + DataGrid1.Items.Cells[1].Text + "‘)";
       }
   }
   if(SIDString.Length>0)
   {
       SIDString=SIDString.Remove(0,4);
       DelString = "Delete from khxx where " + SIDString;
       SqlCommand DelRec = new SqlCommand(DelString,SqlConn);
       DelRec.Connection.Open();
       DelRec.ExecuteNonQuery();
       DelRec.Connection.Close();
       BindGrid();
   }
}

13.不同WEB頁傳遞參數(前一個向後一個傳):
(WebForm1.aspx)
private void Button2_Click(object sender, System.EventArgs e)
{
   Response.Redirect("[email protected]="+TextBox1.Text+"&NAME="+TextBox2.Text);
}
(WebForm2.aspx)
private void Page_Load(object sender, System.EventArgs e)
{
   string s;
   s=Request.QueryString["ID"];
   Label1.Text=s;
   s=Request.QueryString["NAME"];
   Label2.Text=s;        
}


回複:
14.增加一條記錄(一):
    SqlConnection SqlConn;
    DataSet objDataSet=new DataSet();
   string Connstr=ConfigurationSettings.AppSettings["strConn"];
   string SQLstr="select * from khxx";
   SqlConn=new SqlConnection(Connstr);
   SqlDataAdapter objAdapter=new SqlDataAdapter(SQLstr,SqlConn);
   objAdapter.Fill(objDataSet,"khxx");

   DataRow row=objDataSet.Tables[0].NewRow();
   row["khbh"]=TextBox3.Text;
   row["khjc"]=TextBox4.Text;
   row["dh"]=TextBox5.Text;
   row["cjri"]=TextBox6.Text;
   row["bycgl"]=TextBox7.Text;

   objDataSet.Tables[0].Rows.Add(row);
   SqlCommandBuilder cb=new SqlCommandBuilder(objAdapter);
   objAdapter.Update(objDataSet,objDataSet.Tables[0].ToString());
   BindGrid();
15.代碼色之用:
  LinkButton1.ForeColor=ColorTranslator.FromHtml("#FF8000");

16.關於載入執行:
protected void Page_Load(Object sender, EventArgs e)
{
// 網頁每次載入時,執行的一些操作
  if (!IsPostBack)
  {
    // 網頁第一次載入時執行的操作
  }
  else
  {
    // 回送時執行的操作
  }

    // 網頁每次載入時執行的操作
}

17.開啟網頁時全屏顯視:
  (另加default.aspx,加入:)
<script language="javascript">
window.open(‘loadfile.aspx.html‘,‘_blank‘,‘menubar=no,location=no,toolbar=no,
scrollbars=no,status=no,top=0,left=0,width=‘ + screen.width + ‘,height=‘ + screen.height);
window.opener = null;
window.close();
</script>

18.開啟新的瀏覽器視窗:
Response.Write("<SCRIPT language=‘javascript‘> window.open(‘YOURURL‘)</SCRIPT>");

19.網頁啟動的時候彈出一個小表單:
  在Page_Lod事件中加入:
  Response.Write("<script>window.open(‘newyear.htm‘,‘_blank‘,‘toolbar=no,location=no,directories=no,status=no,
  menubar=no,scrollbars=no,revisable=no,left=100,top=0,width=600,height=50‘)</" + "script>");
  彈出檔案名稱:newyear.htm

20.Session的用法:
//傳遞參數
Session["markid"] = webform1.Text1.Value;
Application["markid"] = webform1.Text1.Value;
WebForm2.Text1.Value = Session["markid"].ToString();
WebForm2.Text1.Value = Application["markid"].ToString();

//清除
Session.Remove("markid"); (session可以定義在webform1的任何地方。)

21.html控制項:
  button游標移上移開的不同效果:
<INPUT id="Button3" style="Z-INDEX: 111; LEFT: 248px; POSITION: absolute;
TOP: 456px" onmouseover="this.style.color=‘red‘;"
onmouseout="this.style.color=‘black‘;" type="button"
value="text" name="Button3" runat="server">

22.cookie的用法:
HttpCookie cookie = new HttpCookie("aspcn");
cookie.Values.Add("webmaster","飛刀");
cookie.Values.Add("writer","beige");
cookie.Values.Add("LinkColor","blue");
Response.AppendCookie(cookie);
取出資訊也一樣簡單
HttpCookie cookie = Request.Cookies["aspcn"];
value1 = cookies.Values["webmaster"];
value2 = cookies.Values["writer"];

23.ASP.NET中文顯示之種幾種解決方案:
(1)configuration>
    <globalization
    requestencoding="utf-8"
    responseencoding="utf-8"
    />
  </configuration> 或"gb2312" 或"big5"
(2)添加<%@ CODEPAGE = "936" %>到每一頁的開頭;

24.線上使用者統計:
private void Page_Load(object sender, System.EventArgs e)
{   
  Visitors.Text = Application["user_sessions"].ToString();
}
global.asax檔案:
protected void Application_Start(Object sender, EventArgs e)
{
  Application["user_sessions"] = 0;
}
protected void Session_Start(Object sender, EventArgs e)
{
  Application.Lock();
  Application["user_sessions"] = (int)Application["user_sessions"] + 1;
  Application.Unlock();
}
protected void Session_End(Object sender, EventArgs e)
{
  Application.Lock();
  Application["user_sessions"] = (int)Application["user_sessions"] - 1;
  Application.Unlock();
}

25.DataSet的一種遍曆修改方法:
SqlDataAdapter myCmd=new SqlDataAdapter(strSql,myConn);
DataSet ds=new DataSet();
myCmd.Fill(ds,"操作員");
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
  if(ds.Tables[0].Rows["Oper_state"].ToString()=="1")
  ds.Tables[0].Rows["Oper_state"]="有效";
  else if(ds.Tables[0].Rows["Oper_state"].ToString()=="0")
  ds.Tables[0].Rows["Oper_state"]="凍結";
}

26.自動重新整理:
<head>
<!--每10秒自動重新整理-->
<meta http-equiv="refresh" content="10">
</head>

27.ViewState的讀取:
// 儲存在 ViewState 中
ViewState["SortOrder"] = "DESC";
// 從 ViewState 中讀取
string sortOrder = (string)ViewState["SortOrder"];

28.用Session傳遞DataSet:
DataTable Dt=new DataTable();
Dt=yourDataSet.Tables[yourtable].DefaultView;
DataGrid1.DataSource=Dt;//yourDataSet是一個DataSet對象
DataGrid1.DataBind();//假如你的“當前頁”的DataGrid是這樣邦定的
Session["MyTable"]=Dt;
另一頁(列印頁):
DataTable Dt2=new DataTable();
Dt2=(DataTable)Session["MyTable"];
DataGrid2.DataSource=Dt2;
DataGrid2.DataBind();

29.超連結傳遞中文參數的問題:"list.aspx?name=" + Server.UrlEncode("中國");

30.Calendar的日期取出:
private void Calendar1_SelectionChanged(object sender, System.EventArgs e)
{
   TextBox1.Text=Calendar1.SelectedDate.ToShortDateString();
}
private void Calendar1_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
{
    ......        
}

31.用DataTable在記憶體建表和加入資料方式一:
    DataTable dt=new DataTable();
   dt.Columns.Add(new DataColumn("編號",typeof(Int32)));
   dt.Columns.Add(new DataColumn("客戶",typeof(string)));
   DataRow dr;
   dr=dt.NewRow();
   dr[0]=9;
   dr[1]="custom";
   dt.Rows.Add(dr);
   DataGrid1.DataSource=new DataView(dt);

32.Hashtable表的用法:
Hashtable h = new Hashtable();
h.Add ("鍵 1", "值 1");
h.Add ("鍵 2", "值 2");
h.Add ("鍵 3", "值 3");
MyDataList.DataSource = h;

33.通常的DataList模板應用:
<ItemTemplate>
    編號:<%# DataBinder.Eval(Container.DataItem,"ID","{0:N2}") %><br>
   項:<%# DataBinder.Eval(Container.DataItem,"STRING") %><br>
   日期:<%# DataBinder.Eval(Container.DataItem,"DATETIME","{0:d}") %><br>
   是否:
   <asp:CheckBox ID="checkbox1" Checked=‘<%# DataBinder.Eval(Container.DataItem,"BOOL") %>‘ Runat=server /><br>
</ItemTemplate>

34.使用 SqlDataReader的一般方法:
SqlConnection myConnection = new SqlConnection(".......");
SqlCommand myCommand = new SqlCommand("select * from Authors", myConnection);
myConnection.Open();
SqlDataReader dr = myCommand.ExecuteReader();
MyDataGrid.DataSource = dr;
MyDataGrid.DataBind();
myConnection.Close();
35 一些程式短語:
string fdwmc=TextBox1.text.trim();

ArrayList value=new ArrayList();
value.Add("aaa");
value.Add("bbb");

Hashtable h=new Hashtable();
h.Add("a1","a2");
h.Add("b1","b2");

StringBuilder s3 = new StringBuilder();
s3.Append("hello");
s3.Append(" world");
s3.Append(" !!!");

36.HTML短語:
  <p> 則是代表段落標註
  <br>斷行標註,,這個標註不需要再寫一個對映的 </br>
  <font>標註:本標註用來設定文字的大小、顏色、字型
  <b> 粗體、<i> 斜體及 <u> 底線標註
  <h1>~<h6> 標題標註
  <blockquote>縮排標示
  <ol> 條列標註:(顯數字)
  <ul>條列標註:(顯。)
  <div> 段落對齊標註
  <!-- 這是批註, 給開發人員看的, 不會被解譯 -->
  <table> 標註用來表示表格的開始及結束
  <tr> 則表示其中行的開始及結束
  <td> 則表示一行中的欄位
  <img src="Train.jpg">顯視圖形
  <a href="H10.htm>...</a><br>超級聯結

L來至 http://www.360doc.com/userhome/15774578 收藏

ASP.NET c#學習經驗

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.