1、(dropdownlist.Items.FindByValue("1")).Selected = true ;//設為預設值
2、javascript母片擷取id
var obj1 = document.getElementById("<%=TextBox1.ClientID%>");
3、 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//
//string ID= GridView1.Rows[e.NewEditIndex].Cells[0].Text; //GridView1_RowEditing中取某列的值,
string ID = GridView1.DataKeys[e.NewEditIndex].Value.ToString(); //取主鍵的值
}
4、
//動態加控制項
protected void Page_Load(object sender, EventArgs e)
{
//添加
TextBox tb =new TextBox();
tb.ID = "aa";
Panel1.Controls.Add(tb);
}
protected void Button1_Click(object sender, EventArgs e)
{
//取值
//母片
ContentPlaceHolder cph = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder3");
Panel pan = (Panel)cph.FindControl("Panel1");
TextBox t1=(TextBox) pan.FindControl("aa");
Response.Write(t1.Text);
}
5、在類中Server.MapPath如何使用?
System.Web.HttpContext.Current.Server.MapPath(".")
6、當頁面PostBacks的時候,保持捲軸的位置。
<%@ Page Language="C#" MaintainScrollPositionOnPostback="true" AutoEventWireup="true" CodeFile="" Inherits="" %>
7、當頁面載入的時候,控制項獲得預設焦點
<form id="frm" DefaultFocus="txtUserName" runat="server">
8、當使用者按下Enter鍵的時候,設定預設觸發按鈕。
<form id="frm" DefaultButton="btnSubmit" runat="server">
9、 //擷取當前頁傳輸控制項的頁
if (Page.PreviousPage != null)
{
string a = (TextBox)Page.PreviousPage.FindControl("TextBox!").text;
}
10、加在<head>外 <!-- #Include File="border.html" -->
兩個頁中的id不能重複等
11、
<asp:Label ID="Label2" runat="server" ie:Text="IE 瀏覽器" mozilla:Text="Mozilla 或 Firefor 瀏覽器" Style="position: relative"></asp:Label><br />
12. //Url傳遞中文參數解決方案
//解碼html
// Response.Write(Server.HtmlDecode("aaaaaaaaaaaaaaaaaaaaa<font >ass ssssss</font>"));
// <%# Server.HtmlEncode((string)DataBinder.Eval(Container.DataItem,"內容")) %>
//Url傳遞中文參數解決方案
a href="Admin_SmallSort.aspx?iProductBigSort_ID=<%# Eval("iProductBigSort_ID")%>&sProductBigSort=<%# Server.UrlEncode(Eval("sProductBigSort").ToString())%>">
13. //擷取上一個頁面控制項的值
if (Page.PreviousPage!=null)
{
TextBox a = (TextBox)Page.PreviousPage.FindControl("TextBox1");
a.Text;
}
14。 使用 foreach 顯示整數數組的內容
int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 };
foreach (int i in fibarray)
{
System.Console.WriteLine(i);
}
15. 執行break語句,跳出swith語句。如果沒有break語句,所以的語句都會執行。
string sWork = "a|b|c";
string[] Gz=sWork.Split(new char[] {'|'});
foreach (string s in Gz)
{
System.Console.WriteLine(s);
}
只能在 while、do...while、for 或 for...in 迴圈內使用 continue 語句。執行 continue 語句會停止當前迴圈的迭代,並從迴圈的開始處繼續程式流。這將對不同類型的迴圈有如下影響:
16.
//比較日期大小
DateTime t1 = DateTime.Now; //
DateTime t2 = Convert.ToDateTime("9:10:59");
int a = DateTime.Compare(t1, t2); //a=1,t1大
//從此執行個體中減去指定的日期和時間Subtract
DateTime t1 = System.DateTime.Now; //系統時間
DateTime t2 = Convert.ToDateTime(Row["dtime"].ToString()); //最後線上時間
TimeSpan d3= t2.Subtract(t1);
int a = d3.Minutes; //t2與t1的差幾分鐘 擷取由當前 TimeSpan 結構表示的整分鐘數。
17.//DropDownList1 添加資料
DropDownList1.Items.Add(new ListItem("02", "02"));
DropDownList1.Items.Add(new ListItem( string Text,string Value))
18. 先提示後跳轉
Response.Write("<script>alert('aa');window.location=right1.aspx</script>")
// Response.Redirect("~/Login.aspx",true); true -不執行後面的內容(但alert不行) ,false則執行,
19.
一個頁面,該頁面上兩個按鈕和一個容器控制項(Panel),一按鈕為產生控制項,一按鈕名為取值
一個使用者控制項,該控制項上一個TextBox控制項
-------
點產生按鈕,產生十個使用者控制項,
點取值按鈕,取剛產生的這十個使用者控制項中,TextBox的值
其中,產生十個使用者控制項代碼,
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 9; i++)
{
Control c = Page.LoadControl("WebUserControl.ascx");
this.Panel1.Controls.Add(c);
}
}
取值時,不會了
protected void Button2_Click(object sender, EventArgs e)
{
//取剛產生的這十個使用者控制項中的,TextBox的值??????
}
1.foreach 迴圈遍曆表單所有控制項
2.FindControl("");
20、設定了50M ~~最大上傳量,90秒上傳時間
<system.web>
<httpRuntime maxRequestLength="50000" useFullyQualifiedRedirectUrl="false"
executionTimeout="90"
/>
<compilation debug="true"/>
21 ‘在綁定資料時經常會用到這個句程式:<%# DataBinder.Eval(Container.DataItem,"xxxx")%>或者<%# DataBinder.Eval(Container,"DataItem.xxxx")%>
今天又學到一種,而且微軟也說這種方法的效率要比以上兩種高。
<%# ((DataRowView)Container.DataItem)["xxxx"]%>
很有用的,這樣可以在前台頁面做好多事情了。
還要記住要這樣用必須要在前台頁面匯入名稱空間System.Data,否則會建置錯誤資訊。
<%@ Import namespace="System.Data" %>
這種用法其實和<%# ((DictionaryEntry)Container.DataItem).Key%>是一個道理。
22 //比較兩個字串是否相等,true表示不區分大小寫
int i = string.Compare("aaaaaaa", "aaaaaaa", true);
if (i == 0)
{
Response.Write("相等");
}
if (i==1)
{
Response.Write("不相等");
}
23、字串加密
public string EncryptPassword(string PasswordString,string PasswordFormat )
{
if (PasswordFormat="SHA1")
{
EncryptPassword=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"SHA1");
}
else if (PasswordFormat="MD5")
{
EncryptPassword=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"MD5");
}
else
{
}
}
public string md5(string str, int code)
{
if (code == 16) //16位MD5加密(取32位加密的9~25字元) SHA1還可以換成MD5
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1").ToLower().Substring(8, 16);
// md5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1").ToLower().Substring(8, 16);
}
if (code == 32) //32位加密
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1").ToLower();
// md5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1").ToLower();
}
//必須每種情況都要有傳回值。如果code<>16,32 則返回的值
return "a";
}
24 // String.Format()可以替換相關項目,代碼如下,其中{}為此方法認別的替換符:
//單個字元替換如下:
String RepString = String.Format("Format 是C#String類常用功能之一,{0}","謝謝觀賞!");
//RepString 的值為:Format 是C#String類常用功能之一,謝謝觀賞!
//兩個或者兩個以上
String RepString = String.Format("String.Format(),{2},{1},{0}","謝謝觀賞!","我們在學習和工作中要靈活運用","是C#常用方法之一");
//RepString的值為:String.Format(),是C#常用方法之一,我們在學習和工作中要靈活運用,謝謝觀賞!
//總結:前面有幾個{},後面就跟幾個參數。以此類推,後面的參數可以是無限。
25 //判斷字串是否為空白String.IsNullOrEmpty(str) == True 則說明字串為空白(null 和 "");
//string.IsNullOrEmpty("");
TextBox1.Text = "aaa";
if (TextBox1.Text == string.Empty) // TextBox1.Text=null 或 ="" 都為true
{
Response.Write("空");
}
else
{
Response.Write("非空");
}
26 //指定索引位置插入一個指定的 String 執行個體
string a = "aaa";
string b=a.Insert(1, "b");
Response.Write(b); //abaa