开发者

sending data from script to .cs page

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="hfServ开发者_如何学编程erValue" runat="server" />

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


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

  • http://www.jibbering.com/2002/4/httprequest.html
  • http://www.openjs.com/articles/ajax_xmlhttp_using_post.php

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