﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-龙霖天网-文章分类-.net</title><link>http://www.cnitblog.com/qlb5626267/category/7905.html</link><description>龙霖学习园地</description><language>zh-cn</language><lastBuildDate>Tue, 04 Oct 2011 19:02:31 GMT</lastBuildDate><pubDate>Tue, 04 Oct 2011 19:02:31 GMT</pubDate><ttl>60</ttl><item><title>ASP页面跳转</title><link>http://www.cnitblog.com/qlb5626267/articles/53635.html</link><dc:creator>龙霖</dc:creator><author>龙霖</author><pubDate>Fri, 09 Jan 2009 11:14:00 GMT</pubDate><guid>http://www.cnitblog.com/qlb5626267/articles/53635.html</guid><wfw:comment>http://www.cnitblog.com/qlb5626267/comments/53635.html</wfw:comment><comments>http://www.cnitblog.com/qlb5626267/articles/53635.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/qlb5626267/comments/commentRss/53635.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/qlb5626267/services/trackbacks/53635.html</trackback:ping><description><![CDATA[<table style="TABLE-LAYOUT: fixed">
    <tbody>
        <tr>
            <td>
            <div class=cnt id=blog_text>
            <p>今天在做一个系统的时候需要用到页面跳转，一般来说就是三种方法：</p>
            <p>1、 response.redirect 需要注意的是跳转后内部空间保存的所有数据信息将会丢失，所以需要用到<strong>session</strong>。</p>
            <p>　　2、 server.transfer 这个方法的重定向请求是发生在服务器端，所以浏览器的url地址仍然保留的是原页面的地址!</p>
            <p>　　3、 sever.execute 这个方法主要是用在页面设计上面，而且他必须是跳转同一站点下的页面。</p>
            <p>&nbsp;</p>
            <p>不过在具体的时候用到页面跳转的时候，虽然能够跳转过去，但是总是会出现异常。如果你试图用try，catch捕获异常的并将异常的路径打出来的时候就会发现问题：页面正常跳转了，同时也出现了异常。</p>
            <p>网上有一个帖子是解决这个问题的，我摘过来了并以我自身的尝试将对此补充一点：</p>
            <p>下面是帖子的原文（地址：<a href="http://hi.baidu.com/lyh0702/blog/item/e121588277bbb7a10cf4d241.html"><u><font color=#0000ff>http://hi.baidu.com/lyh0702/blog/item/e121588277bbb7a10cf4d241.html</font></u></a>）</p>
            <div align=right><font color=#0000ff>在项目里负责异常处理部分：异常信息的写入，读取，查看，因此就可以看到各种异常，受益匪浅</font></div>
            <div style="FONT-SIZE: 14px">
            <div><font color=#0000ff>看到有N多的&#8220;线程正被中止&#8221;异常，而且来自同一个页面。</font></div>
            <div>
            <div><font color=#0000ff>System.Threading.ThreadAbortException: 线程正被中止。</font></div>
            <div><font color=#0000ff>at System.Threading.Thread.AbortInternal() </font></div>
            <div><font color=#0000ff>at System.Threading.Thread.Abort(Object stateInfo) at System.Web.HttpResponse.End() </font></div>
            <div><font color=#0000ff>at System.Web.HttpServerUtility.Transfer(String path, Boolean preserveForm) </font></div>
            <div><font color=#0000ff>at RPM.Page.LogInHR.btnLogin_Click(Object sender, EventArgs e) </font></div>
            <div><font color=#0000ff>in c:\inetpub\wwwroot\rpm_2\page\login\loginhr.aspx.cs:line 363 </font></div>
            </div>
            <div><font color=#0000ff>虽然捕获了，但并没有跳转到错误页面，但这个信息是在错误页面被写入数据库的！</font></div>
            <div><font color=#0000ff>我做了个试验，在异常信息查询页面跳转到异常信息一览页面</font></div>
            <div><font color=#0000ff>引发异常的页面：</font></div>
            <div><font color=#0000ff>try<br>&nbsp;&nbsp;&nbsp;&nbsp; {</font></div>
            <div><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 异常一览页面<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Server.Transfer("../SystemManagement/ErrorReportView.aspx",false);<br>&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp; catch (Exception ex)<br>&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpContext.Current.Session["CurrentError"] = ex.ToString();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpContext.Current.Session["CurrentErrorUrl"] = HttpContext.Current.Request.Url.ToString();</font></div>
            <div></div>
            <div><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 跳转到异常信息显示页面<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpContext.Current.Server.Transfer("ErrorReport.aspx",false);<br>&nbsp;&nbsp;&nbsp;&nbsp; }</font></div>
            <div><font color=#0000ff>异常信息显示页面对异常的处理:</font></div>
            <div><font color=#0000ff>string errortip = "&lt;h4&gt;" + Resources["MEMO_346"] + "&lt;/h4&gt;" + "&lt;br&gt;&lt;br&gt;";</font></div>
            <div><br><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string error = <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "在页面：" + "&lt;br&gt;" <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + (string)Session["CurrentErrorUrl"] +"&lt;br&gt;" <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + "发生了如下异常：" + "&lt;br&gt;"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + (string)Session["CurrentError"];</font></div>
            <div><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;</font></div>
            <div><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 部署时 不要显示异常信息！<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lblErrorMessage.Text = errortip + error;</font></div>
            <div><font color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 写入数据库<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LCW.RPM.BLL.ErrorReport errorReport = new LCW.RPM.BLL.ErrorReport();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LCW.RPM.Model.ErrorReportInfo errorInfo = new LCW.RPM.Model.ErrorReportInfo();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; errorInfo.Content = error;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; errorInfo.UserName = "sa";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; errorReport.Insert(errorInfo);</font></div>
            <div><font color=#0000ff>也就是说实际上，确实跳转到了异常处理页面，并执行了代码，但没等到页面显示，就又跳到了异常信息一览页面，但此时并没有将刚才的异常查询出来，需要刷新页面才会出来。看来确实存在终止线程的问题</font></div>
            <div><font color=#0000ff>MSDN是这么说的：</font></div>
            <div><font color=#0000ff>ThreadAbortException 的引发条件：对 <strong>End</strong> 的调用已经终止当前请求。</font></div>
            <div>
            <p><font color=#0000ff><strong>Redirect</strong> 调用End，该方法在完成时引发 ThreadAbortException 异常。</font></p>
            <p><font color=#0000ff>对 <strong>End、</strong>Redirect 和Transfer的调用在当前响应提前结束时引发一个 ThreadAbortException。</font></p>
            <p><font color=#0000ff>End(HttpResponse.End )方法的说明：</font></p>
            <p><font color=#0000ff>将当前所有缓冲的输出发送到客户端，停止该页的执行，并引发 <strong>Application_EndRequest</strong> 事件。</font></p>
            <p><font color=#0000ff>ThreadAbortException：</font></p>
            <p><font color=#0000ff>在调用 Abort方法以销毁线程时，公共语言运行库将引发 <strong>ThreadAbortException</strong>。<strong>ThreadAbortException</strong> 是一种可捕获的特殊异常，但在 <strong>catch</strong> 块的结尾处它将自动被再次引发。引发此异常时，运行库将在取消线程前执行所有 <strong>finally</strong> 块。由于线程可以在 <strong>finally</strong> 块中进行未绑定的计算，因此必须调用Join方法以保证线程已死亡。<strong>Join</strong> 是一个阻塞调用，它要到线程实际停止执行后才返回。</font></p>
            <p><font color=#0000ff>想不太明白</font></p>
            <p><font color=#0000ff>Update：</font></p>
            <p><font color=#0000ff>看了</font><a href="http://jinglecat.cnblogs.com/"><u><font color=#0000ff>晓风残月</font></u></a><font color=#0000ff> 写的一篇文章 </font><a href="http://jinglecat.cnblogs.com/archive/2006/04/25/384897.html"><u><font color=#0000ff>Response.Redirect 与 异常（线程正在中止）</font></u></a><font color=#0000ff> ，提到</font><a href="http://support.microsoft.com/default.aspx?scid=kb;EN-US;312629"><u><font color=#0000ff>一文</font></u></a><font color=#0000ff>：</font></p>
            <p><font color=#0000ff>原来&nbsp;&nbsp;&nbsp; HttpContext.Current.Server.Transfer("ErrorReport.aspx",false);<br>是把异常挂起了，所以代码照样执行</font></p>
            <p><font color=#0000ff>按照文章试验了一下：</font></p>
            <p><font color=#0000ff>Response.Redirect(url)等同于Response.Redirect(ur,true),有异常</font></p>
            <p><font color=#0000ff>Response.Redirect(url,false),无异常</font></p>
            <p>&nbsp;</p>
            <p><font color=#0000ff>Server.Execute(url)无异常</font></p>
            <p>&nbsp;</p>
            <p><font color=#0000ff>Server.Transfer(url)等同于Server.Transfer(url,true)和Server.Transfer("ErrorReportView.aspx",false);都会出现异常</font></p>
            <p>&nbsp;</p>
            <p><font color=#0000ff>总之，要想避免异常，就用Response.Redirect(url,false)或Server.Execute(url)。</font></p>
            <p><font color=#0000ff>关于这三者的区别，另开新帖说明。</font></p>
            <p><font color=#0000ff>ps：</font></p>
            <p><font color=#0000ff>偶然发现Server.Execute(url)可以实现那种弹出框提示后紧接着跳转页面的需求，而其他两种情况下弹出框提示信息都不会显示。</font></p>
            <p><font color=#0000ff>注：如果使用 try 、catch 处理包含Response.Redirect&nbsp;&nbsp;&nbsp; 语句代码，总是能捕捉到异常：线程正在中止，<br>其实不仅仅Response.Redirect 会，Response.End 和 Server.Transfer 也是一样的情况，本质的原因的原因是Response.End 方法停止页的执行，并将该执行变换到应用程序的事件管线中的 Application_EndRequest 事件， Response.End 后面的代码行将不执行。通常认为Response.End 方法是线程的非正常结束，因此MS就在内部抛出ThreadAbortException 异常，而Response.Redirect 和 Server.Transfer 这两种方法都在内部调用 Response.End。</font></p>
            <p>我对此做一点补充，我尝试了各种方法，最后我用的是Page.Response.Redirect("Allowed/Default.aspx");没有出现上述异常问题。其实这个问题的具体原因我也不知道，也许正是那个帖子说的呢。</p>
            </div>
            </div>
            </div>
            </td>
        </tr>
    </tbody>
