C#Web应用程序入门经典 学习笔记之二

本文档介绍了C# Web应用程序中的关键配置方法,包括如何读取Web.config文件中的设置、使用不同命名空间来实现特定功能、通过Session保存数据、从DataTable中筛选行以及创建用户控件等内容。
读取 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”; />
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值