将值绑定到Html表

时间:2015-01-03 07:20:25

标签: c# html asp.net

在下面的代码中,我有一个带有文本框和下拉列表的html表,我想将数据集中的值绑定到html表。但是它只绑定了1行,但它有15个记录。请帮助我这样做。< / p>

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>

1 个答案:

答案 0 :(得分:0)

您可以按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