2012-04-17 9 views
0

私は、ユーザーが入力しなければならないパラメータ値の数に応じてテキストボックスを作成するメソッドを持っています。問題は、ユーザーが[OK]をクリックしたときに、ユーザーがこれらのテキストボックスに入力した値を取り込み、文字列に置き換えることです。これらのテキストボックスを検索してテキスト値を取得しているときに、そのテキストボックスを見つけられません。プロジェクトを継続するためにこれらの値を取得するにはどうすればよいですか?コードはコードの他の部分に値を動的に追加するにはどうすればいいですか?

protected void btnPreview_Click(object sender, EventArgs e) 
    { 
     lbHeader.Text = "Template Designer"; 
     divQueryBuilder.Visible = false; 
     divTemplateDesigner.Visible = false; 
     divFloating.Visible = true; 

     if (txtQuery.Text.Contains("WHERE") || txtQuery.Text.Contains("where")||txtQuery.Text.Contains("Where")) 
     { 
      string[] splitter=new string[10]; 
      splitter[0]="Where"; 
      splitter[1]="WHERE"; 
      splitter[2]="where"; 
      splitter[3]="AND"; 
      splitter[4] = "and"; 
      splitter[5] = "And"; 
      string[] condition=txtQuery.Text.Split(splitter, StringSplitOptions.None); 
      int numberOfParameters = condition.Length - 1; 
      string[] Condition1 =null; 
      Label[] newLabel = new Label[10]; 
      TextBox[] txtBox = new TextBox[10]; 
      for (int i = 0; i < numberOfParameters; i++) 
      { 
       string lbValue="lbValue"; 
       string lbID=lbValue+i; 
       string txtValue = "txtValue"; 
       string txtID = txtValue + i; 
       HtmlGenericControl genericControl = new HtmlGenericControl("br/"); 
       Condition1 = condition[i + 1].Split('[', ']'); 
       newLabel[i] = new Label(); 
       txtBox[i] = new TextBox(); 
       newLabel[i].ID=lbID; 
       newLabel[i].Text = Condition1[1]; 
       txtBox[i].ID = txtID; 

       td2.Controls.Add(newLabel[i]); 
       td2.Controls.Add(genericControl); 
       td2.Controls.Add(txtBox[i]); 
       td2.Controls.Add(genericControl); 
       txtBox[i].EnableViewState = true; 
      } 
     } 
    } 

private bool ControlExistence(string lbID) 
    { 
     try 
     { 
      td2.FindControl(lbID); 
      return true; 
     } 
     catch(Exception ex) 
     { 
      return false; 
     } 
    } 

    protected void btnOk_Click(object sender, EventArgs e) 
    { 
     // GetTextBoxValues(); 
     string[] splitter1 = new string[10]; 
     splitter1[0] = "Where"; 
     splitter1[1] = "WHERE"; 
     splitter1[2] = "where"; 
     splitter1[3] = "AND"; 
     splitter1[4] = "and"; 
     splitter1[5] = "And"; 
     string[] condition = txtQuery.Text.Split(splitter1, StringSplitOptions.None); 
     int numberOfParameters = condition.Length - 1; 
     string[] splitter = new string[4]; 
     splitter[0] = "["; 
     splitter[1] = "]"; 
     splitter[2] = "'"; 

     string[] queryBoxValue = txtQuery.Text.Split(splitter,StringSplitOptions.RemoveEmptyEntries); 
     StringBuilder query = new StringBuilder(); 
     TextBox txtBox = new TextBox(); 

     for (int i = 0; i < queryBoxValue.Length; i++) 
     { 
      if (!queryBoxValue[i].Contains("?")) 
      { 
       query.Append(queryBoxValue[i]); 
       query.Append(" "); 
      } 
      else 
      { 
       for (int counter1 = 0; counter1 < numberOfParameters; counter1++) 
       { 
        string txtValue = "txtValue"; 
        string txtID = txtValue + counter1; 
        if (ControlExistence(txtID)) 
        { 
         TextBox box = (TextBox)td2.FindControl(txtID); 
         string b = box.Text; 
         //txtBox.ID = txtID; 
        } 
        txtBox.Text = "'" + txtBox.Text + "'"; 
        queryBoxValue[i] = queryBoxValue[i].Replace(queryBoxValue[i], txtBox.Text); 
        query.Append(queryBoxValue[i]); 
       } 

      } 
     } 
     string fireQuery = query.ToString(); 
     adp = new SqlDataAdapter(fireQuery, conn); 
     tab = new DataTable(); 
     adp.Fill(tab); 
     if (tab.Rows.Count > 0) 
     { 
      string[] tempString = txtTemplate.Text.Split(' '); 
      for (int j = 0; j < tempString.Length; j++) 
      { 
       if (tempString[j].StartsWith("[")) 
       { 
        txtPreview.Text = txtTemplate.Text.Replace(tempString[j], tab.Rows[0][0].ToString()); 
       } 
      } 
     } 
     divFloating.Visible = false; 
     divQueryBuilder.Visible = false; 
     divTemplateDesigner.Visible = true; 
    } 

助けてください。これは私の邪魔者になっています。

+0

IDが実際に使用されているものをあなたの生成されたHTMLのソースコードを見てみましょう。私はあなたが古いid - ingスタイルか予測可能であると思います。 – walther

答えて

0

追加されたコントロールの一覧があり、必要なときに使用します。

 for (int i = 0; i < numberOfParameters; i++) 
     { 
      // ... 

      td2.Controls.Add(newLabel[i]); 
      td2.Controls.Add(genericControl); 
      td2.Controls.Add(txtBox[i]); 
      td2.Controls.Add(genericControl); 

      addedTextBoxes.Add(txtBox[i]); 

      // ... 
     } 

そして、あなたのコードの別の部分から:

var values = addedTextBoxes.Select(tb => tb.Text).ToList(); 

foreach (var txtBox in addedTextBoxes) 
{ 
    // ... 
} 

等...

+0

オートポストバックが発生した場合、nullに設定しないでください。 – Apoorva

+0

ポストバックがある場合、リストに保存する代わりに、paramsをリクエストに追加することができます。あなたはそれらを呼び出すことができます( "txtBox" + i) – SimpleVar

+0

あなたの提案を得ることができません。あなたは詳しく教えてもらえますか? – Apoorva

関連する問題