8

Hi i have a jquery function which executes when a button is clicked, i also need to execute this function from my code behind based on whether an Item has a comment attached to it. Here is the jquery

  //Comments Slide
$('.commentsnr').live("click", function () {
    // up to parent li
    $li = $(this).closest('li');
    $li.find("#commentload").slideToggle(300);
});

How do i call this from my code behind, thanks alot

pmillio
  • 1,089
  • 2
  • 13
  • 20
  • possible duplicate of [Call jQuery function from ASP.NET code behind C#](http://stackoverflow.com/questions/2397165/call-jquery-function-from-asp-net-code-behind-c) – C. Ross May 30 '11 at 16:57

3 Answers3

15

You can do this, but it will only be executed when the page is delivered or you receive a Postback.

See ClientScriptManager.RegisterStartupScript for documentation.

string jquery = "$('.commentsnr').live(\"click\", function () {$li = $(this).closest('li');$li.find(\"#commentload\").slideToggle(300);});"

ClientScript.RegisterStartupScript(typeof(Page), "a key", 
             "<script type=\"text/javascript\">"+ jquery +"</script>"
             );
anton.burger
  • 5,637
  • 32
  • 48
Simon Woker
  • 4,994
  • 1
  • 27
  • 41
  • 1
    Thanks that worked, i didnt use it in the end i forgot i could change the style attribute to show the div. cahrefReply.Attributes.Add("style", "display:block"); – pmillio May 30 '11 at 17:16
1

try this

Page.ClientScript.RegisterStartupScript(typeof(String), btnID,"$('.commentsnr').live("click", function () {
$li = $(this).closest('li');
$li.find("#commentload").slideToggle(300);});", True);
Amir Ismail
  • 3,865
  • 3
  • 20
  • 33
-1

Not sure exactly what you are asking, but I'm guessing its just how to manually call this method..?

$(selector).click();

The problem you will have is that its not an ID, its a class. So in my example you'd have to update the selector to find the exact commentsnr that you want to 'click'

Rabbott
  • 4,282
  • 1
  • 30
  • 53