2

I'm currently working on a sort of Administration method to be able to save certain text and to display this on another page.

Currently, the defaults in the "Admin" page are to be referenced from the other page. It's a simple page:

<asp:TextBox Text="Testing jQuery method" ID="TextBox" runat="server"/>
<asp:CheckBox Checked = "true" ID="CheckBox" runat="server" />  

Now, the page that references these fields is actually a simple HTML file, so I'm trying to use jQuery to reference the fields. I'm trying to use a jQuery ajax call, but I can't seem to get it to work correctly.

<script type="text/javascript" src="Scripts/jquery-1.4.1.js">
var asp_checked;
var asp_text;

try {
    jQuery.ajax({
        url: '~/About.aspx',
        success: function (response, status, xhr) {
            if (status != "error") {
                asp_checked = $('#CheckBox', response).text();
                asp_text = $('#TextBox', response).text();
            }
            else { asp_text = "Error in jQuery Call"; }
        },
        async: false
    });
    document.write(asp_text);
        }
catch (err) {
    document.write("The jQuery is not working");
}
</script>

Things to note:
1. I don't really know much about jQuery in general.
2. I can't use cookies (I don't think). This is going onto our corporate intranet for announcements and such.
3. The jQuery code was taken from SO, but I can't recall the Question reference. When I do I will update here.

3
  • the only way that will work is if About.aspx is generating JSON. Commented Aug 22, 2011 at 15:17
  • @rockinthesixstring: Not true, jQuery has the ability to load the HTML and parse it as a DOM object (and thus grab these elements' values). The real problem, as I see it, is that ASP is probably changing the ID of the element on you, so id="CheckBox" in the ASP code != id="CheckBox once it makes it to HTML. (But you can play with ClientIdMode within the ASP code) Commented Aug 22, 2011 at 15:20
  • It sounds like using HTML5 local storage would help you out! Commented Aug 22, 2011 at 15:27

1 Answer 1

2

Because it's an ASP control, keep in mind that ASP likes to make its own ("relative") ids for controls. Something like <asp:CheckBox id="cb" /> can turn in to <input type="checkbox" id="SomeContainer_foo$cb" /> once it hits the end result page. (This means using $('#cb') is no longer valid because of all the other information now within the control's ID)

Your options?

  1. Change your control to use static ids by setting the ClientIdMode on the control. This makes whatever you use as an ID in the ASP markup transfer to the HTML page.
  2. Change your selector in jQuery to something like this: $('[id$=cb]') ($= means "ends with")
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for elaborating on the meaning of "$" within a jquery selector - I've started to see it crop up in a few places and wasn't familiar with the notation.
@mikemanne: Well, $() is short-hand for using jQuery(). But, the $ within the selector means something else, in this case it's a partial ID match.

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.