Conn = new SqlConnection(ConfigurationSettings.AppSettings[“cnFriends.ConnectString”]);



<appSettings>

<!-- User application and configured property settings " /> 3atv一区二区三区,成人免费a**址,亚洲综合专区

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

C#Web應用程序入門經典學習筆記之二

讀取Web.config中設置

   Conn = new SqlConnection(ConfigurationSettings.AppSettings[“cnFriends.ConnectString”]);

 

  <appSettings>

    <!--   User application and configured property settings go here.-->

    <!--   Example: <add key="settingName" value="settingValue"/> -->

<add key="cnFriends.ConnectionString" value="data source=(local)/NETSdk;initial catalog=FriendsData;user id=sa" />  </appSettings>

 


幾個命名空間

當用到DataSet時,用using system.Data.SqlClient

當配置Web.config時,用using system.Configuration


這個讓我想起了大一學習C語言時

bool visible

btnSearch.Text = visible? “New Search” : “Search” ;


這個也蠻好

dsResult.tables[“Users”].rows.count

Conver.Tonint32(ConfigurationSettings.AppSettings[“Cokuale.number”]);


夠狠1:用Session保存結果并綁定

Session[“Search”] = dsResults;

dsResults = (DataSet) Session[“Search”];

grdResults.DataBind();
其實,Session,Application等存的是object 類型,因此,最后都要顯式轉換類型
順便說說,判斷是否取到字符串類型的值用null 判斷。

夠狠2:從DataTable中選擇行

DataRow[] rows = dsResults.Tables[“Users”].Select(filter);

dsResults = dsResults.Clone();

foreach(DataRow row in rows)

{

         dsResults.Tables[“Tables”].ImportRow(row);

}


獲取webForm 上的一個控件

ImageButton img = (ImageButton)e.Item.FindControl(“Selectbutton”)


跳轉:

Server.Transfer(“Caoxicao.ASPx”);


服務器控件添加js腳本(Attributes屬性)

imgShow.Attributes.Add(“onclick”,”document.getElementById(‘tbPrefs').style.display = ‘block';”);
再(Style屬性),

img.Style.Add(“Cursor”,'Pointer');


Color相關:

ColorConvert cv = new ColorConvert();

Color selected = Color.Empty;

Selected = (olor)cv.ConvertFromString(White);


增加Cookie

Response.Cookies.Add(new HttpCookie(“backColor”,r))


我的最愛----用戶控件

Using FriendsReunion.Controls;

Protectd override void Oninit(EventArgs e)

{

         FriendsFooter _footer = (FriendsFooter)LoadControl(Request.ApplicationPath+”/Controls/ FriendsFooter.ASPx”);

         SubHeader _subHeader = new SubHeader();

}

Page.Contros.AddAt(0,_footer);

Page.Contros.AddAt(0,_subHeader);

base.OnInit(e);

}


新建Html控件實例

HtmlGenericControl div = new HtmlGenericControl(“div”);

div.Style.Add(“background-color”,bg);

使用該類可以表示不直接用 .NET Framework 類表示的 HTML 服務器控件標記,如 <span>、<div>、<body> 和 <font>


返回DataSet

Public DataSet Contact()

{

         String sql = “@ Select * from … …”;

         DataSet requests = new DataSet();

         New SqlDtaAdapter (sql,conn).Fill(requests);

         //return requests.GetXml();
                   Return requests;

}

接收:(當返回值是Xml格式的數據集時)

DataSet results = new DataSet();

Results.ReadXml(new StringReader(fi.ContactRequest(userid)));


用到WebService時,只需在方法上添加[WebMethod]特性即可!

如果添加緩存,則[WebMethod(CacheDurition=600)]

實例化WebService

FriendsService.FriendsInfo fi = new FriendsService.FriendsInfo();

String userid;

Userid = fi.GetUserID(“…”);


小Tips!

HyperLink reg = new HyperLink();

Reg.ToolTip = “… …”;


簽出:

System.Web.Security.Forms.Authentication.SignOut();

Response.write (Request.ApplicaltionPath);


跟蹤調試:

Trace.Write

Trace.Warn


異常:

1.   拋出異常

         程序異常拋出

         Throw new ***Exception(“…”);

2.   捕獲異常

         必須開始時從一個try代碼塊拋出,try代碼塊用來放置所有可能拋出異常的代碼。

Eg:

         Try

                  {

                            … …

                  }

         Catch(ArgumentNullExeption e)

                  {

                            …

                  }

 

未處理異常web.config設置

<Custom Errors mode = “on” defaultRedriect = “customerror.ASPx”; />

AspNet技術C#Web應用程序入門經典學習筆記之二,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 保德县| 云龙县| 榕江县| 盖州市| 将乐县| 临江市| 油尖旺区| 邵阳市| 蒙山县| 沛县| 蒲城县| 盘锦市| 岑巩县| 南投县| 黎川县| 满城县| 东明县| 民和| 翁牛特旗| 昭苏县| 呼玛县| 锦州市| 乌兰察布市| 襄垣县| 南昌市| 福鼎市| 论坛| 怀柔区| 四会市| 武宁县| 山丹县| 石阡县| 湖北省| 汾阳市| 许昌市| 湘潭县| 慈利县| 湛江市| 古蔺县| 且末县| 南皮县|