1

I have an <asp:menu/> control and a hidden field.Now i am using jQuery to change value of hidden field. Code is:-

$(function() {

    $(".primaryStaticMenu  tr,td").each(function(index) {

        $(this).click(function() {

            if ($(this).attr("title") != "undefined"
                && $(this).attr("title").length > 0) {

                document.getElementById('ctl00_Hidden_Master_Location').value = $(this).attr("title");

                alert(document.getElementById('ctl00_Hidden_Master_Location').value);
                //return false;
            }
        });
    });
});

Server side code to get updated value is:-

string Get_cng_value = Hidden_Master_Location.Value;

But Hidden_Master_Location.Value shows null every time. can any one tell me how to get updated value of hidden field from code behind.

7
  • 2
    please provide aspx page code for hidden field Commented Mar 5, 2013 at 6:26
  • what this line prompts in the browser? alert(document.getElementById('ctl00_Hidden_Master_Location').value); Commented Mar 5, 2013 at 6:27
  • i'm new to jquery but the id of the hidden field is ctl00_Hidden_Master_Location, shouldn't that be used to refer it in your server code instead of Hidden_Master_Location.Value? Commented Mar 5, 2013 at 6:28
  • In addition to server side code show how you submit data to server (that portion of script/HTML is missing from your question). Commented Mar 5, 2013 at 6:30
  • try: document.getElementById(<%=Hidden_Master_Location.ClientId%>.value); Commented Mar 5, 2013 at 6:37

2 Answers 2

2

Let say your hidden field is as..

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

you can get the value of hidden filed in jquery as

var locationValue= $("#<%= Hidden_Master_Location.ClientID %>").val();
Sign up to request clarification or add additional context in comments.

3 Comments

It works fine in client side i need to access updated value from server side code.And i am not able to get that value.
@ashish,Make sure your hidden value not getting cleared on page load, post-back .
I were place this code on form load but the value is null. Code :- String Get_cng_value = Hidden_Master_Location.Value;
0

Do this, it works for me.the trick is to save your hidden field precious id in another hidden input field then build it back using that hidden value.

Markup

<asp:HiddenField ID="HiddenFieldMaster" runat="server" />
   <input type="hidden" id="inputHidden" value='<%= HiddenFieldMaster.ClientID%>' />

Javascript

    $(function() {

$(".primaryStaticMenu  tr,td").each(function(index) {

    $(this).click(function() {

        if ($(this).attr("title") != "undefined"
            && $(this).attr("title").length > 0) {

           var inputHidden = document.getElementById('inputHidden');
                $("#" + inputHidden.value).val($(this).attr("title"));

            alert(inputHidden.value);
            //return false;
        }
    });
});
 });

Code Behind

String Get_cng_value = HiddenFieldMaster.Value;

4 Comments

Sorry to say but it is not working. Hidden field working properly on simple aspx page. But here my master page contains this hidden field and on master page load event the hidden field value goes cleared.And i am not able to access that value.
Sorry to hear that.You have to investigate what is happening in your page life cycle.Try a textbox with a hidden css class and see if also its value is being wiped out.
I tried with text-box control but the problem is still there. The value of text-box being wiped out on page load.
Zip up the offending files and drop box them so that I can have a look.Zip up the master and its child form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.