2013-02-11 13 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)次にここで私は、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-control-available-somewhere) – MikeM

答えて

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; 
    } 
関連する問題