Click to See Complete Forum and Search --> : Getting javascript array into database by ASP.net


yuenli
07-22-2007, 10:13 PM
Hi!

I am lost here. I have a javascript array in my html page. How do i get the array values to store in database using asp.net? Any reference or guidance out there? Thanks in advance. Really appreciate your help. :)

LochDhu
07-24-2007, 10:38 AM
Put your array into a Server side Hidden input. This way to can access it at PostBack.

yuenli
07-24-2007, 10:43 AM
Thanks. Can you show me how to put the array to server side hidden value? I am new about this. Thanks.

LochDhu
07-24-2007, 11:40 AM
Place a hidden field into your form.

<asp:HiddenField ID="HiddenField1" runat="server" />

And somewhere before postback in javascript set the value of this hidden field.


var cars = new Array("Mercedes", "Ford", "Chrysler")
document.form1.HiddenField1.value = cars

Now you can get to this from your serverside code.

Dim a As String
a = Me.HiddenField1.Value

yuenli
07-24-2007, 07:52 PM
Thanks for the reply. But i still blur how do I set the postback in javascript? Thanks again.

lmf232s
07-24-2007, 09:07 PM
document.form1.submit();

yuenli
07-24-2007, 10:59 PM
Hi! I am still facing some errors doing the post back and passing the value to server side. I don't know what are those errors from. Anyone can help me? this is my code:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ganttchart.aspx.cs" Inherits="ganttchart" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<script type="text/javascript">
<!--//
function sendform(){
var textbox=new Array("one","two","three");
document.form1.HiddenField1.value = textbox;
document.form1.submit();
}
//-->
</script>
</head>
<body>
<form id="form1" action="ganttchart.aspx" >
<asp:HiddenField ID="HiddenField1" runat="server"/>
<input type="button" name="sendDB" id="sendDB" value="Submit" onclick="send();"/>
<br />
</form>
<asp:Label ID="Label10" runat="server" Width="272px"></asp:Label>
</body>
</asp:Content>

server side code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ganttchart : System.Web.UI.Page
{
protected string postBackStr;
protected void Page_Load(object sender, EventArgs e)
{
postBackStr = Page.ClientScript.GetPostBackEventReference(this, "MyCustomArgument");
if (Page.IsPostBack)
{
string eventArg = Request["__EVENTARGUMENT"];
if (eventArg != null)
{
Response.Write("You got it !");
string [] a;
a = this.HiddenField1.Value;
Label10.Text = a[0];
}
}

}
}

Thanks for helping