0

Hi i am trying to load the following jquery on buttonclick in the code behind, however nothing seems to be happening;

    StringBuilder sb = new StringBuilder();
    sb.Append("$(document).ready(function () {");
    sb.Append("$.gritter.add({");
    sb.Append("title: 'This is a regular notice!',");
    sb.Append("text: 'This will fade out after a certain amount of time. Vivamus eget tincidunt velit. Cum sociis natoque penatibus et <a href='#' style='color:#ccc'>magnis dis parturient</a> montes, nascetur ridiculus mus.',");
    sb.Append("image: 'http://a0.twimg.com/profile_images/59268975/jquery_avatar_bigger.png',");
    sb.Append("sticky: false,");
    sb.Append("time: ''");
    sb.Append("});");
    sb.Append("});");


    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), Guid.NewGuid().ToString(), sb.ToString(), true);

Can anyone see what i am doing wrong?

2
  • Are you getting script errors in your browser? What does this look like if you do a view-source? Commented Apr 25, 2011 at 13:36
  • No i am not getting any errors, nothing appears on source code either. Commented Apr 25, 2011 at 14:06

1 Answer 1

3

Without knowing the context (UpdatePanel? Postback? Why do this from the server at all?) it's hard to say.

You should probably be using RegisterStartupScript instead of RegisterClientScriptBlock, if this happens to be in an UpdatePanel. Otherwise it will never be executed.

Also I can't think of a good reason why this needs to be created from the server in the first place. Put your code in a function that loads with the page, and call the function from a client event (e.g. onclick or better, register an event handler using jQuery) rather than trying to create the entire function and run it after a postback, e.g.

(editied based on comments) -- just set your functions up for either condition. If the only thing different is the title, you don't need two functions, e.g.

<script type="text/javascript">
function addNotice(success) {
    var title = success ? "This is a regular notice!" : "This is a failed notice";
    var text = success? "Success text" : "Failed text";
    $.gritter.add({
        title: title,
        text: text,
        ...
     });
}
</script>

In c#:

bool success; // your update should set a bool somewhere based on whether it 
              // succeeded
Page.ClientScript.RegisterStartupScript("addNotice(" + success.ToString() + ");");
             // will render "addNotice(true)" or "addNotice(false)"

A better way to do this would be with ajax instead of posting back, but that would be a different kettle of fish and this should work with your current architecture.

Sign up to request clarification or add additional context in comments.

3 Comments

The reason it needs to be added server side, is the image that appears is dynamic, also depending on success or failure i want to load a different jquery, does that make sense?
I don't know anything about the gritter plugin, but none of what you've said tells me that it needs to be done on the server. There's no issue with loading an image from client script, or acting conditionally. I would need to see some more context to better respond to that, however..
I am checking in the database is allowed to insert data, if they are i allow them to insert the data, and display a jquery notification. If they are not allowed i display a different version of the notification. i hope i am making sense

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.