[英]dynamically created table rows (jquery) not passing values on page postback
我已經搜索了這個問題,盡管這似乎是一個常見問題,但我仍無法找到適合我情況的解決方案。 我有一個asp:table控件,在頁面上有一行。 jQuery腳本會在按鈕單擊時添加一個具有唯一ID的新行。 我的問題是,在回發以將值添加到數據庫時,回發后不存在其他行,只是初始行。 所以我的問題是我如何保留動態創建的行中的數據,以便可以將其傳遞給背后的代碼。
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" />
簡短的答案是,由於控件是在客戶端創建的,當您返回服務器時,它不知道如何映射表單值。 您可以通過Request.Form對象獲取值,如下所示:
string value = Request.Form["formElementName"];
資料來源: http : //forums.asp.net/t/1668471.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.