I have an asp.net application with a div that triggers a popup window (on click). I need to pop up the window if something happens in my code behind code.
I figured I could send a javascript code block to my page that would click the div, but I can't seem to make it work. Here's the jQuery for the popup plugin:
$(function () {
$('.c_popup').modalPopLite(
{ openButton: '#div_trigger',
closeButton: '#close-btn'
});
});
A simple div triggers it (by clicking the div):
<div id="div_trigger">Trigger</div>
I can click the div from javascript via a button on the page:
<input type="button" id="btn_show_error" value="Popup"
onclick="document.getElementById('div_trigger').click();" />
That works. But when I try to send exactly that javascript block to the page from my code behind, like this:
ClientScript.RegisterStartupScript(Page.GetType(), "error_click",
"<script>document.getElementById('div_trigger').click();</script>")
Nothing happens. I tested my code behind by changing to a simple alert box:
ClientScript.RegisterStartupScript(Page.GetType(), "error_click",
"<script>alert('Hello World');</script>")
And that works fine. So I can see that the javascript is reaching the page from my code behind, and that same javascript block does what I want when it comes from a button on the page itself, but it doesn't work coming from the code behind.
Any ideas would be appreciated. I'm really stuck.
EDIT: Two posters have responded that I'm posting my javascript before the popup control is bound. I'm not sure how to solve this problem, even with the help I've been given. I tried creating this function in my page header block:
function TriggerDiv() {
$(document).ready(function () {
alert('success'); //alert just to check if the function is firing
document.getElementById('div_trigger').click();
});
}
And called it from my code behind like this:
ClientScript.RegisterStartupScript(Page.GetType(), "error_click",
"<script>TriggerDiv();</script>")
The alert fires, but the click event does not - I'm thinking that I'm still not doing things in the right order. Any further help would really be appreciated.