I am using multiple user control in my web page. E开发者_运维问答ach of these usercontrol has $(document).ready() method. Because i am using an update panel, i am binding all the events again in end_request event. But I dont want to do that in all my usercontrols. Is is possible to do this at a common place(only once)?
thanks, syd
There are many interesting ways to do what you want. The simplest one is:
- make your ascx control's ClientIdMode to static
- in master page add call to init function which will handle init for all the ascx controls
for example, if you have controls a1.ascx and b1.ascx having wrapped in
<div id="divA1">ascx markup ... </div>
and same is b1.ascx you can write something like:
var site = {
init:function(){
if($('#divA1').length > 0)
{
// bind events for the a1.ascx here
}
if($('#divB1').length >0)
{
//bind events for a2
}
}
};
$(function(){
site.init();
});
--- update ----
if you are using asp.net 3.5 you can try following:
var site={
ascx1_init:function(clientId){
$('#'+clientId+'_Button1').live('click',function(){//button click code goes here});
},
ascx2_init:function(clientId){
$('#'+clientId+'_Button1').live('click',function(){//button click code goes here});
},
....
}
in each of your ascx, add following link at the end of page_load function:
Me.Page.ClientScript.RegisterStartupScript(Me.GetType(),"_"+Me.ClientID,"site.ascx1_init('" & Me.ClientID & "');",True)
I'm assuming you are using vb.net. So this way, you can access all your asp.net server controls in js and you can maintain single js without problem
精彩评论