javascript - asp.net passing values from JSjquery to code behind c# - Stack Overflow

I have tried "every" possible way of sending the screen.width vlaue from the JS script on the

I have tried "every" possible way of sending the screen.width vlaue from the JS script on the aspx page to the c# in the code behind and while I can see the screen.width being assigned correctly it never gets assigned to my hidden field value.

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

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

   <script type="text/javascript" language="javascript">
       $(function(){ 
       $('#hiddenfield').val(screen.width); 
    });
    </script>

other content

</asp:Content>

and the code behind:

protected void btnChartGo_Click(object sender, EventArgs e)
{
   string s = hiddenfield.Value;
}

No matter what I try s is always ""

Something wrong with the above, everyone seems to be doing it like that and it works?

I have tried "every" possible way of sending the screen.width vlaue from the JS script on the aspx page to the c# in the code behind and while I can see the screen.width being assigned correctly it never gets assigned to my hidden field value.

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

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

   <script type="text/javascript" language="javascript">
       $(function(){ 
       $('#hiddenfield').val(screen.width); 
    });
    </script>

other content

</asp:Content>

and the code behind:

protected void btnChartGo_Click(object sender, EventArgs e)
{
   string s = hiddenfield.Value;
}

No matter what I try s is always ""

Something wrong with the above, everyone seems to be doing it like that and it works?

Share Improve this question asked Jun 20, 2012 at 8:12 user1468537user1468537 7035 gold badges13 silver badges27 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

The ID of the rendered hidden field isn't "hiddenfield" - it'll be something like ctl00_bodycontent_hiddenfield.

Try using

$('[id$="hiddenfield"]') 

as the selector instead.

<asp:HiddenField ID="hiddenfield" runat="server" ClientIDMode="Static">
</asp:HiddenField >

Make sure client ID mode of your hidden field is static if you are using ASP.NET 4 or use

$('#<%= hiddenfield.ClientID %>').val(screen.width); 

This should get the right selector:

$('#<%= hiddenfield.ClientID %>').val(screen.width); 

Check the view source of the page and find out proper id of the element and then use jquery selector over it and then at the page load check for request.form collection to check if hidden variable is ing in post request or not

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745339082a4623241.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信