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

1

3 Answers 3

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>"
             );
Sign up to request clarification or add additional context in comments.

1 Comment

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");
1

try this

Page.ClientScript.RegisterStartupScript(typeof(String), btnID,"$('.commentsnr').live("click", function () {
$li = $(this).closest('li');
$li.find("#commentload").slideToggle(300);});", True);

1 Comment

you can refer to codeproject.com/KB/aspnet/ClientServer.aspx it may help
-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'

Comments

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.