</table>
<img src ="http://www.cnitblog.com/qlb5626267/aggbug/53635.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/qlb5626267/" target="_blank">龙霖</a> 2009-01-09 19:14 <a href="http://www.cnitblog.com/qlb5626267/articles/53635.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ASP.NET十分有用的页面间传值方法</title><link>http://www.cnitblog.com/qlb5626267/articles/51575.html</link><dc:creator>龙霖</dc:creator><author>龙霖</author><pubDate>Tue, 18 Nov 2008 02:24:00 GMT</pubDate><guid>http://www.cnitblog.com/qlb5626267/articles/51575.html</guid><wfw:comment>http://www.cnitblog.com/qlb5626267/comments/51575.html</wfw:comment><comments>http://www.cnitblog.com/qlb5626267/articles/51575.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/qlb5626267/comments/commentRss/51575.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/qlb5626267/services/trackbacks/51575.html</trackback:ping><description><![CDATA[<h1>ASP.NET十分有用的页面间传值方法</h1>
<div class=times>2007-07-13 14:09:00 来源:中国站长站 作者:编辑整理 【<a href="javascript:doZoom(16)"><u><font color=#0000ff>大</font></u></a> <a href="javascript:doZoom(14)"><u><font color=#0000ff>中</font></u></a> <a href="javascript:doZoom(12)"><u><font color=#0000ff>小</font></u></a>】 <a class=fsource href="http://www.chinaz.com/Program/.NET/0G31192R007.html#plshow"><u><font color=#0000ff>评论：</font><font id=feedcounttop color=red>2</font><font color=#0000ff> 条</font></u></a></div>
<div class=content id=textbody>
<p><strong>一、目前在ASP.NET中页面传值共有这么几种方式：</strong><br><strong>1、表单提交</strong><br>&nbsp;&nbsp; &lt;form action= "target.aspx" method = "post" name = "form1"&gt;<br>&nbsp;&lt;input name = "param1" value = "1111"/&gt;<br>&nbsp;&lt;input name = "param2" value = "2222"/&gt; <br>&nbsp;&nbsp; &lt;/form&gt;<br>&nbsp;&nbsp; ....<br>&nbsp;&nbsp; form1.submit();<br>&nbsp;&nbsp; ....<br>&nbsp;&nbsp; 此种方在ASP。NET中无效，因为ASP。NET的表单总是提交到自身页面，如果要提交到别一页面，需要特殊处理。<br><strong>2、链接地址传送</strong><br>接收页面： string str = Request["param1"]<br><strong>3、Session共享</strong><br>发送页面：Session("param1") = "1111";&nbsp; <br>按收页面&nbsp; string str = Session("param1").ToString();&nbsp; <br><strong>4、Application共享</strong><br>发送页面： Application("param1") = "1111";&nbsp;&nbsp; <br>按收页面： string str = Application("param1").ToString();&nbsp; <br>此种方法不常使用，因为Application在一个应用程序域范围共享，所有用户可以改变及设置其值，故只应用计数器等需要全局变量的地方。<br><strong>5、Cookie</strong><br><strong>6、Response.Redirect()方式</strong><br>&nbsp;&nbsp; Response.Redirect("target.aspx?param1=1111&#182;m2=2222")<br>&nbsp;&nbsp; 接收页面： string str = Request["param1"]<br><strong>7、Server.Transfer()方式。</strong><br>&nbsp;&nbsp; Server.Transfer("target.aspx?param1=1111&#182;m2=2222")<br>&nbsp;&nbsp; 接收页面： string str = Request["param1"]</p>
<p>二、如果在两个页面间需要大量的参数要传传递，如数据查询等页面时，用1 － 6的方法传值及其不便，而第 7 种方法确有一独特的优势！但使用该方法时需要一定的设置，现简单介绍一下该方法的使用方式：<br>&nbsp;&nbsp; 以查询数据页面为例：<br>&nbsp;&nbsp; 在查询页面中设置如下公有属性(QueryPage.aspx)：</p>
<p>
<table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
    <tbody>
        <tr>
            <td style="WORD-WRAP: break-word" bgColor=#fdfddf><font color=#ff0000>以下为引用的内容：</font><br>public class QueryPage : System.Web.UI.Page<br>{<br>protected System.Web.UI.WebControls.TextBox txtStaDate;<br>protected System.Web.UI.WebControls.TextBox txtEndDate;<br>&nbsp;&nbsp; ...<br>/// &lt;summary&gt;<br>/// 开始时间<br>/// &lt;/summary&gt;<br>public string StaDate<br>{<br>get{ return this.txtStaDate.Text;}<br>set{this.txtStaDate.Text = value;}<br>}<br>/// &lt;summary&gt;<br>/// 结束时间<br>/// &lt;/summary&gt;<br>public string EndDate<br>{<br>get{ return this.txtEndDate.Text;}<br>set{this.txtEndDate.Text = value;}<br>}<br>....<br>private void btnEnter_Click(object sender, System.EventArgs e)<br>{<br>Server.Transfer("ResultPage.aspx");<br>}<br>}&nbsp;</td>
        </tr>
    </tbody>
