如何在asp.net中访问表单操作值?

时间:2014-08-11 17:33:51

标签: c# html asp.net .net

我在asp.net中有一个网页,其表单的ID为"form1",如此

<form id="form1" runat="server">

当我浏览时,它会在页面呈现时转换为跟随:

localhos/default1.aspx/john

渲染的html如下:

<form method="post" action="john" id="form1">

如何在C#中访问操作值"john"

2 个答案:

答案 0 :(得分:0)

由于它已转换为Url(localhos / default1.aspx / john),因此在C#中

Protected void Page_Load()

{
string CurrentUrl=HttpContext.Current.Request.Url.AbsoluteUri;
string ActionName = CurrentUrl.Split('/')[CurrentUrl.count.Split('/').Count-1];


}

//或其他选项是将表单转换为在服务器上运行,因为C#只能访问服务器端控件:

<form="form1" runat="server" action="john"/>

//然后在你的C#代码后面

protected void Page_Load()
{

string FormAction=form1.Action;
}

答案 1 :(得分:0)

您可以使用form1.Action

进行操作

http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.action(v=vs.110).aspx

默认为空,替换为实际的rawurl。

因此,如果form1.Action为空,只需获取Request.RawUrl或Request.Url并获取最后一段

示例:

if (form1.Action == String.Empty)
    Response.Write("action=" 
         + Request.Url.Segments[Request.Url.Segments.Length-1]);