2009-12-04 49 views
0

我想將一些.NET託管控件加載到頁面中動態使用JavaScript將OBJECT標籤插入到HTML中。如何實例化通過JavaScript動態加載的.NET託管控件?

我的問題是對象沒有實例化,只有頁面加載時可見的靜態對象纔會出現。有沒有辦法讓這些對象加載?

 

<h1> 
    Visible static</h1> 
    <div id="staticObject"> 
    <object id="Object1" name="dropfolder" classid="http:DropFolder.dll#DropFolder.DropFolder"> 
     <param name="FolderName" value="Intranet only - Worcs"> 
     <param name="NodeKey" value="default|Intranet"> 
     <param name="DropAction" value=""> 
    </object> 
    </div> 
    <h2> 
    Hidden static</h2> 
    <div id="objwrap" style="display: none;"> 
    <object id="default_Intranet" name="dropfolder" classid="http:DropFolder.dll#DropFolder.DropFolder"> 
     <param name="FolderName" value="Intranet only - Worcs"> 
     <param name="NodeKey" value="default|Intranet"> 
     <param name="DropAction" value=""> 
    </object> 
    </div> 
    <input type="button" value="unhide!" onclick="document.getElementById('objwrap').style.display = 'block';" /> 
    <h2> 
    OnLoad target</h2> 
    <div id="objectTarget1"> 
    </div> 
    <h2> 
    Click target</h2> 
    <div id="objectTarget2"> 
    </div> 
    <input type="button" value="inject" onclick="javascript:inject('objectTarget2');" /> 

    <script type="text/javascript"> 
    function inject(target) { 
     var target = document.getElementById(target); 
     target.innerHTML = 
     '<object id="default_Intranet" name="dropfolder" classid="http:DropFolder.dll#DropFolder.DropFolder">' + 
     '<param name="FolderName" value="Intranet only - Worcs">' + 
     '<param name="NodeKey" value="default|Intranet">' + 
     '<param name="DropAction" value="">' + 
     '</object>'; 
    }; 

    inject('objectTarget1'); 
    </script> 
 

回答

0

你可以嘗試:

function inject(target) { 
    var obj = document.createElement("object"); 

    obj.id = "default_Intranet"; 
    obj.name = "dropFolder"; 
    obj.classid = "http:DropFolder.dll#DropFolder.DropFolder"; 

    var param1 = document.createElement("param"); 
    param1.name = "FolderName"; 
    param1.value = "Intranet only - Worcs"; 
    obj.appendChild(param1); 
    // repeat for other params 

    document.getElementById(target).appendChild(obj); 
} 
+0

我想我試過,一個在某一點,解決辦法是注入的對象標記,然後在DOM一旦設置的classid。 – gum411 2010-02-01 09:41:30