tags:

views:

23

answers:

2

hi, i am getting values from .cs to script but i want to send data from script to .cs page i have tried this,but it is not working.

 <script type="text/javascript">
         $(document).ready(function() {
         $("hfServerValue").val("From ClientSide!");
         });
     </script>

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

 protected void Page_Load(object sender, EventArgs e)
    {
       Response.Write(hfServerValue.Value.ToString ());      
    }
+1  A: 

You can't directly invoke the Page_Load method from script. There are several ways to send data to server.
Try using an XMLHttpReqest

If you're using jQuery, you may also try $.ajax(), $.load() etc.

Do remember that unlike a submit action which internally creates a request, you're trying to create the request yourself so you might need to deal with stuff like request headers (content-type, content-length etc.), content etc. So, there's a bit of work to do here even though you're trying to do a simple thing. But once you get it running it comes naturally.

Sidharth Panwar
Yes. I too think that AJAX would be a good choice here :)
Ranhiru Cooray
+1  A: 

You first need to add a control that get the data from javascript and post them back:

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

Then you place data on that control by getting the cliendID.

$(document).ready(function() { $("<%=hfServerValue.ClientID%>").val("From ClientSide!"); });

And then on post back you get them

protected void Page_Load(object sender, EventArgs e)
{ 
    if(IsPostBack)
    {
        var YourReturn = hfServerValue.Text;

    }   
}

This is an answer to a question. Of course ajax is a different way.

Update

Now I see the hidden field, this is also a better way the hidden field. The only error is that you did not use the CliendID !. Also I do not know if you use prototype or jquery or just microsoft.

Aristos
I tried this,but its not working.
nitin dhage
@nitin Yes its works, search for javascript errors that stop javascript from run. Also this works on postback !.
Aristos
i did not get what is 'divNotice' in "<%=divNotice.ClientID%>".
nitin dhage
why i am getting error for style="display:hidden" and hfServerValue.Text
nitin dhage
@nitin I have update my answer, please see it again. The divNotice was from a previous code, I change it to hfServerValue. Please note that you need to get the clientID on the render code. Search to see what is
Aristos