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

所属分类: 网络编程 / ASP.NET 阅读数: 1202
收藏 0 赞 0 分享
读取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”; />

更多精彩内容其他人还在看

解析WPF实现音频文件循环顺序播放的解决方法

本篇文章是对WPF实现音频文件循环顺序播放的方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

解决.net framework 4.0环境下遇到版本不同编译不通过的方法详解

本篇文章是对.net framework 4.0环境下遇到版本不同编译不通过的解决方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

将文件上传、下载(以二进制流保存到数据库)实现代码

将文件以二进制流的格式写入数据库:首先获得文件路径,然后将文件以二进制读出保存在一个二进制数组中具体请祥看本文,希望对你有所帮助
收藏 0 赞 0 分享

点击提交按钮后DropDownList的值变为默认值实现分析

在点击提交按钮后,页面上所有的绑定到数据库的控件值都恢复到默认值,下面与大家分享下DropDownList的值变为默认值
收藏 0 赞 0 分享

ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法

ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

Linkbutton控件在项目中的简单应用

Button控件可分为button控件、LinkButton控件、ImageButton控件三类,而LinkButton控件则在页面上显示为一个超级链接,下面与大家分享下其具体应用
收藏 0 赞 0 分享

Web.config 和 App.config 的区别分析

Web.config 和 App.config 的区别分析,需要的朋友可以参考一下
收藏 0 赞 0 分享

基于.Net中的数字与日期格式化规则助记词的使用详解

本篇文章是对.Net中的数字与日期格式化规则助记词的使用进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

解决在Web.config或App.config中添加自定义配置的方法详解

本篇文章是对在Web.config或App.config中添加自定义配置的方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

深入本机影像生成器(Ngen.exe)工具使用方法详解

本篇文章是对本机影像生成器(Ngen.exe)工具使用方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享
查看更多