Reputation: 1139
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?
Upvotes: 0
Views: 475
Reputation: 24334
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.
Upvotes: 3