C# 使用javascript修改表单操作
我有一个用c#/asp.net编写的多步骤Web表单。出于谷歌分析的原因,我正在尝试更改发回的url。我已经编写了下面的代码来更改客户端的url,但它似乎没有在最后使用参数发布到url。几乎就像在提交时在客户端对其进行了更改一样。有什么想法吗C# 使用javascript修改表单操作,c#,asp.net,javascript,forms,C#,Asp.net,Javascript,Forms,我有一个用c#/asp.net编写的多步骤Web表单。出于谷歌分析的原因,我正在尝试更改发回的url。我已经编写了下面的代码来更改客户端的url,但它似乎没有在最后使用参数发布到url。几乎就像在提交时在客户端对其进行了更改一样。有什么想法吗 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestNamespace.WebForm1" %> <!DOC
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestNamespace.WebForm1" %>
<!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 onload="setaction(<%= step %>);">
<form id="form1" runat="server">
<div>
<asp:Label ID="lblCurrentUrl" runat="server"></asp:Label>
<asp:Panel ID="panel1" runat="server">
Panel 1<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Button" />
</asp:Panel>
<asp:Panel ID="panel2" runat="server" Visible="false">
Panel 2<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="Button" />
</asp:Panel>
<asp:Panel ID="panel3" runat="server" Visible="false">
Panel 3<asp:Button ID="Button3" runat="server" onclick="Button3_Click"
Text="Button" />
</asp:Panel>
<asp:Panel ID="panel4" runat="server" Visible="false">
Panel 4<asp:Button ID="Button4" runat="server" onclick="Button4_Click"
Text="Button" />
</asp:Panel>
</div>
<script language="javascript" type="text/javascript">
function setaction(step) {
var bdy = document.getElementsByTagName("body")[0];
bdy.setAttribute("action", "webform1.aspx?step=" + step);
alert(document.location.href);
}
</script>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TestNamespace
{
public partial class WebForm1 : System.Web.UI.Page
{
protected int step;
protected void Page_Load(object sender, EventArgs e)
{
lblCurrentUrl.Text = Request.Url.ToString();
if ( IsPostBack ) return;
step = 1;
}
protected void Button1_Click(object sender, EventArgs e)
{
panel1.Visible = false;
panel2.Visible = true;
step = 2;
}
protected void Button2_Click(object sender, EventArgs e)
{
panel2.Visible = false;
panel3.Visible = true;
step = 3;
}
protected void Button3_Click(object sender, EventArgs e)
{
panel3.Visible = false;
panel4.Visible = true;
step = 4;
}
protected void Button4_Click(object sender, EventArgs e)
{
}
}
}
小组1
小组2
小组3
小组4
函数设置动作(步骤){
var bdy=document.getElementsByTagName(“正文”)[0];
bdy.setAttribute(“操作”,“webform1.aspx”步骤=“+step”);
警报(document.location.href);
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
命名空间TestNamespace
{
公共部分类WebForm1:System.Web.UI.Page
{
保护整数步;
受保护的无效页面加载(对象发送方、事件参数e)
{
lblCurrentUrl.Text=Request.Url.ToString();
如果(IsPostBack)返回;
步骤=1;
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
面板1.可见=假;
面板2.可见=真实;
步骤=2;
}
受保护的无效按钮2\u单击(对象发送者,事件参数e)
{
面板2.可见=假;
面板3.可见=真实;
步骤=3;
}
受保护的无效按钮3\u单击(对象发送者,事件参数e)
{
面板3.可见=假;
面板4.可见=真实;
步骤=4;
}
受保护的无效按钮4\u单击(对象发送者,事件参数e)
{
}
}
}
看起来您是在主体上设置动作,而不是在表单上设置动作。除非我遗漏了什么,否则您应该能够这样解决问题:
function setaction(step) {
var frm = document.getElementsByTagName("form")[0];
frm.setAttribute("action", "webform1.aspx?step=" + step);
alert(document.location.href);
}
aaaaaaaaaaahhhhhhhhh。我想我只是需要第二副眼睛。完全没有注意到这一点。我的头撞到墙上了。谢谢