【问题标题】:To bind Values to Html Table将值绑定到 Html 表
【发布时间】:2015-01-03 18:06:05
【问题描述】:

在下面的代码中,我有一个带有文本框和下拉列表的 html 表,我想将 datset 中的值绑定到 html 表。但它只绑定 1 行,但它有 15 条记录。请帮我这样做。

Asp.net 代码:-

 public string getWhileLoopData()
        {
            GetProduct();
            string htmlStr = "";
            MastersClient objIndent = new MastersClient();
            DataSet ds = objIndent.GetIndent(hidIndentID.Value);

            DataRow[] drIndentID = ds.Tables[0].Select("IndentID =" + hidIndentID.Value);

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                txtQty.Value = drIndentID[i]["RecommentedQuantity"].ToString();
                string Qty = txtQty.Value;
                string strProductID = drIndentID[i]["ProductID"].ToString();
                ddlProduct.Text = strProductID;
                txtDate.Text = drIndentID[i]["ProductRequiredDate"].ToString();
                string date = txtDate.Text;
               Response.Write( htmlStr += "<tr><td>" + Qty + "</td><td>" + strProductID + "</td><td>" + date + "</td></tr>");
            }


            return htmlStr;
        }

HTML 表格:-

  <table id="dataTable" width="350px" border="1" runat="server">
            <tr >
                <td><input id="checkbox" type="checkbox" name="chk" runat="server"/></td>
                <td><input type="text" name="txt" id="txtQty" runat="server"/></td>
                <td>
                <asp:DropDownList ID="ddlProduct" runat="server"  Style="width: 100%; height:23px" ></asp:DropDownList>  


                </td>
               <td>
               <asp:TextBox ID="txtDate" Style="text-align: left" onkeypress="return isNumberKey(event, false);"
                                                            onblur="DateValidation(this)" onkeyup="ValidateDate(this, event.keyCode)" onkeydown="return DateFormat(this, event.keyCode)"
                                                            Height="20px" runat="server" Width="80px"> </asp:TextBox>


               </td>
            </tr>

        </table>

【问题讨论】:

  • 很明显,您的控件(txtQty,txtData,..) 将由 for 循环中的最新行填充,并且 response.write 不会将新行添加到您的 html 表中。
  • @aria 那么我们如何添加新行并为其添加值?
  • 现在查看答案并在此处写下您的评论以完成。
  • 您可以使用gridview并将文本框和下拉列表等模板字段添加到gridview

标签: c# html asp.net


【解决方案1】:

您可以通过 HtmlTableRow 向 HTML 表格添加新行,例如执行以下操作:

        HtmlTableRow row ;
        HtmlTableCell cell;
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            row = new HtmlTableRow();
            cell = new HtmlTableCell();
            cell.Controls.Add(new LiteralControl(drIndentID[i]["RecommentedQuantity"].ToString()));
            row.Cells.Insert(0, cell);

            cell = new HtmlTableCell();
            cell.Controls.Add(new LiteralControl(drIndentID[i]["ProductID"].ToString()));
            row.Cells.Insert(1, cell);

            cell = new HtmlTableCell();
            cell.Controls.Add(new LiteralControl(drIndentID[i]["ProductRequiredDate"].ToString()));
            row.Cells.Insert(2, cell);
            dataTable.Rows.Insert(i, row);//If you want to add new rows after the exist tr use dataTable.Rows.Insert(i+1, row);
        }

Gridview 是用于排序、分页、编辑、删除和格式化的最强大和最有用的控件,您可以在 GridView 中使用下拉菜单、复选框和文本框控件

查看以下链接:

GridView Example1

GridView Example2

GridView Example3

【讨论】:

  • 它不与控件文本框和下拉列表一起绑定
  • 你想如何绑定文本框?
  • 我有 2 个文本框和下拉列表,值应该是绑定文本框和表格中的下拉列表,所以我可以编辑
  • 正如我在 'for' 文本框将由最新的行和上面的 sn-p 代码记录将以编程方式添加到 HTML 表中所提到的,但要进行编辑,您必须执行其他操作,或者您可以使用GridView 很容易使用。
猜你喜欢
  • 2019-01-24
  • 2015-02-28
  • 1970-01-01
  • 2015-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-11
  • 2019-10-29
相关资源
最近更新 更多