2014-02-28 32 views
0

我做了一個表格,其中有兩個字段,Name and Products。在產品旁邊,我帶了一個文本框和一個按鈕。我允許我的用戶添加多個文本框,限制爲5個文本框。現在我將這個表單中的數據插入我的SQL Database。而現在我想從表中獲取數據並顯示到相應的文本框中。我希望在單擊「顯示所有按鈕」時,所有動態文本框都應顯示在窗體上。如果該數據存在於該特定字段的數據庫中,那麼它應該在這些動態textbox中顯示爲文本。如何從數據庫獲取數據以在點擊按鈕時顯示動態創建的文本框?

我曾嘗試做這個 -

<div> 
    <table border="1" width="1000px"> 
    <tr><td colspan="2" align="center"><b>Inserting Data Into Table</b></td></tr> 
    <tr> 
    <td class="style1">Name: </td> 
    <td class="style2"> 
     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td> 
    </tr> 
    <tr> 
    <td class="style1">Add Text Box: </td> 
    <td class="style2"> 
     <asp:TextBox ID="txt1" runat="server"></asp:TextBox> 
     <asp:Button ID="Button1" runat="server" Text="Add More" 
      onclick="Button1_Click" /><br /> 
     <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>  
     </td> 
    </tr> 
    <tr><td colspan="2" align="center"> 
     <asp:Button ID="Button2" runat="server" Text="Submit" onclick="Button2_Click" /> 
     <br /> 
     </td></tr> 
    </table> 
    </div><br /><br /> 
    <div> 
    <table border="1" width="1000px"> 
    <tr><td colspan="2" align="center"><b>Fetching Data And Showing into Textbox</b></td></tr> 
    <tr> 
    <td class="style1">Name: </td> 
    <td class="style2"> 
     <asp:TextBox ID="txtname" runat="server"></asp:TextBox></td> 
    </tr> 
    <tr> 
    <td class="style1">Add Text Box: </td> 
    <td class="style2"> 
     <asp:TextBox ID="txtt1" runat="server"></asp:TextBox> 
     <asp:Button ID="Button3" runat="server" Text="Show All" 
      onclick="Button3_Click" /><br /> 
     <asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder>  
     </td> 
    </tr> 
    <tr><td colspan="2" align="center"> 
     <asp:Button ID="Updt" runat="server" Text="Update" onclick="Updt_Click" /> 
     <br /> 
     </td></tr> 
    </table> 
    </div> 

CS頁: -

