0

I have a < div > in my aspx page which has some notification messages (like success or error messages)

There is also a button which does some work on server. at the end, I am populating a small asp:repeater inside that div, and calling a javascript function which slides down the div.

Theoretically, when you click the button, the repeater should be populated, and since JS is client side, after server has done all its thing, and at the end of partial postback / pageload, the js should trigger and slide the div down.

But in Reality the div just appears all of a sudden.. its not sliding down smoothly at all. Am I missing something?

the way I am calling js from c# code is like this:

ClientScript.RegisterStartupScript(typeof(Page), "showRQ", "<script language='javascript'> ShowRQ();</script>");

and showRQ is a function with a simple definition:

function ShowRQ() {
            $('.divRQ').slideDown();
            $('.txtExtraInfo').focus();
        }

the weirdest thing is, the text is not being focused too! i added an alert just to make sure the function is being called, and it is. actually, the divRQ is set to display:none so if its visible at all, its because of the slideDown(), but its not sliding Down.. what do I need to do to make it slide down?

Thanks!

iamserious
  • 5,385
  • 12
  • 41
  • 60

1 Answers1

1

Your function is being called before the page loads.
Therefore, your two selectors aren't matching any elements, and the function won't do anything.

To fix this, you can execute the function after the page finishes loading, like this:

<script language='javascript'> $(ShowRQ);</script>

Since ShowRQ is a function, this is equivalent to $(function() { ... }).

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Is that a shorthand version of `$(document).ready(function () {...` ? – Codesleuth Aug 10 '10 at 14:32
  • @Codesleuth: Yes. http://stackoverflow.com/questions/2662778/what-is-difference-of-function-and-document-readyfunction/2662783#2662783 – SLaks Aug 10 '10 at 14:33
  • I just realised, the button is using ajax postback. so its a partial postback.. document ready would not be called at all after postback, unless i am very much mistaken! – iamserious Aug 10 '10 at 14:40
  • You should look at the rendered HTML and use a debugger to figure out where/when to execute the script. – SLaks Aug 10 '10 at 14:41
  • Also, you are mistaken. If the document is already ready, the function will execute immediately. http://dev.jquery.com/browser/trunk/jquery/src/event.js#L707 – SLaks Aug 10 '10 at 14:42
  • ^^ Ok, thanks, i just tried to check the partial postback and response using firebug, and I realized that the function is now not being called at all! i am sure the alert popped up before.. is my client register startup script correct? thanks! – iamserious Aug 10 '10 at 15:08
  • I mean the ClientScript.RegisterStartupScript is not actually calling the function. I dont know why! – iamserious Aug 11 '10 at 09:21