</table>
</p>
<p>在显示查询结果页面(ResultPage.aspx)：</p>
<p>
<table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
    <tbody>
        <tr>
            <td style="WORD-WRAP: break-word" bgColor=#fdfddf><font color=#ff0000>以下为引用的内容：</font><br>&nbsp;public class ResultPage : System.Web.UI.Page<br>{<br>&nbsp;&nbsp; private void Page_Load(object sender, System.EventArgs e)<br>&nbsp;&nbsp; {
            <p>//转换一下即可获得前一页面中输入的数据<br>QueryPage queryPage = ( QueryPage )Context.Handler;</p>
            <p>Response.Write( "StaDate：" );<br>Response.Write( queryPage.StaDate );<br>Response.Write( "&lt;br/&gt;EndDate：" );<br>Response.Write( queryPage.EndDate );<br>&nbsp; }<br>}&nbsp;</p>
            </td>
        </tr>
    </tbody>
</table>
</p>
</div>
<div class=content id=textbody>
<p>&nbsp;</p>
<p><strong>三、如果有许多查询页面共用一个结果页面的设置方法：</strong><br>&nbsp;&nbsp;&nbsp; 在这种方式中关键在于&#8220; QueryPage queryPage = ( QueryPage )Context.Handler; &#8221;的转换，只有转换不依赖于特定的页面时即可实现。<br>如果让所有的查询页面都继承一个接口，在该接口中定义一个方法，该方法的唯一作用就是让结果页面获得构建结果时所需的参数,就可实现多页面共享一个结果页面操作！</p>
<p>1、先定义一个类，用该类放置所有查询参数：</p>
<p>
<table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
    <tbody>
        <tr>
            <td style="WORD-WRAP: break-word" bgColor=#fdfddf><font color=#ff0000>以下为引用的内容：</font><br>/// &lt;summary&gt;<br>/// 结果页面中要用到的值<br>/// &lt;/summary&gt;<br>public class QueryParams<br>{<br>private string staDate;<br>private string endDate;
            <p>/// &lt;summary&gt;<br>/// 开始时间<br>/// &lt;/summary&gt;<br>public string StaDate<br>{<br>get{ return this.staDate;}<br>set{this.staDate = value;}<br>}<br>/// &lt;summary&gt;<br>/// 结束时间<br>/// &lt;/summary&gt;<br>public string EndDate<br>{<br>get{ return this.endDate;}<br>set{this.endDate = value;}<br>}<br>}&nbsp;</p>
            </td>
        </tr>
    </tbody>