General_Logic g1 = new General_Logic(); 
DataTable dt = new DataTable(); 
int rows = 0; 
List<string> ControlIdList = new List<string>(); 
int Counter = 1; 
TextBox tb = new TextBox(); 
protected override void LoadViewState(object SavedState) 
{ 
    base.LoadViewState(SavedState); 
    ControlIdList = (List<string>)ViewState["ControlIdList"]; 
    foreach (string Id in ControlIdList) 
    { 
     Counter++; 
     TextBox tb = new TextBox(); 
     tb.ID = Id; 
     LiteralControl linebreak = new LiteralControl(); 
     PlaceHolder1.Controls.Add(tb); 
     PlaceHolder1.Controls.Add(linebreak); 
    } 
} 
protected void Page_Load(object sender, EventArgs e) 
{ 
    show(); 
} 
public void show() 
{ 
    dt = g1.return_dt("select product1,product2,product3,product4,product5,name from tbl_products where name='Yo Yo'"); 
    if (dt.Rows.Count > 0) 
    { 
     txtname.Text = dt.Rows[0]["name"].ToString(); 
     txtt1.Text = dt.Rows[0]["product1"].ToString(); 
     //TextBox txtb; 
     int x = 2; 
     foreach (Control ctrl in PlaceHolder2.Controls) 
     { 
      if (ctrl is TextBox) 
      { 
       if (x <= 5) 
       { 
        if (Counter <= 4) 
        { 
         Counter++; 
         tb.ID = "TextBox" + Counter; 
         //tb.Text = tb.ID; 
         tb = (TextBox)ctrl; 
         LiteralControl linebreak = new LiteralControl("<br />"); 
         tb.Text = dt.Rows[0]["product'" + x + "'"].ToString(); 
         PlaceHolder2.Controls.Add(tb); 
         PlaceHolder2.Controls.Add(linebreak); 
         ControlIdList.Add(tb.ID); 
         ViewState["ControlIdList"] = ControlIdList; 
         x++; 
        } 
        //txtb = (TextBox)ctrl; 
        //LiteralControl linebreak = new LiteralControl("<br />"); 
        //txtb.Text = dt.Rows[0]["product'" + x + "'"].ToString(); 
        //PlaceHolder2.Controls.Add(txtb); 
        //PlaceHolder2.Controls.Add(linebreak); 
        //x++; 
       } 
      } 

     } 
    } 
} 
protected void Button1_Click(object sender, EventArgs e) 
{ 
    if (Counter <= 4) 
    { 
     Counter++; 
     tb.ID = "TextBox" + Counter; 
     tb.Text = tb.ID; 
     LiteralControl linebreak = new LiteralControl("<br />"); 
     PlaceHolder1.Controls.Add(tb); 
     PlaceHolder1.Controls.Add(linebreak); 
     ControlIdList.Add(tb.ID); 
     ViewState["ControlIdList"] = ControlIdList; 
    } 
    else 
    { 
     Button1.OnClientClick = null; 
     Response.Write("<script>alert('Maximum Entry is 5');</script>"); 
    } 
} 
protected void Button2_Click(object sender, EventArgs e) 
{ 
     int limit = 4; 
     string[] DBVALUES = new string[5]; 
     for (int parcount = 0; parcount<=limit; parcount++) 
     { 
      if (parcount == 0) 
      { 
       DBVALUES[parcount] = txt1.Text; 
      } 
      else 
      { 
       DBVALUES[parcount] = Request.Form["TextBox" + (parcount + 1).ToString()]; 
      } 
     } 
     for (int i = 0; i <= 4; i++) 
     { 
      if (DBVALUES[i] == null) 
      { 
       DBVALUES[i] = "NULL"; 
      } 
     } 
     rows = g1.ExecDB("insert into tbl_products(product1,product2,product3,product4,product5,name) values('" + DBVALUES[0].ToString() + "','" + DBVALUES[1].ToString() + "','" + DBVALUES[2].ToString() + "','" + DBVALUES[3].ToString() + "','" + DBVALUES[4].ToString() + "','"+TextBox1.Text.ToString()+"')"); 
     TextBox1.Text = string.Empty; 
     txt1.Text = string.Empty; 
     TextBox txtb; 
     foreach (Control ctrl in PlaceHolder1.Controls) 
     { 
      if (ctrl is TextBox) 
      { 
       txtb = (TextBox)ctrl; 
       txtb.Text = string.Empty; 
      } 
     } 
    Response.Write("<script>alert('Data Inserted!!!');</script>"); 
} 
protected void Updt_Click(object sender, System.EventArgs e) 
{ 

} 
protected void Button3_Click(object sender, System.EventArgs e) 
{ 
    show(); 
} 

請指引我哪裏做錯了。我正在等待你的所有建議。

+0

根據你的代碼,你所面臨的問題你能找到動態創建的文本框嗎? – Dotnet

+0

@Dotnet實際上,在第二個div中,我無法通過單擊Show All按鈕來獲取動態創建的文本框。在這裏,我希望在點擊顯示時,所有動態創建的文本框應該與來自數據庫的值一起顯示。 而在第一個分區中,我可以在單擊添加更多按鈕時創建文本框。而且我可以將值插入到我的表的各個屬性中。 – Omi

+0

根據你的代碼'Omi'我已經看到你沒有將控件添加到第二個佔位符,只是調試並檢查你的'Show'方法是否進入這個條件按鈕點擊'foreach(控制Ctrl在PlaceHolder2 .Controls){' – Dotnet

回答

1

我想你需要修改你的節目像這種方法有內佔位符的控制,如果你的datatable是通過所有的行返回你需要循環多行獲得的個人

