动态创建的表行(jquery)不在页回发上传递值

时间:2014-01-20 19:46:41

标签: c# jquery asp.net

我已经搜索过这个问题,虽然这似乎是一个常见的问题但我无法找到适合我情况的解决方案。 我有一个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" />

2 个答案:

答案 0 :(得分:0)

对此的简短回答是,由于控件是在客户端创建的,当您返回服务器时,它不知道如何映射表单值。您可以通过Request.Form对象获取值,如下所示:

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

答案 1 :(得分:0)

  • 在asp.net后面的代码中创建一个隐藏字段。
  • 将表序列化为字段
  • 访问后面代码中的序列化代码并再次解析

来源: http://forums.asp.net/t/1668471.aspx