Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call jquery from code behind

Tags:

jquery

c#

asp.net

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

like image 968
pmillio Avatar asked May 30 '11 16:05

pmillio


2 Answers

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>"
             );
like image 188
Simon Woker Avatar answered Nov 18 '22 14:11

Simon Woker


try this

Page.ClientScript.RegisterStartupScript(typeof(String), btnID,"$('.commentsnr').live("click", function () {
$li = $(this).closest('li');
$li.find("#commentload").slideToggle(300);});", True);
like image 25
Amir Ismail Avatar answered Nov 18 '22 14:11

Amir Ismail