ZackyV
ZackyV

Reputation: 13

Passing arrays from Javascript to asp.net

I have two separate pages. One with a HTML page involving JavaScript (bunch of functions etc.) and an .aspx page.

I have some array in html page and I want to send them(using JavaScript) to .aspx to store them into a database.

I have tried hidden field, creating a form etc. All I get is null. How can I pass my arrays to aspx to store them in database later? I can't use jQuery.

function openPopup() {
        window.open("Default.aspx",  "scrollbars=yes, width=900,height=500,left=430,top=100");
        return false;
    }
function control() {
        var a = JSON.stringify(x);// x and y are my arrays
        var b = JSON.stringify(y);

        var btnId = '<%= HiddenField1.ClientID%>';
        var btnId2 = '<%= HiddenField2.ClientID%>';
        document.getElementById(btnId).value = a;
        document.getElementById(btnId2).value = b;                       
    }

IN HTML

<button onclick="openPopup()">POP</button> //opens the aspx page

IN ASPX

<form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Database" />
    <asp:HiddenField ID="HiddenField1" runat="server" />
    <asp:HiddenField ID="HiddenField2" runat="server" />
</form>

IN ASPX.CS

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Write(HiddenField1);
}

Upvotes: 1

Views: 614

Answers (1)

danwellman
danwellman

Reputation: 9253

You will need to send the data to the server using AJAX (preferably) or using a traditional form submission (page will be reloaded after submission).

The server cannot access the HTML elements once the page has been rendered in the browser.

Upvotes: 1

Related Questions