2013-02-11 15 views
6

我正在創建一個調查網站。我想動態添加文本框,然後在數據庫中獲取它們的值。如何使用ASP.NET動態創建文本框,然後將其值保存在數據庫中?

現在我們假設從下拉列表中選擇4個文本框以便動態顯示。

代碼上下拉列表中選擇:

 protected void NumDropDown_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (DropDownList1.SelectedValue == "TextBox") 
     { 

      int j; 
      i = int.Parse(NumDropDown.SelectedValue); 
      Session["i"] = i; 
      switch (i) 
      { 
       case 1: 
        t = new TextBox[i]; 
        Session["textBox"] = t; 
        for (j = 0; j < i; j++) 
        { 
         t[j] = new TextBox(); 
         t[j].ID = "txtCheckbox" + j.ToString(); 
         Panel1.Controls.Add(t[j]); 

        } 
        break; 
       case 2: 
        t = new TextBox[i]; 
        Session["textBox"] = t; 
        for (j = 0; j < i; j++) 
        { 
         t[j] = new TextBox(); 
         t[j].ID = "txtCheckbox" + j.ToString(); 
         Panel1.Controls.Add(t[j]); 

        } 
        break; 

       case 3: 
        t = new TextBox[i]; 
        Session["textBox"] = t; 
        for (j = 0; j < i; j++) 
        { 
         t[j] = new TextBox(); 
         t[j].ID = "txtCheckbox" + j.ToString(); 
         Panel1.Controls.Add(t[j]); 

        } 
        break; 
       case 4: 
        t = new TextBox[i]; 
        List<TextBox> MyTextBoxes; 
        for (j = 0; j < i; j++) 
        { 
         t[j] = new TextBox(); 
         t[j].ID = "txtCheckbox" + j.ToString(); 
         Panel1.Controls.Add(t[j]); 
         try 
         { 
          MyTextBoxes = (List<TextBox>)Session["AddedTextBox"]; 
          MyTextBoxes.Add(t[j]); 
          Session["AddedTextBox"] = MyTextBoxes; 
         } 
         catch 
         { 
          MyTextBoxes = new List<TextBox>(); 
          MyTextBoxes.Add(t[j]); 
          Session["AddedTextBox"] = MyTextBoxes; 
         } 
        } 
        break; 
      } 

     } 
    } 

2)然後在這裏,我像A,B,C,d文本框中輸入的值,然後單擊添加:

代碼在點擊Add點擊:

1)首先我檢查到那裏Page_Init會議:

protected void Page_Init(object sender, EventArgs e) 
    { 
     if (Session["AddedTextBox"] != null) 
     { 
      string a; 
      string b; 
      string c; 
      string d; 

      int listCount = ((List<TextBox>)Session["AddedTextBox"]).Count; 
      foreach (TextBox t in ((List<TextBox>)Session["AddedTextBox"])) 
      { 
       if (listCount == 1) 
       { 

       } 
       if (listCount == 2) 
       { 

       } 
       if (listCount == 3) 
       { 

       } 
       if (listCount == 4) 
       { 
        if (t.ID == "txtCheckbox0") 
        { 
         a = t.Text; 
        } 
        if (t.ID == "txtCheckbox0") 
        { 
         b = t.Text; 
        } 
        if (t.ID == "txtCheckbox0") 
        { 
         c = t.Text; 
        } 
        if (t.ID == "txtCheckbox0") 
        { 
         d = t.Text; 
        } 

       } 
      } 
     } 

但是,這裏的問題是我沒有得到文本值,它們似乎是空的。 請幫我解決這個問題。

+0

你可以從這裏提到的方式中獲益:是否有可用textboxlist控制的地方?(http://stackoverflow.com/questions/9070327/is-there-a -textboxlist控制,可供地方) – MikeM 2013-02-11 16:52:24

回答

3

這聽起來像是asp.net向頁面動態添加控件的古老經典問題。

問題是視圖狀態和回發控件的重建。

你需要運行在頁面生命週期正確的時間,以確保回傳值匹配的服務器端控件生成回發控件相同的代碼。

當然,如果你想有一個哈克快捷方式,訪問Request.Form["textCheckbox" + index]值直接

A useful article on the subject.

1

正如@簡森鍵式活動中提到你可以通過Request.Form這裏訪問TextBox值是一個例子:

ASPX:

<asp:DropDownList ID="ddlTextBoxes" runat="server"> 
    <asp:ListItem Value="1" Text="1" /> 
    <asp:ListItem Value="5" Text="5" /> 
</asp:DropDownList> 
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Add" /><br /> 
<asp:PlaceHolder ID="container" runat="server" Visible="false"> 
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="Submit" /> 
</asp:PlaceHolder> 

後面的代碼:

protected void Add(object sender, EventArgs e) 
    { 
     int numOfTxt = Convert.ToInt32(ddlTextBoxes.SelectedItem.Value); 
     var table = new Table(); 

     for (int i = 0; i < numOfTxt; i++) 
     { 
      var row = new TableRow(); 
      var cell = new TableCell(); 

      TextBox textbox = new TextBox(); 
      textbox.ID = "Textbox" + i; 
      textbox.Width = new Unit(180); 

      cell.Controls.Add(textbox); 
      row.Cells.Add(cell); 
      table.Rows.Add(row); 
     } 

     container.Controls.AddAt(0,table); 
     container.Visible = true; 
    } 

    protected void Submit(object sender, EventArgs e) 
    { 
     var textboxValues = new List<string>(); 
     if (Request.Form.HasKeys()) 
     { 
      Request.Form.AllKeys.Where(i => i.Contains("Textbox")).ToList().ForEach(i => 
       { 
        textboxValues.Add(Request.Form[i]); 
       }); 
     } 

     //Do something with the textbox values 
     textboxValues.ForEach(i => Response.Write(i + "<br />")); 
     container.Visible = false; 
    } 
相關問題