未分类

《C#Web应用程序入门经典》学习笔记

最近看了《Beginning C# Web Applications Wtith Visual Studio .Net 》。感觉这本书在一些细节方面写的不错,特做笔记,为后来者提供一些或许有用的东东。今天先写出来一些,年前正确整理完。

当前日期:

Lbll.Text = DateTime.Now.ToLongDataString();
This.controls.Add(lbl);

URL:

HyperLink reg = new HyperLink();
Reg.Text = “Register;
Reg.NavigateUrl = Context.Request.ApplicationPath + “Myfirst.aspx”;

判断用户授权:

Context.User.Identity.IsAuthenticated;

表格相关:

1. 新建一图片img
2. img添加到cell
3. cell添加到row
4. row添加到Table
5. Table添加到PlaceHolder

Table tb = new Table();
TableRow row = new TableRow();
Image img = new Image();
img.ImageUrl = “Images/winbook.gif”;
img.ImageAlign = ImageAlign.Middle;
img.Width = new Unit(24, UnitType.Pixel);
img.Height = new Unit(24, UnitType.Pixel);
cell = new TableCell();
cell.Controls.Add(img);
row.Cells.Add(cell);

HyperLink lnk = new HyperLink();
lnk.Text = “News”;
lnk.NavigateUrl = “News.aspx”;

row.Cells.Add(cell);
tb.Rows.Add(row);
phNav.Controls.Add(tb);

将已验证身份的用户重定向回最初请求的URL

public static void RedirectFromLoginPage(string userName,bool createPersistentCookie);

参数
userName
用于 Cookie 身份验证的用户名称。这不需要映射到帐户名称,并将由 URL 身份验证使用。
createPersistentCookie
指定是否应当发出持久性 Cookie(跨浏览器会话保存的 Cookie)。

标准数据库操作1

String sql;
SqlCommand cmd;
SqlConnection conn;
Sql = “insert into …”;
conn = new SqlConnection (“data source = (local); initial catalog = caoxicao;userid = sa”);
cmd = new SqlCommand (sql,conn);
conn.open();
cmd.ExecuteNonQuery();

标准数据库操作2

SqlConnection conn;
SqlCommand cmd;
SqlDataReader reader;
string sql;
sql = “select * from TableName”;
conn = new SqlConnection (“data source = (local); initial catalog = caoxicao;userid = sa”)
cmd = new SqlCommand(sql,conn);
conn.open();
reader = cmd.ExecuteReader();

可以用reader的Read()方法判断是否真的返回了值

If (reader.Read())
…{
This.Email.Text = reader[“Email”].ToString();
}

DataSet 基本操作
DataSet dsCaoxicao;
String sql;
SqlConnection conn;
SqlDataAdapter adPlaces;
conn = new SqlConnection (“data source = (local); initial catalog = caoxicao;userid = sa”)
adPlaces = new SqlDataAdapter(sql,conn);
dsCaoxiCao = new DataSet();
conn.Open();
adPlaces.Fill(dsCaoxiCao,”Places”);

读取Web.config中设置
Conn = new SqlConnection(ConfigurationSettings.AppSettings[“cnFriends.ConnectString”]);

<!– User application and configured property settings go here.–>
<!– Example: <add key=”settingName” value=”settingValue”/> –>

几个命名空间
当用到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>、</span>
<div>、和</div>
返回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设置

分类: 未分类

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部