2016-11-28 7 views
0

ASP Panelを使用してボタンクリック時にテキストボックスを動的に作成しています。テキストボックスが作成されButton1の私は上をクリックしてから、私は、テキストボックスに値を入力し、ボタン2をクリックした後、私は使用していたコードは、以下のようにasp:Panelを使用したテキストボックスのASP.NET動的作成FindControlを使用したテキストボックスへのアクセス

<asp:TextBox ID="text_number" runat="server"></asp:TextBox> 
<asp:Button ID="button1" runat="server" OnClick="button1_Click"></asp:Button> 
<asp:Panel ID="mypanel" runat="server"></asp:Panel> 
<asp:Button ID="button2" runat="server" OnClick="button2_Click"></asp:Button> 


protected void button1_Click(object sender, EventArgs e) 
{ 
    int n = Convert.ToInt32(text_number.Text); 
    for (int i = 0; i < n; i++) 
    { 
     TextBox MyTextBox = new TextBox(); 
     MyTextBox.ID = "newtext_" + (i + 1); 
     mypanel.Controls.Add(MyTextBox); 
     MyTextBox.CssClass = "textblock"; 
     Literal lit = new Literal(); 
     lit.Text = "<br />"; 
     mypanel.Controls.Add(lit); 
    } 
} 

です。 button2をクリックすると、テキストボックスからのすべての値が読み込まれ、C#の後ろのリストに格納されます。私が使用していたコードは、以下のように

protected void button2_Click(object sender, EventArgs e) 
{ 
    int n = Convert.ToInt32(text_number.Text); 
    for (int i = 0; i < n; i++) 
    { 
     TextBox control = (TextBox) FindControl("newtext_" + (i+1)); 
     mylist.Add(control.Text); 
    } 
} 

ですが、私はボタン2をクリックしたときに、私がパネルに追加されたすべてのテキストボックスには、Webページから消えても、私はにFindControlにnullオブジェクト参照エラーが発生します。ボタン2を押すとパネル内のテキストボックスコントロールがすべて消去され、これがWebページで消える理由とnullオブジェクトエラーが発生する理由がわかります。ここで何が問題なの?ボタンのクリック時に 'n'個のテキストボックスを動的に作成し、2番目のボタンクリックで値を取得する方法はありますか?

答えて

0

コントロールを動的に作成するため、すべてのページ読み込み時にコントロールを再作成する必要があります。そうしないと、コントロールとその内容が失われます。

テキストボックスの数をSessionまたはViewStateに保存して再作成する必要があります。

protected void Page_Load(object sender, EventArgs e) 
{ 
    //check if the viewstate exists and if so, build the controls 
    if (ViewState["boxCount"] != null) 
    { 
     buildControls(Convert.ToInt32(ViewState["boxCount"])); 
    } 
} 

protected void button1_Click(object sender, EventArgs e) 
{ 
    //adding controls moved outside the button click in it's own method 
    try 
    { 
     buildControls(Convert.ToInt32(text_number.Text)); 
    } 
    catch 
    { 
    } 
} 

protected void button2_Click(object sender, EventArgs e) 
{ 
    //check if the viewstate exists and if so, build the controls 
    if (ViewState["boxCount"] != null) 
    { 
     int n = Convert.ToInt32(ViewState["boxCount"]); 

     //loop al controls count stored in the viewstate 
     for (int i = 0; i < n; i++) 
     { 
      TextBox control = FindControl("newtext_" + i) as TextBox; 
      mylist.Add(control.Text); 
     } 
    } 
} 

private void buildControls(int n) 
{ 
    //clear the panel of old controls 
    mypanel.Controls.Clear(); 

    for (int i = 0; i < n; i++) 
    { 
     TextBox MyTextBox = new TextBox(); 
     MyTextBox.ID = "newtext_" + i; 
     MyTextBox.CssClass = "textblock"; 
     mypanel.Controls.Add(MyTextBox); 

     Literal lit = new Literal(); 
     lit.Text = "<br />"; 
     mypanel.Controls.Add(lit); 
    } 

    //save the control count into a viewstate 
    ViewState["boxCount"] = n; 
} 
関連する問題