views:

358

answers:

3

What are some good jQuery Resources along with some gotchas when using it with ASP.Net?

+5  A: 

ASP.Net's autogenerated id's make using jQuery's selector syntax somewhat difficult.

Two easy ways around this problem:

  • Search for objects using css class instead of id
  • You can weed out the uniqueid garbage with: $('[id$=myid]')
Adam Lassek
Thanks for the note about $('[id$=myid]'). I can finally get rid of my "uniqueid garbage"!
EndangeredMassa
WOW! I didn't know what [id$=myId] would do, so I looked it up in [the documentation](http://docs.jquery.com/Selectors/attributeEndsWith#attributevalue). Matching the end, so simple! Bet it uses Regex. You could probably enhance it further by always prefacing with '_'. THANKS! You get a gold star!
Atømix
Awesome tip Adam!
L. De Leo
+1  A: 

Rick Strahl's Blog is a good place to start. He has quite a few jQuery posts.

Brian Schmitt
+3  A: 

One thing to note is that if you use WebMethods for Ajax, the response values will be returned wrapped in an object named 'd' for security reasons. You will have to unwrap that value, which is usually not a problem, unless you are using a component (such as the jqGrid plugin) that relies upon jquery ajax. To get around that, I just changed the code in the grid that called ajax and inserted a bit of code to unwrap. I do plan on sending in some code to the jquery crew to see if it can be accepted for future versions.

The next thing, as was mentioned previously, is the ids. If you have the time and inclination, I actually subclassed all of the HTML controls to make participating in the NamingContainer optional, like this:

protected override void RenderAttributes(HtmlTextWriter writer) {
 HtmlControlImpl.RenderAttributes(this, writer);
}

And then the helper object (to prevent writing the same code in each object) looks like this:

public static void RenderAttributes(IFormControl cntrl, HtmlTextWriter writer) {
 if (cntrl.ID != null) {
  cntrl.Attributes.Remove("id");
  cntrl.Attributes.Remove("name");
  writer.WriteAttribute("id", cntrl.RenderedId);
  writer.WriteAttribute("name", cntrl.RenderedName);
 }
 cntrl.Attributes.Render(writer);
 HtmlContainerControl containerCntrl = cntrl as HtmlContainerControl;
 if (containerCntrl == null)
  writer.Write(" /");
}

public static string GetRenderedId(IFormControl cntrl) {
 return cntrl.UseNamingContainer ? cntrl.ClientID : cntrl.ID;
}

public static string GetRenderedName(IFormControl cntrl) {
 return cntrl.UseNamingContainer ? cntrl.UniqueID : cntrl.ID;
}
Travis