C# Form Post 通过 Request.QueryString 而不是 Request.Form 发送值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/568129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Form Post Sends values via Request.QueryString instead of Request.Form
提问by daveywc
Here is an extract of HTML from within a standard HTML page:
以下是标准 HTML 页面中的 HTML 摘录:
<form name="login_form" action="http://localhost/BOB.WebPortal/LoginForm.aspx" method="post>
<div id="content_logins">
<div id="spacer_ie" style="width:990px; height:7px; border:none; float:left"></div>
<div style="width:255px; height:22px; border:none; float:left"></div>
<div style="width:172px; border:none; float:left; vertical-align:bottom">
<input type="text" name="uname" id="uname" size="13" /></div>
<div style="width:110px; border:none; float:left">
<input type="password" name="pword" id="pword" size="13" /></div>
<div style="width:140px; height:22px; border:none; margin-top:-2px; float:left">
<!--input class="btbgfix" type="image" src="images/submit.png" alt="Submit" value="Login"-->
<input name="submit" id="loginButton" class="btbgfix" type="image" src="images/submit.png" alt="Submit" value="Login"></div>
<div style="width:320px; height:22px border:none; float:left">
<img name="login" src="x.gif" id="alogin" usemap="#m_login" border="0" width="320" height="22" />
<map name="m_login" id="m_login">
<area shape="rect" coords="0,0,681,22" href="https://www.aetmyportfolio.com.au" target="_blank" alt="Go to AET My Portfolio" />
</map>
</div>
</div>
</form>
My problem is that I am trying to retrieve the post value uname and pword using Request.Form from within the Page_Load event of the C# codebehind file in my ASP.Net application. However I find that Request.Form is empty. However if I use Request.Params then I can access the values as they are being passed as query string values in the URL.
我的问题是我正在尝试使用 Request.Form 从我的 ASP.Net 应用程序中 C# 代码隐藏文件的 Page_Load 事件中检索发布值 uname 和 pword。但是我发现 Request.Form 是空的。但是,如果我使用 Request.Params,那么我可以访问这些值,因为它们作为 URL 中的查询字符串值传递。
Am I doing something wrong in my html?
我在我的 html 中做错了什么吗?
采纳答案by David Wengier
Assuming that's a direct copy-paste from your HTML, you're missing a closing quote on the end of "post".
假设这是从您的 HTML 中直接复制粘贴,那么您将缺少“post”末尾的结束语。
回答by RSolberg
Since you are working with a standard HTML page for your login control, it does not seem like the runat="server" is going to solve all of your problems. Here is a quick sample of how I replicated your solution. It seems to pick up the values correctly.
由于您正在为登录控件使用标准 HTML 页面,因此 runat="server" 似乎无法解决您的所有问题。这是我如何复制您的解决方案的快速示例。它似乎正确地获取了值。
Login Page
登录页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form name="login_form" action="Default.aspx" method="post">
<div id="content_logins">
user:<input type="text" name="uname" id="uname" size="13" /><br />
pass:<input type="password" name="pword" id="pword" size="13" /><br />
<input name="submit" id="loginButton" type="submit" alt="Submit" value="Login">
</div>
</form>
</body>
</html>
Default.aspx
默认.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Default.aspx.cs
默认.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Label1.Text = Request.Params["uname"].ToString();
this.Label2.Text = Request.Params["pword"].ToString();
}
}