C#中级小常识

1、虚路径

(1)“~” ASP中的服务器端控件,仅能使用在服务器控件中,从当前应用根路径开始。

(2)“/” 表示网站根目录(域名);html

(3)“../”表示上一级目录;html

(4)“./”表示当前目录;html

 

程序中

 

 

/* 获取应用虚路径/* 获取虚路径 */
 string url = VirtualPathUtility.ToAbsolute("~/a.aspx");
 /* 将正斜杠 (/) 追加到虚拟路径的末尾(如果尚不存在正斜杠) */
string url2 = VirtualPathUtility.AppendTrailingSlash("~/b");

 

2、Server对象

 

Server是context的一个属性

获得方法:

 

 HttpServerUtility server = HttpContext.Current.Server;
 
 

 

 

 /*(1)防止跨站脚本攻击,处理的是html代码 */
 Server.HtmlEncode("");
 Server.HtmlDecode("");

HttpUtility.HtmlEncode("");//写法不同
HttpUtility.HtmlDecode("");

            
 /*(2)URL 转码 */
 Server.UrlEncode("");
 Server.UrlDecode("");


HttpUtility.UrlEncode("");//写法不同
HttpUtility.UrlDecode("");

 

 

 

3、请求转发和重定向

(1)Server.Transfer(path) //内部重定向请求,(请求转发)

(2)Server.Redirect(path)//请求重定向

 

 

PS: Java中

请求转发:response.getRequestDispatcher("xx.jsp").forword(request,response)

重定向:request.sendRedirect("xx.jsp")

 

4、HttpHandler

对请求的响应,可以输出普通的html内容,也可以输出图片、也可以输出一个文件(下载)

PS:一般非html请求使用HttpHandler做处理

举例用处:

(1)处理下载图片 context.Response.ContentType = "image/JPEG";

(2)处理下载excel context.Response.ContentType = " application/x-excel";

更多.Response.ContentType

参考 http://www.cnblogs.com/chenghm2003/archive/2008/10/19/1314703.html

 

 

5、WebForm基础之CodeBehind

前置显示aspx作为页面模板(页面的描述文件),后置代码cs作为页面动态处理程序。

这种前aspx后cs的方法称为CodeBehind

 

6、ASP.Net服务端控件 成员

(1)ClientID,控件在客户端的Id

<%=txt1.ClientID%>

 

(2)Visible

(3)CssClass

(4)Attributes

 

7、正则备忘

\d:代表一个数字,等同于[0-9]

\D:代表非数字,等同于[^0-9]

\s:代表换行符、Tab制表符等空白字符

\S:代表非空白字符

\w:匹配字母或数字或下划线或汉字。能组成单词

\W:非\w,等同于[^\w]

 

 

8、验证组件

(1)RequiredFieldValidator : 非空验证

设置 ControlToValidate="TextBox1" ErrorMessage="输入不得为空" 即可。

 

  if(this.IsValid){    //防止跳过客户端验证,IsValid保证客户端和服务端都验证过了。
                   Label1.Text = "与后台交互成功";
            }

 

 

 

(2)RangeValidator : 范围验证器

 

   <asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="时间格式不正确" 
            ForeColor="Red" ControlToValidate="tb_date" MaximumValue="2015-12-31" 
            MinimumValue="2000-01-01" Type="Date"></asp:RangeValidator>

 

(3)高级Validator

RegularExpressionValidator : 正则表达式验证

 

       <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
            ErrorMessage="邮箱错误" ControlToValidate="tb_email" ForeColor="Red" 
            ValidationExpression=".+@.+"></asp:RegularExpressionValidator>

 

 

 

9、数据绑定控件

复杂控件数据展示控件 : DataGrid、GridView、DetailsView、FormView、DataList (PS:从ASP.Net3.5起都不建议使用)

推荐使用ListView(复杂数据展示时使用)、Repeater(简单数据展示时使用)

 

ObjectDataSource : 用来将一个类作为数据源;

 

Repeater:基于模板型的数据绑定展示。

 

 <asp:Repeater ID="repeater2" runat="server" 
            onitemdatabound="repeater2_ItemDataBound">
       
        <ItemTemplate><!-- 展示模板-->
            <div class="imges">
            <asp:Image ID="Image1" runat="server" Height="150px" ImageUrl='<%#String.Format("~/img/{0}", Eval("address"))%>' />
            <%#Eval("name")%>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
           </div>        
        </ItemTemplate>

        </asp:Repeater>
    </div>

 

   
/* 
1、Repeater 获取行数据绑定信息
2、获取模板内服务器控件方法
 */
protected void repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Student student = (Student)e.Item.DataItem;//获得行绑定数据
            Label label1 = (Label)e.Item.FindControl("Label1");//模板中的服务端组件 
                                                               //要通过Item.FindControl(控件ID)方式获取
            label1.Text = Convert.ToString(student.Age);
        }

 

 

10、VS中配置错误页面

在web.config文件中

  <customErrors mode="On"
        defaultRedirect="~/error.aspx">
      <error statusCode="403" redirect="~/error.aspx" />
      <error statusCode="404" redirect="~/error.aspx" />
    </customErrors>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值