开发者

Can I define a function in embeded javascript file of a asp.net server control to access Page Load event?

开发者 https://www.devze.com 2023-02-07 01:48 出处:网络
I have designded a asp.net server control that have an embeded javascript file. document.getElementById(\"div_Messages_Back\").style.left = 0;

I have designded a asp.net server control that have an embeded javascript file.

document.getElementById("div_Messages_Back").style.left = 0;
document.getElementById("div_Messages_Back").style.top = 0;
document.getElementById("div_Messages_Back").style.width = document.documentElement.clientWidth;
document.getElementById("div开发者_开发技巧_Messages_Back").style.height = document.documentElement.clientHeight;
document.getElementById("tbl_Messages").style.left = (document.documentElement.clientWidth - 250) / 2;
document.getElementById("tbl_Messages").style.top = (document.documentElement.clientHeight - 120) / 2;
function btn_Close() {
    document.getElementById("div_Messages_Back").style.visibility = "collapse";
    document.getElementById("tbl_Messages").style.visibility = "collapse";
}

I want the first 6 line of my javascript file run at runtime. But I recieve error at runtime: Error: Object Required.


Try this:

window.onload= function(){
    var divMessagesBack = document.getElementById("div_Messages_Back");
    divMessagesBack.style.left = 0;
    divMessagesBack.style.top = 0;
    divMessagesBack.style.width = document.documentElement.clientWidth;
    divMessagesBack.style.height = document.documentElement.clientHeight;
    document.getElementById("tbl_Messages").style.left = (document.documentElement.clientWidth - 250) / 2;
    document.getElementById("tbl_Messages").style.top = (document.documentElement.clientHeight - 120) / 2;
}

function btn_Close() {
    document.getElementById("div_Messages_Back").style.visibility = "collapse";
    document.getElementById("tbl_Messages").style.visibility = "collapse";
}

P.S: Since it is an ASP.Net control, the ID's of the controls might get changed once it renders as HTML. Hence add the code to render the ClientID's of the controls in the JS to avoid any further issues with the elements/object not found.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号