在asp.net环境下开发web应用,一般情况下我们都直接使用asp.net提供的默认页面提交方式。这里对asp.net环境下的提交方式做个比较。
页面代码如下:
<form id="form2" runat="server">
<div>
<input type="text" id="filelist" name="filelist" runat="server" style="width: 531px" /><br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:TextBox ID="TextBox1" runat="server" Width="468px"></asp:TextBox>
</div>
<div>
<input type="file" class="InputCss" name="upload_file" id="upload_file" οnchange="doUpload();" />
</div>
</form>
1、asp.net默认方式:点击button1直接提交,后台获取参数代码为
TextBox1.Text = filelist.Value;//正确取得数据;
string s = Request.QueryString["filelist"];//取不到数据;
string s1 = Request["filelist"];//正确取得数据
string s2 = Request.Form["filelist"];//正确取得数据
2、修改action方式:
document.getElementById('form2').action="upfile.ashx";
document.getElementById('form2').submit();
后台获取参数代码为
TextBox1.Text = filelist.Value;//错误;
string s = Request.QueryString["filelist"];//为null
string s1 = Request["filelist"];//正确;
string s2 = Request.Form["filelist"];//正确
3、执行方式2后,把action设回原值,点击button1提交
var act = document.getElementById('form2').action;
document.getElementById('form2').action="upfile.ashx";
document.getElementById('form2').submit();
document.getElementById('form2').action=act;
后台获取参数
TextBox1.Text = filelist.Value;//有值
string s = Request.QueryString["filelist"];//有值
string s1 = Request["filelist"];//有值
string s2 = Request.Form["filelist"];//Form为null
4、采用超链接方式提交
<a href='save.aspx?name=ame'>提交</a>
后台获取参数
string s = Request.QueryString["filelist"];//无
string s1 = Request["filelist"];//无;
string s2 = Request.Form["filelist"];//无
string s3 = Request["name"];//正确