8月3日学习内容

1:关于TryParse的用法

TryParse的用法让我头痛了哈,上课没认真啊!

用法实例:

private void button1_Click(object sender, EventArgs e)
         {
             int nA =0;    //你要得到的转换后的数字变量,默认为0 ,因为你不赋值,转换失败还是给你个数字0
             bool result; //监视转换是否成功的变量
             result = int.TryParse(textBox1.Text, out nA);
             MessageBox.Show(nA.ToString());
             MessageBox.Show(result.ToString());
         }:

2:HttpUtility.UrlDecode Server.UrlDecode 区别

在对URL进行编码时,该用哪一个?这两都使用上有什么区别吗?
测试:
string file="文件上(传)篇.doc";
string Server_UrlEncode=Server.UrlEncode(file);
string Server_UrlDecode=Server.UrlDecode(Server_UrlEncode);
string HttpUtility_UrlEncode=System.Web.HttpUtility.UrlEncode(file);
string HttpUtility_UrlDecode=System.Web.HttpUtility.UrlDecode(HttpUtility_UrlEncode);
Response.Write("原数据:"+file);
SFun.WriteLine("Server.UrlEncode:"+Server_UrlEncode);
SFun.WriteLine("Server.UrlDecode:"+Server_UrlDecode);
SFun.WriteLine("HttpUtility.UrlEncode:"+HttpUtility_UrlEncode);
SFun.WriteLine("HttpUtility.UrlDecode:"+HttpUtility_UrlDecode);

输出:
原数据:文件上(传)篇.doc
Server.UrlEncode:%ce%c4%bc%fe%c9%cf%a3%a8%b4%ab%a3%a9%c6%aa.doc
Server.UrlDecode:文件上(传)篇.doc
HttpUtility.UrlEncode:%e6%96%87%e4%bb%b6%e4%b8%8a%ef%bc%88%e4%bc%a0%ef%bc%89%e7%af%87.doc
HttpUtility.UrlDecode:文件上(传)篇.doc

区别在于:HttpUtility.UrlEncode()默认是以UTF8对URL进行编码,而Server.UrlEncode()则以默认的编码对URL进行编码。

在用 ASP.Net 开发页面的时候, 我们常常通过 System.Web.HttpUtility.UrlEncode 和 UrlDecode 在页面间通过 URL 传递参数. 成对的使用 Encode 和 Decode 是没有问题的.

但是, 我们在编写文件下载的页面的时候, 常常用如下方法来指定下载的文件的名称:
Response.AddHeader("Content-Disposition","attachment; filename="
+ HttpUtility.UrlEncode(fileName, Encoding.UTF8));
之所以转换成 UTF8 是为了支持中文文件名.

这 时候问题就来了, 因为 HttpUtility.UrlEncode 在 Encode 的时候, 将空格转换成加号('+'), 在 Decode 的时候将加号转为空格, 但是浏览器是不能理解加号为空格的, 所以如果文件名包含了空格, 在浏览器下载得到的文件, 空格就变成了加号.

一个解决办法是, 在 HttpUtility 的 UrlEncode 之后, 将 "+" 替换成 "%20"( 如果原来是 "+" 则被转换成 "%2b" ) , 如:
fileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8);
fileName = fileName.Replace("+", "%20");
不明白微软为什么要把空格转换成加号而不是"%20". 记得 JDK 的 UrlEncoder 是将空格转换成 "%20"的.
经检查, 在 .Net 2.0 也是这样.

经Reflector反编译得出:

HttpUtility.UrlDecoder原型如下:

复制代码
      
      
public static string UrlDecode( string str) { if (str == null ) { return null ; } return UrlDecode(str, Encoding.UTF8); }
复制代码

Server.UrlEncoder原型如下:

复制代码
      
      
public string UrlEncode( string s) { Encoding e = ( this ._context != null ) ? this ._context.Response.ContentEncoding : Encoding.UTF8; return HttpUtility.UrlEncode(s, e); }
复制代码
可见HttpUtility中的编码默认是UTF-8,而Server中的默认编码与上下文(Context)有关,缺省编码为UTF-8。

