2014-01-20 34 views
0

我已經搜索了這個問題,雖然它似乎是一個常見的問題,我一直無法找到我的情況的解決方案。 我有一個頁面上的一行asp:表格控件。一個jQuery腳本在按鈕點擊時添加一個具有唯一ID的新行。我的問題是,在回發到將數據添加到數據庫時,附加行在回發後不存在,只是最初的一行。 所以我的問題是我如何保存動態創建的行中的數據,以便它可以傳遞給代碼隱藏。動態創建的錶行(jQuery)不傳遞頁面回發值

jQuery函數

function addNewRow() 
    { 
     var i = 1; 
     $("#<%=addRow.ClientID %>").click(function() { 
      $("#<%=tblAddItems.ClientID %> tr").eq(1).clone().find("input").each(function() { 
       $(this).attr({ 
        'id': function (_, id) { return id + i }, 
        'name': function (_, name) { return name + i }, 
        'value': '' 
       }); 

       // $(this).val('').attr('id', function (_, id) { return id + i }); 
       //}).end().appendTo("#<=tblAddItems.ClientID %>"); 
       // i++; 
      }).end().appendTo("#<%=tblAddItems.ClientID %>"); 
      SearchItemNumber(); 
      i++; 
     }); 

     } 

的asp.net表

<asp:Table EnableViewState="true" runat="server" ID="tblAddItems" HorizontalAlign="Center" > 
       <asp:TableHeaderRow BackColor="#5C9CCC" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" 
     VerticalAlign="Middle" TableSection="TableHeader" Width="975px"> 
        <asp:TableHeaderCell style="display:none"> 
         <asp:Label ID="Label13" runat="server" Text="Item Id"></asp:Label> 

        </asp:TableHeaderCell> 

        <asp:TableHeaderCell > 
         <asp:Label ID="Label1" runat="server" Text="Item Number"></asp:Label> 

        </asp:TableHeaderCell> 

        <asp:TableHeaderCell > 
         <asp:Label ID="Label2" runat="server" Text="Description"></asp:Label> 

        </asp:TableHeaderCell> 

        <asp:TableHeaderCell > 
         <asp:Label ID="Label3" runat="server" Text="UOM"></asp:Label> 

        </asp:TableHeaderCell> 

        <asp:TableHeaderCell > 
         <asp:Label ID="Label4" runat="server" Text="MPN"></asp:Label> 

        </asp:TableHeaderCell> 

        <asp:TableHeaderCell > 
         <asp:Label ID="Label5" runat="server" Text="Average Price"></asp:Label> 

        </asp:TableHeaderCell> 

       </asp:TableHeaderRow> 
       <asp:TableRow TableSection="TableBody"> 

         <asp:TableCell style="display:none"> 
         <asp:TextBox runat="server" ID="hdnItmDet" Height="12px" Width="150px" ></asp:TextBox> 
        </asp:TableCell> 

        <asp:TableCell style="display:none"> 
         <asp:TextBox runat="server" ID="tbItmId" Height="12px" Width="150px" ></asp:TextBox> 
        </asp:TableCell> 

        <asp:TableCell> 
         <asp:TextBox runat="server" ID="tbItmNo" Height="12px" Width="150px" CssClass="autosuggestItemDetails"></asp:TextBox> 
        </asp:TableCell> 

        <asp:TableCell> 
         <asp:TextBox runat="server" ID="tbDescription" Height="12px" Width="150px" ></asp:TextBox> 
        </asp:TableCell> 

        <asp:TableCell> 
         <asp:TextBox runat="server" ID="tbMPN" Height="12px" Width="150px" ></asp:TextBox> 
        </asp:TableCell> 

         <asp:TableCell> 
         <asp:TextBox runat="server" ID="tbUOM" Height="12px" Width="150px" ></asp:TextBox> 
        </asp:TableCell> 

        <asp:TableCell> 
         <asp:TextBox runat="server" ID="tbAvPrice" Height="12px" Width="150px" ></asp:TextBox> 
        </asp:TableCell> 


         <asp:TableCell style="display:none"> 
         <asp:TextBox runat="server" ID="hdnChanged" Height="12px" Width="150px" ></asp:TextBox> 
        </asp:TableCell> 

         <asp:TableCell style="display:none"> 
         <asp:TextBox runat="server" ID="hdnFlag1" Height="12px" Width="150px" ></asp:TextBox> 
        </asp:TableCell>    

       </asp:TableRow> 

     <asp:TableFooterRow TableSection="TableFooter"> 
      <asp:TableCell ColumnSpan="1"> 
       <button runat="server" ID="addRow" type="button">Add Row</button> 
      </asp:TableCell> 
      <asp:TableCell ColumnSpan="2"> 
       <asp:Button ID="btnSave" runat="server" Text="Add Items to List" OnClick="btnSave_Click" /> 
      </asp:TableCell> 
     </asp:TableFooterRow> 
      </asp:Table> 

和按鈕

<asp:Button ID="saveList" runat="server" Text="Save Changes to List" OnClick="saveList_Click" /> 
+0

這將有助於見下表HTML,並添加了jQuery文檔 – Jonesopolis

+0

你使用webforms嗎? – Becuzz

+0

@Jonesy謝謝你的時間我用代碼更新了問題。和Becuzz是的,我使用webforms。 – user2359298

回答

0

簡短的回答這是由於對照創建客戶機當你回到服務器時,它不知道如何映射表單值。您可以通過像這樣的Request.Form對象得到的值:

string value = Request.Form["formElementName"]; 
+0

這似乎是解決方案,但我仍然有一些問題的值,因爲字符串保持爲空。在調試時,我可以看到Request.Form [tbItmNo.ClientID]包含表值,但它沒有被分配給字符串。 – user2359298

+0

我想通了,我不得不使用確切的客戶端ID來工作,謝謝你的幫助。 – user2359298