public void show() 
{ 
dt = g1.return_dt("select product1,product2,product3,product4,product5,name from tbl_products where name='Yo Yo'"); 
if (dt.Rows.Count > 0) 
{ 
    txtname.Text = dt.Rows[0]["name"].ToString(); 
    txtt1.Text = dt.Rows[0]["product1"].ToString(); 
    //TextBox txtb; 
    int x = 2; 
      if (x <= 5) 
      { 
       if (Counter <= 4) 
       { 
        TextBox tb = new TextBox(); 
        Counter++; 
        tb.ID = "TextBox" + Counter; 
        //tb.Text = tb.ID; 
        LiteralControl linebreak = new LiteralControl("<br />"); 
        tb.Text = dt.Rows[0]["product'" + x + "'"].ToString(); 
        PlaceHolder2.Controls.Add(tb); 
        PlaceHolder2.Controls.Add(linebreak); 
        ControlIdList.Add(tb.ID); 
        ViewState["ControlIdList"] = ControlIdList; 
        x++; 
       } 
       //txtb = (TextBox)ctrl; 
       //LiteralControl linebreak = new LiteralControl("<br />"); 
       //txtb.Text = dt.Rows[0]["product'" + x + "'"].ToString(); 
       //PlaceHolder2.Controls.Add(txtb); 
       //PlaceHolder2.Controls.Add(linebreak); 
       //x++; 
      } 
     } 

    } 

數據如果它是固定的管制不應多於4試試這個代碼,而你對showall點擊與DataTable行更換,按您的要求

protected void Button3_Click(object sender, EventArgs e) 
    { 
     createControls(); 
    } 

private void createControls() 
    { 
     PlaceHolder2.Controls.Clear(); 
     for (int i = 0; i < 4; i++) 
     { 
      TextBox tb = new TextBox(); 
      tb.ID = "TextBox" + i; 
      tb.Text = tb.ID; 
      LiteralControl linebreak = new LiteralControl("<br />"); 
      PlaceHolder2.Controls.Add(tb); 
      PlaceHolder2.Controls.Add(linebreak); 
     } 
    } 

我創建了一個datatab樂按您的要求檢查這個

private void createControls() 
    { 
     PlaceHolder2.Controls.Clear(); 
     DataTable dt = Session["Table"] as DataTable; 
     for (int i = 1; i < 5; i++) 
     { 
      int cnt = 0; 
      cnt = i; 
      TextBox tb = new TextBox(); 
      cnt = cnt + 1; 
      tb.ID = "TextBox" + i; 
      tb.Text = dt.Rows[0]["Product" + cnt + ""].ToString(); 
      LiteralControl linebreak = new LiteralControl("<br />"); 
      PlaceHolder2.Controls.Add(tb); 
      PlaceHolder2.Controls.Add(linebreak); 
     } 
    } 

    private void assignValues() 
    { 
     DataTable dt = new DataTable(); 
     dt.Columns.Add("Product2", typeof(string)); 
     dt.Columns.Add("Product3", typeof(string)); 
     dt.Columns.Add("Product4", typeof(string)); 
     dt.Columns.Add("Product5", typeof(string)); 

     DataRow lrow = dt.NewRow(); 
     lrow["Product2"] = "ABC"; 
     lrow["Product3"] = "DEF"; 
     lrow["Product4"] = "GHI"; 
     lrow["Product5"] = "JKL"; 
     dt.Rows.Add(lrow); 
     Session["Table"] = dt; 
    } 

protected void Button3_Click(object sender, EventArgs e) 
    { 
     assignValues(); 
     createControls(); 
    } 

這裏是O/P按我的數據表上點擊SHOWALL

PageLoad

ShowAll

+0

我討論已取代我的代碼,現在我得到這個異常 'System.ArgumentException:列'產品'2''不屬於表' 我在這一行中得到錯誤- 'tb.Text = dt.Rows [0 ] [「product'」+ x +「'」] .ToString();' 先生有什麼問題? – Omi

+0

'tb.Text = dt.Rows [0] [ 「產品 '」 + X + 「'」]的ToString();'什麼'x'這裏 – Dotnet

+0

其實我有像這 - 產品1在我的表的屬性,產品2 。 。 etc 這就是爲什麼我將x的值與if語句中的屬性名稱連接起來的原因。 – Omi

相關問題