创建源 List 中的元素范围的浅表副本:GetRange

用法实例:

protected IList<PriceCement> getPCBy2(int num)

{

if(pclist.Count>30)

{

if(num==0){return pclist.ToList().GetRange(0,30);}

return pclist.ToList().GetRange(30,pclist.Count>=60?30:pclist.Count-30);}

else{if(num==0) {return pclist;}

return new List<PriceCement>();

}

}

3:ToList操作符

  (1)作用:用于将一个输入序列转换成一个类型为List<T>集合对象;

  (2)方法原型

    public static List<T> ToList<T>(

    this IEnumerable<T> source);

  (3)原型说明:ToList操作符接受一个元素类型为T的输入序列,生成一个元素类型为T的List序列并返回。其中返回值List<T>是一个可以通过索引访问的强类型列表,它也是C#2.0中比较常用的集合类型之一;

如数组类型转换:数字类型:pricement[]转变

List<PriceCement> tempList =pricement.SearchProByPage(null,plist,null,null,null,null,null,null).ToList();

4:Post和Get的区别

从一个页面转向另一个页面的请求方式有两种,Post和Get.
所有的人都知道如下区别:
1.Post传输数据时,不需要在URL中显示出来,而Get方法要在URL中显示。
2.Post传输的数据量大,可以达到2M,而Get方法由于受到URL长度的限制,只能传递大约1024字节.
3.Post顾名思义,就是为了将数据传送到服务器段,Get就是为了从服务器段取得数据.而Get之所以也能传送数据,只是用来设计告诉服务器,你到底需要什么样的数据.Post的信息作为http请求的内容,而Get是在Http头部传输的。

示例:

<title>登录页面</title>
</head>
<body>
    <form id="form1" runat="server" method="post" action="index.aspx">
    <div>
        <asp:TextBox ID="Txtid" runat="server"></asp:TextBox>
        <asp:Button ID="btnpost" runat="server" Text="post传值" οnclick="btnpost_Click"
            PostBackUrl="~/post/index.aspx" />
    </div>
    </form>
</body>

 

+++++===================

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string id = Request.Form["Txtid"].ToString();
             LblMsseage.Text = "欢迎光临" + id;
        }
    }

 

 

get传值=================================

 

   <title>get传值</title>
</head>
<body>
    <form id="form1" runat="server" method="get">
    <div>
        <asp:TextBox ID="Txtid" runat="server"></asp:TextBox>
        <asp:Button ID="btnget" runat="server" Text="get传值"
        PostBackUrl="~/get/index.aspx"
          />
    </div>
    </form>
</body>

 

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string id = Request.QueryString["Txtid"].ToString();
             LblMsseage.Text = "欢迎光临" + id;
        }
    }


5:ashx 文件的作用

.ashx文件用于写web handler的。其实就是带HTML和C#的混合文件。完全可以用.aspx的文件后缀。使用.ashx可以让你专注于编程而不用管相关的WEB技术。.ashx必须包含IsReusable。HttpHandler是一个彻底自定义Http请求的方法,它通过web.config来定义asp.net运行时来过滤出要自定义的Http请求,发送到定义的web.config的指定类中。

利用.ashx文件是一个更好的方法,这个文件类似于.aspx文件,可以通过它来调用HttpHandler类,从而免去了普通.aspx页面的控件解析以及页面处理的过程。这个文件特别适合于生成动态图片,生成动态文本等内容。(多用于生成验证码之类的)

然后在“TextBuilder.ashx”文件的第一行处输入上面这个类的调用代码:
<%@ WebHandler language="C#" Class="MyNamespace.TextBuilder" codebehind="TextBuilder.ashx.cs" %>
上面的代码需要注意的是:必须在Class项中输入类的完整名称,即包括名称空间及类名称。

最后保存并编译项目。

使用IE测试,输入这个.ashx的地址即可。

大家可以看出Response类有个OutputStream方法,可以向客户端输出二进制数据流,所以在我的项目中,使用这个方法,在一个.ashx中使用DundasChart控件就可以生成非常好的统计图,用它发送二进制数据,方便快捷,而且不需在web.config内输入任何配置代码。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值