</table>
</p>
<p>2、接口定义：</p>
<p>
<table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
    <tbody>
        <tr>
            <td style="WORD-WRAP: break-word" bgColor=#fdfddf><font color=#ff0000>以下为引用的内容：</font><br>
            <p>/// &lt;summary&gt;<br>/// 定义查询接口。<br>/// &lt;/summary&gt;<br>public interface IQueryParams<br>{<br>/// &lt;summary&gt;<br>/// 参数<br>/// &lt;/summary&gt;<br>QueryParams Parameters{get;}<br>}&nbsp; </p>
            </td>
        </tr>
    </tbody>
</table>
</p>
<p>3、查询页面继承IQueryParams接口(QueryPage.aspx)：</p>
<p>
<table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
    <tbody>
        <tr>
            <td style="WORD-WRAP: break-word" bgColor=#fdfddf><font color=#ff0000>以下为引用的内容：</font><br>
            <p>/// &lt;summary&gt;<br>///查询页面，继承接口<br>/// &lt;/summary&gt;<br>public class QueryPage : System.Web.UI.Page, IQueryParams<br>{<br>protected System.Web.UI.WebControls.TextBox txtStaDate;<br>protected System.Web.UI.WebControls.TextBox txtEndDate; <br>private QueryParams queryParams;<br>&nbsp;&nbsp; ...<br>/// &lt;summary&gt;<br>/// 结果页面用到的参数<br>/// &lt;/summary&gt;<br>&nbsp;&nbsp; public QueryParams Parameters<br>{<br>get<br>{<br>return queryParams;<br>}<br>}<br>....<br>private void btnEnter_Click(object sender, System.EventArgs e)<br>{<br>//赋值<br>queryParams = new QueryParams();<br>queryParams.StaDate = this.txtStaDate.Text;<br>queryParams.EndDate = this.txtEndDate.Text</p>
            <p>Server.Transfer("ResultPage.aspx");<br>}<br>} <br></p>
            </td>
        </tr>
    </tbody>
