none
服务器端为什么读取不到aspx页面中标记为runat=“server”的table中的所有行? RRS feed

  • 问题

  •      table中的初始为:

                       <table cellpadding="0" cellspacing="0" border="0"  width="100%" id="purchaseOrderListTable" runat="server">
                               <thead>
                                    <tr>                              
                                        <th style="text-align:left;">
                                            商品编号
                                        </th>                       
                                        <th style="text-align:left;">
                                            商品名称
                                        </th>
                                        <th style="text-align:left;">
                                            计价单位
                                        </th>
                                        <th style="text-align:left;">
                                            单价
                                        </th>
                                        <th style="text-align:left;">
                                            数量
                                        </th>
                                         <th style="text-align:left;">
                                            金额
                                        </th>                                  
                                        <th style="text-align:left;">
                                            操作
                                        </th>
                                    </tr>
                                    </thead>
                                 <tbody>                
                                </tbody>
                            </table>

    通过js操作 向其中添加了行:

    其中单价和数量都是放在input中的,并且在提交前可以修改。

    现在在服务器端获取table中的值:

                HtmlTable = this.purchaseOrderListTable;
                for (int i = 0; i < table.Rows.Count; i++)
                {
                        string orderId= table.Rows[i].Cells[0].InnerText.Trim();
                }

    为什么只能获取表头行的数据,不能获取js添加的行数据??  求助高手,万分感谢。

    1.要求不要用ajax动态更新行数据,因为在提交前,单价和数量都是可以修改的。如果用ajax动态提交了数据,而后用户不提交,则会产生垃圾数据的。

    2.能否不用在提交前在客户端循环table中的行,把所需要的值保存在hidden中在提交。 我想就在服务器端的方法取得。

     


    2011年7月15日 15:57

答案

  • 楼主你好:)

    原因很简单——尽管你使用js动态添加行。但是当你点击submit按钮的时候,由于动态添加的脚本不会回传到服务器端,结果一个来回之后服务器端的Table重新生成。

    尝试我的示例代码:
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplicationTest.WebForm2" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.min.js"></script>
        <script>
            $(function () {

                $("#Button1").click(function () {
                    $("#HiddenField1").val($("#HiddenField1").val() + $("#TextBox1").val() + ",");
                    alert($("#HiddenField1").val());

                });

            })
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <table cellpadding="0" cellspacing="0" border="0" width="100%" id="purchaseOrderListTable"
                runat="server">
                    <tr>
                        <th style="text-align: left;">
                            商品编号
                        </th>
                    </tr>
              
            </table>
        </div>
        <br />
        输入商品Id,点击Button添加到表格中:
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        <asp:HiddenField ID="HiddenField1" runat="server" Value="" />
        </form>
    </body>
    </html>

    namespace WebApplicationTest
    {
        public partial class WebForm2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                //服务器端获取HiddenField内容
                if (Request["HiddenField1"] != null)
                {
                    string[] Ids = Request["HiddenField1"].ToString().Split(',');
                    foreach (string item in Ids)
                    {
                        HtmlTableRow row = new HtmlTableRow();
                        row.Cells.Add(new HtmlTableCell());
                        row.Cells[0].InnerText = item;
                        purchaseOrderListTable.Rows.Add(row);
                    }

                    //移除最后一行
                    purchaseOrderListTable.Rows.RemoveAt(purchaseOrderListTable.Rows.Count - 1);
                }
            }

            protected void Button1_Click(object sender, EventArgs e)
            {
              
            }
        }
    }


    你有其它意见或私下交流,请直接发送maledong_work@foxmail.com;或者讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处

    2011年7月16日 5:10

全部回复

  • 楼主你好:)

    原因很简单——尽管你使用js动态添加行。但是当你点击submit按钮的时候,由于动态添加的脚本不会回传到服务器端,结果一个来回之后服务器端的Table重新生成。

    尝试我的示例代码:
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplicationTest.WebForm2" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.min.js"></script>
        <script>
            $(function () {

                $("#Button1").click(function () {
                    $("#HiddenField1").val($("#HiddenField1").val() + $("#TextBox1").val() + ",");
                    alert($("#HiddenField1").val());

                });

            })
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <table cellpadding="0" cellspacing="0" border="0" width="100%" id="purchaseOrderListTable"
                runat="server">
                    <tr>
                        <th style="text-align: left;">
                            商品编号
                        </th>
                    </tr>
              
            </table>
        </div>
        <br />
        输入商品Id,点击Button添加到表格中:
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        <asp:HiddenField ID="HiddenField1" runat="server" Value="" />
        </form>
    </body>
    </html>

    namespace WebApplicationTest
    {
        public partial class WebForm2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                //服务器端获取HiddenField内容
                if (Request["HiddenField1"] != null)
                {
                    string[] Ids = Request["HiddenField1"].ToString().Split(',');
                    foreach (string item in Ids)
                    {
                        HtmlTableRow row = new HtmlTableRow();
                        row.Cells.Add(new HtmlTableCell());
                        row.Cells[0].InnerText = item;
                        purchaseOrderListTable.Rows.Add(row);
                    }

                    //移除最后一行
                    purchaseOrderListTable.Rows.RemoveAt(purchaseOrderListTable.Rows.Count - 1);
                }
            }

            protected void Button1_Click(object sender, EventArgs e)
            {
              
            }
        }
    }


    你有其它意见或私下交流,请直接发送maledong_work@foxmail.com;或者讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处

    2011年7月16日 5:10
  • 谢谢  wei_dong。此问题已解决。

    2011年7月16日 7:03