简体   繁体   中英

How to access master page control in child .aspx page (in javascript)

In my masterpage.cs i am assigning a value to a hidden field ( hfSession="abc"; )

In my content page (default.aspx) i need to access this value in my javascript:

<script type="text/javascript" language="javascript">
    function(sender, e) 
    {   
        var Sessioninfo= $get('<%= ((Hiddenfield)this.Master.FindControl("ct100_hfSession")).ClientID %>');
</script> 

but here i get an error :

object reference not set to an instance of an object

I use this little javascript util function:

// This function returns a control's (mangled) client id given its ASP.NET Id
function GetClientId(strid) {
var count = document.forms[0].length;
var i = 0;
var eleName;
for (i = 0; i < count; i++) {
    eleName = document.forms[0].elements[i].id;
    pos = eleName.indexOf(strid);
    if (pos >= 0) break;
}
return '#' + eleName;
}

采用,

Sessioninfo= $get('<%= ((Hiddenfield)this.Master.FindControl("hfSession")).ClientID %>');          

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM