2015-01-03 66 views
0

在下面的代碼中,我有文本框和下拉列表中的HTML表格,我想從datset值綁定到HTML table.But它只是綁定1行,但它有15個records.Pls幫助我這樣做。將值綁定到HTML表格

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> 
+0

這清楚地表明你的控件(txtQty,txtData,..)將被最新的行中填寫的循環和response.w禮儀不添加新行到您的HTML表格。 – Aria

+0

@aria所以我們如何添加新行併爲其添加值? – Developer

+0

現在看到答案,並在那裏寫下你的評論來完成。 – Aria

回答

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

+0

它不與控件綁定文本框和下拉 – Developer

+0

你想如何綁定文本框? – Aria

+0

我有2個文本框,下拉列表中的值應綁定表中的文本框,下拉所以我可以編輯 – Developer