Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
553 views
in Technique[技术] by (71.8m points)

asp.net - Get the Value of an asp:HiddenField using jQuery

I have two pages. From the first page, I open a modal with a querystring that holds that value of a client name. I then use this to set a hiddenfield on the modal that opened.

I need a TextBox on the new modal to display the value that has been sent through from the first screen.

I've tried getting the value using:

var hv = $('hidClientField').val();`

But this doesn't seem to work.

This is my hidden field:

<asp:HiddenField ID="hidClientName" runat="server" />`

I set it in the code behind on the Page_Load like this:

hidClientName.Value = Request.QueryString["Client_Name"] ?? "";`

Any ideas will be much appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try any of the following

  1. If ASP.Net control and javascript both are on same page, then use

    var hv = $("#"+ '<%= hidClientField.ClientID %>').val();
    
  2. If you want to access the control from some JS file, then

    // 'id$' will cause jQuery to search control whose ID ends with 'hidClientField'
    var hv = $('input[id$=hidClientField]').val();
    
  3. You can use class name selector to achieve same. Check out this similar question.

In asp.net, controls id is mangled. Because of this your code is not working.

Hope this works for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...