2014-01-15 76 views
0

我只想問是否可以使用Javascript克隆asp控件,因爲我發現很難添加另一行,與之前的行相同..如何使用Javascript克隆ASP.NET控件

這裏是JavaScript:

<script type="text/javascript"> 
    function deleteRow(row) { 
     var i = row.parentNode.parentNode.rowIndex; 
     document.getElementById('POITable').deleteRow(i); 
    } 
    function myFunction() 
     { 
     var table = document.getElementById("POITable"); 
     var row = table.insertRow(2); 
     var cell1 = row.insertCell(0); 
     var cell2 = row.insertCell(1); 
     var cell3 = row.insertCell(2); 
     var cell4 = row.insertCell(3); 
     var cell5 = row.insertCell(4); 
     var cell6 = row.insertCell(5); 
     cell1.innerHTML = row.cells[1].getElementsByTagName('#asp:DropDownList')[0]; 
     cell2.innerHTML = row.cells[2].getElementsByTagName('#asp:DropDownList')[0]; 
     cell3.innerHTML = row.cells[3].getElementsByTagName('#asp:TextBox')[0]; 
     cell4.innerHTML = row.cells[4].getElementsByTagName('#asp:TextBox')[0]; 
     cell5.innerHTML = row.cells[5].getElementsByTagName('#asp:TextBox')[0]; 
     cell6.innerHTML = row.cells[6].getElementsByTagName('#asp:Label')[0]; 
     } 
     </script> 

這裏是表,其中我要克隆行:

<div id="POItablediv"> 
      <table class="table" id="POITable"> 
      <tr class="th"> 
       <td>Fabric</td> 
       <td>Color</td> 
       <td>Good For How Many</td> 
       <td>Consumption</td> 
       <td>Allowance</td> 
       <td>MRP Quantity</td> 
      </tr> 
      <tr> 
       <td> <asp:DropDownList ID="ddlFab" runat="server" DataSourceID="DSFabric" 
           DataTextField="Description" DataValueField="Description" Width="120px"> 
           </asp:DropDownList></td> 
       <td><asp:DropDownList ID="ddlColor" runat="server" DataSourceID="ddlColors" 
           DataTextField="Description" DataValueField="Description" Width="120px"> 
           </asp:DropDownList></td> 
       <td><asp:TextBox ID="txtGdForHwMany" runat="server" Width="120px" 
         ontextchanged="txtGdForHwMany_TextChanged" ></asp:TextBox></td> 
       <td><asp:TextBox ID="txtConsump" runat="server" Width="120px" 
         ontextchanged="txtConsump_TextChanged"></asp:TextBox></td> 
       <td><asp:TextBox ID="txtAllow" runat="server" Width="120px" 
         ontextchanged="txtAllow_TextChanged"></asp:TextBox></td> 
       <td><asp:Label ID="lblMRPQuantity" runat="server" Text="0" Width="120px"></asp:Label></td> 
        <td><input type="button" id="delPOIbutton" value="Delete" onclick="deleteRow(this)"/></td> 
        <td><input type="button" id="addmorePOIbutton" value="Add More POIs" onclick="myFunction()"/></td> 
      </tr> 
      </table> 
     </div> 

DropDownList是databounded。

回答

1
  1. 下拉在客戶端將被轉換成「<選擇/>」,所以getElementByTagName不會爲ASP工作:DropDownList的。你將不得不使用像getElementByTagName(「選擇」),或者如果你正在使用jQuery,那麼你可以給一個類名下拉,該類名稱,如$有選擇(「類名」),以獲得下拉列表。

  2. 爲< ASP類似:文本框/>標記將是<輸入/>或使用類名它

  3. 爲< ASP:標籤/>,標籤將是<跨度/>或使用類名因爲它

希望這有助於!

0

這不會工作,你所期望的方式,一開始asp.net呈現控件的HTML。這是asp:dropdownlist成爲<select>...</select>asp:textbox<input type="text/>等時呈現的頁面。

此外,如果您成功克隆了該行,您將爲表單元素重複使用nameid屬性。

的另外的問題是新的元件不會被暴露於頁後面的代碼作爲一個正常asp.net元件會。該會使用Request對象是可訪問的,但你仍然有重複的name屬性的問題。

+0

有沒有什麼辦法,我可以在表中添加新行,我可以繼承我的表中的ASP控制? – jelliaes