</table>
&nbsp;</p>
<p>&nbsp;</p>
</div>
<div class=content id=textbody>
<p>&nbsp;</p>
<p>4、别外的页面也如此设置<br>5、接收页面(ResultPage.aspx)：</p>
<p>
<table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
    <tbody>
        <tr>
            <td style="WORD-WRAP: break-word" bgColor=#fdfddf><font color=#ff0000>以下为引用的内容：</font><br>public class ResultPage : System.Web.UI.Page<br>{<br>&nbsp;&nbsp; private void Page_Load(object sender, System.EventArgs e)<br>&nbsp;&nbsp; {
            <p>QueryParams queryParams = new QueryParams();<br>IQueryParams queryInterface;<br>//实现该接口的页面<br>if( Context.Handler is IQueryParams)<br>{<br>queryInterface = ( IQueryParams )Context.Handler;<br>queryParams = queryInterface.Parameters;<br>}</p>
            <p>Response.Write( "StaDate：" );<br>Response.Write( queryParams.StaDate );<br>Response.Write( "&lt;br/&gt;EndDate：" );<br>Response.Write( queryParams.EndDate );<br>&nbsp; }<br>}</p>
            </td>
        </tr>
    </tbody>
</table>
</p>
<p>三、本文起因:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 因在工作中要作一个数据查询，参数烦多，原先是用Session传递，用完该Session传来的参数后，还需清理Session，在用Session之前还得判断该Session是否存在，极其烦琐，我想应该还有更简便的方法来实现页面间的参数传递，故上网查找，终于找到这样一种方式来实现页面间的参数传递。</p>
<p><strong>相关阅读：</strong></p>
<p><a href="http://www.chinaz.com/Program/.NET/101U10R006.html"><font color=#ff0000><strong>asp.net WebForm页面间传值方法</strong></font></a></p>
<p>本文收集整理自互联网，如果您是原文作者，请与我们联系 Zujizhe#Chinaz.com （请将#改为@）</p>
</div>
<img src ="http://www.cnitblog.com/qlb5626267/aggbug/51575.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/qlb5626267/" target="_blank">龙霖</a> 2008-11-18 10:24 <a href="http://www.cnitblog.com/qlb5626267/articles/51575.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Asp.net动态添加附件上传</title><link>http://www.cnitblog.com/qlb5626267/articles/51264.html</link><dc:creator>龙霖</dc:creator><author>龙霖</author><pubDate>Sun, 09 Nov 2008 12:00:00 GMT</pubDate><guid>http://www.cnitblog.com/qlb5626267/articles/51264.html</guid><wfw:comment>http://www.cnitblog.com/qlb5626267/comments/51264.html</wfw:comment><comments>http://www.cnitblog.com/qlb5626267/articles/51264.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/qlb5626267/comments/commentRss/51264.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/qlb5626267/services/trackbacks/51264.html</trackback:ping><description><![CDATA[两种方式：<br>1、如果页面中form指定了enctype="multipart/form-data" 属性则可以无file服务器控件<br>2、如果页面上有初始的file服务器控件（必须有runat="server"标志）可以无enctype="multipart/form-data"属性，但是这个页面上必须至少有一个runat="server"的file控件，否则后台接收不到Request.Files。
<pre>&lt;%@ Page language="c#" Codebehind="MultiAttchments.aspx.cs" AutoEventWireup="false" Inherits="WebApplication3.MultiAttchments" %&gt;<br>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" &gt;<br>&lt;HTML&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    &lt;HEAD&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    &lt;script&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    function AddAttachments()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    document.getElementById('attach').innerText = "继续添加附件";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    tb = document.getElementById('attAchments');&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    newRow = tb.insertRow();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    newRow.insertCell().innerHTML = "&lt;input name='File' size='50' type='file'&gt;&amp;nbsp;&amp;nbsp;&lt;input type=button value='删除' onclick='delFile(this.parentElement.parentElement.rowIndex)'&gt;";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    function delFile(index)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    document.getElementById('attAchments').deleteRow(index);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    tb.rows.length &gt; 0?document.getElementById('attach').innerText = "继续添加附件":document.getElementById('attach').innerText = "添加附件";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    &lt;/script&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    &lt;/HEAD&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    &lt;body MS_POSITIONING="GridLayout"&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    &lt;form id="form1" method="post" runat="server" enctype="multipart/form-data"&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    &lt;div&gt;&lt;table id="attAchments"&gt;&lt;/table&gt;&lt;/div&gt;&lt;span&gt;&lt;IMG src="icoAddFl.gif"&gt; &lt;/span&gt; &lt;A id="attach" style="font-family:宋体;font-size:9pt;" title="如果您要发送多个附件，您只需多次点击&#8220;继续添加附件&#8221;即可, 要注意附件总量不能超过发送限制的大小。" onclick="AddAttachments();"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    href="javascript:;" name="attach"&gt;添加附件&lt;/A&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    &lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    &lt;asp:Button id="btnSend" runat="server" Text=" 上传 "&gt;&lt;/asp:Button&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    &lt;/form&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    &lt;/body&gt;<br>&lt;/HTML&gt;</pre>
<pre>private void btnSend_Click(object sender, System.EventArgs e)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    StringBuilder sb = new StringBuilder();
<p> </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    int attCount = 0;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    string filePath = "";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    for(int i=0; i&lt; Request.Files.Count; i++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    if(Request.Files[i].ContentLength &gt; 0)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    filePath = Request.Files[i].FileName;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    sb.Append("Files" + attCount++ + ": " + filePath + "&lt;br&gt;");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    Request.Files[0].SaveAs(Server.MapPath("./") + filePath.Substring(filePath.LastIndexOf("<a href="file://%22)+1/"><u><font color=#0000ff>\\")+1</font></u></a>));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    }</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    sb.Insert(0, "you upload " + attCount + " files.&lt;br&gt;");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    Response.Write(sb.ToString());<br>}</p>
</pre>
<br>注意对于超出默认大小的文件需修改配置文件<br><br>&lt;configuration&gt; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;system.web&gt; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;httpRuntime&nbsp;&nbsp; maxRequestLength="10000" <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; useFullyQualifiedRedirectUrl="true" <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; executionTimeout="45"/&gt; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/system.web&gt; <br>&lt;/configuration&gt;<br>需要将machine.config或web.config中的&lt;httpRuntime&gt;&nbsp;&nbsp; section的maxRequestLength值作相应的修改. <br><br>===========================================<br>//html文件<br>&lt;form id="form1" runat="server" &gt;<br>&lt;div id="fileupload_div"&gt;&lt;span&gt;作品：&lt;/span&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:FileUpload ID="FileUpload1" runat="server" /&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;INPUT TYPE="button" VALUE="添加附件" onclick="javascript:AddFile();"&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;INPUT TYPE="button" VALUE="删除附件" onclick="javascript:RemoveFile();"&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:ListBox ID="ListBox1" runat="server" Height="120px" Width="252px"&gt;&lt;/asp:ListBox&gt;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/div&gt;<br>&lt;/form&gt;<br><br><br><br>// JScript 文件<br>//注结合jquery+jquery selecte pulgin http://www.texotela.co.uk/code/jquery/select/<br>function AddFile()<br>{<br>&nbsp;&nbsp;&nbsp; var fileupload=$("input[type=file]")[0];<br>&nbsp;&nbsp;&nbsp; if(fileupload.value == "")<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert("请选择要上传的文件!");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; var ary = fileupload.value.split("\\");<br>&nbsp;&nbsp;&nbsp; var filename = ary[ary.length-1];<br>&nbsp;&nbsp;&nbsp; var bAddFile = true;<br>&nbsp;&nbsp;&nbsp; if(CheckOptionsExists(filename,$("#ListBox1")[0]))<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert("文件已经存在列表中!");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $("#fileupload_div")[0].removeChild(fileupload);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bAddFile = false;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; var newfile = document.createElement("input");<br>&nbsp;&nbsp;&nbsp; newfile.type ="file";<br>&nbsp;&nbsp;&nbsp; newfile.name ="file";<br>&nbsp;&nbsp;&nbsp; $("#fileupload_div")[0].insertBefore(newfile,$("#fileupload_div")[0].firstChild);<br>&nbsp;&nbsp;&nbsp; if(!bAddFile)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; return<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; //var o = new Option(filename,fileupload.uniqueID);<br>&nbsp;&nbsp;&nbsp; //$("#ListBox1")[0].add(o); //IE only<br>&nbsp;&nbsp;&nbsp; $("#ListBox1").addOption(fileupload.uniqueID,filename,false);<br>&nbsp;&nbsp;&nbsp; fileupload.style.display = "";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>}<br>function CheckOptionsExists(value,ddl)<br>{<br>&nbsp;&nbsp;&nbsp; for(var i=0;i&lt;ddl.options.length;i++)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(ddl.options[i].innerText == value)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; return false;<br>}<br>function RemoveFile()<br>{<br>&nbsp;&nbsp;&nbsp; var lst = $("#ListBox1")[0];<br>&nbsp;&nbsp;&nbsp; if(lst.selectedIndex == -1)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert("请选择要删除的附件!");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; var id = lst.options[lst.selectedIndex].value;<br>&nbsp;&nbsp;&nbsp; //alert( lst.options[lst.selectedIndex].value);<br>&nbsp;&nbsp;&nbsp;&nbsp; $("#fileupload_div")[0].removeChild(document.all[id]);<br>&nbsp;&nbsp;&nbsp; lst.removeChild(lst.options[lst.selectedIndex]);<br>&nbsp;&nbsp;&nbsp;&nbsp; $("#fileupload_div")[0].firstChild.style.display = "";<br>&nbsp;&nbsp;&nbsp;&nbsp; <br>} 
<img src ="http://www.cnitblog.com/qlb5626267/aggbug/51264.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/qlb5626267/" target="_blank">龙霖</a> 2008-11-09 20:00 <a href="http://www.cnitblog.com/qlb5626267/articles/51264.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>