使用javascript修改表单操作

时间:2009-11-16 22:58:50

标签: c# asp.net javascript forms

我有一个用c#/ asp.net编写的多步webform。我正在尝试更改用于Google分析原因的回复网址。我已经编写了下面的代码来更改客户端的URL,但它似乎没有使用最后的参数发布到url。几乎就像它在客户端更改回来提交。有什么想法吗?

<%@ 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 个答案:

答案 0 :(得分:2)

看起来你正在设置身体上的动作,而不是表格。除非我遗漏了什么,否则你应该能够解决问题:

function setaction(step) {
    var frm = document.getElementsByTagName("form")[0];

    frm.setAttribute("action", "webform1.aspx?step=" + step);
    alert(document.location.href);
}