views:

120

answers:

1

I've got a multiple step webform written in c#/asp.net. I'm trying to change the url that posts back to for google analytics reasons. I've written the code below to change the url on the client side, but it doesn't seem to post to the url with the parameters on the end. Almost like it's being changed back on the client side onsubmit. Any ideas?

<%@ 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"&gt;

<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  A: 

It looks like you're setting the action on the body, rather than the form. Unless I'm missing something, you should be able to fix the problem like so:

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

    frm.setAttribute("action", "webform1.aspx?step=" + step);
    alert(document.location.href);
}
Luke Dennis
AAAAAAAAAAAAHHHHHHHHHHHHHHHHH. I guess I just needed a 2nd set of eyes. Completely missed that. Been banging my head off the wall. Thanks.
Darthg8r
haha, I've been there. :) Happy to help.
Luke Dennis