2014-11-03 113 views
0

這裏是我的代碼來創建動態textboxes..I要插入這些值到數據庫如何插入動態創建文本框的值到數據庫

我需要插入動態創建的文本框值database.how我可以做that..i是新的ASP.NET

標記:

<div> 
    <asp:Panel ID="Panel1" runat="server" Width="300px" /> 
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> 
</div> 

代碼隱藏:

protected void Page_load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     //Remove the session when first time page loads. 
     Session.Remove("clicks"); 
    } 
} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
    int rowCount = 0; 

    //initialize a session. 
    rowCount = Convert.ToInt32(Session["clicks"]); 

    rowCount++; 

    //In each button clic save the numbers into the session. 
    Session["clicks"] = rowCount; 

    //Create the textboxes and labels each time the button is clicked. 
    for (int i = 0; i < rowCount; i++) 
    { 
     TextBox TxtBoxU = new TextBox();   
     Label lblU = new Label();   
     TxtBoxU.ID = "TextBoxU" + i.ToString();    
     lblU.ID = "LabelU" + i.ToString();   
     lblU.Text = "Category Name " + (i + 1).ToString() + " : "; 

     //Add the labels and textboxes to the Panel. 
     Panel1.Controls.Add(lblU); 
     Panel1.Controls.Add(TxtBoxU); 
    } 
} 
+0

你正在使用哪個數據庫,以及如何與它進行通信? – muddymess 2014-11-03 09:35:59

+0

我正在使用MS SQL服務器數據庫.. – 2014-11-03 09:40:04

+0

我的表名稱是Category和字段是CategoryID,CategoryName – 2014-11-03 09:40:36

回答

0

我認爲你無法訪問文本框的值,通過這個你可以很容易地訪問這個值。 和我hompe你知道如何將數據插入數據庫。

for (int i = 0; i < rowCount; i++) 
      { 
       string OptionID = "TextBoxU" + i; 
       TextBox tb = (TextBox)Panel1.FindControl(OptionID); 

      /******** USe the tb.Text value to get 
        the value and then store it into Database**********/ 
      } 
+0

謝謝kumod singh ..我試圖做 – 2014-11-03 10:16:36

+0

我得到了一個錯誤.. 。對象引用沒有設置爲Oblect的實例...請幫助 – 2014-11-03 10:29:43

+0

U必須在每個回發中添加動態控件,以便...在PageLoad中添加控件,然後僅在buttonclick中可訪問它。 – 2014-11-03 10:46:24

相關問題