2011-10-29 15 views
0

私は正しい値を得るためにお互いに分ける方法がわからない3つのTextboxesを持っています。私はTextboxesGetTrix();に作成し、の値の値に変更しようとすると、違いがないので間違っています。コントロール内の異なるテキストボックスを区切る方法は?

protected void btnUpdate_Click(object sender, EventArgs e) 
{ 
    string TrixName = null; 
    string TrixID = null; 
    string Hardness = null; 
    string Webpage = null; 

    foreach (Control c in phdTrix.Controls) 
    { 
     if (c.GetType() == typeof(TextBox)) 
     { 
      //This is where there is no difference between the textboxes 

      TrixName = (c as TextBox).Text; 
      TrixID = (c as TextBox).ID; 
      Hardness = (c as TextBox).Text; 
      Webpage = (c as TextBox).Text; 

      UpdateTrix(TrixID, TrixName, Hardness, Webpage); 
     } 
    } 
} 

private void GetTrix() 
{ 
    DataTable table = CategoryAccess.GetAllTrix(); 

    for (int i = 0; i < table.Rows.Count; i++) 
    { 
     TextBox textbox = new TextBox(); 
     textbox.ID = table.Rows[i]["TrixID"].ToString(); 
     textbox.Text = table.Rows[i]["TrixName"].ToString(); 

     Literal literal1 = new Literal(); 
     literal1.Text = "<p>"; 

     TextBox textbox2 = new TextBox(); 
     textbox2.Text = table.Rows[i]["Hardness"].ToString(); 

     TextBox textbox3 = new TextBox(); 
     textbox3.Text = table.Rows[i]["Webpage"].ToString(); 

     Literal literal2 = new Literal(); 
     literal2.Text = "</p>"; 

     phdTrix.Controls.Add(literal1); 
     phdTrix.Controls.Add(textbox); 
     phdTrix.Controls.Add(textbox2); 
     phdTrix.Controls.Add(textbox3); 
     phdTrix.Controls.Add(literal2); 
    } 
} 

これはロジックソリューションです。しかし、用量の仕事。

TrixName = (c as TextBox textbox).Text; 
Hardness = (c as TextBox textbox2).Text; 
Webpage = (c as TextBox textbox3).Text; 

新しいコード:

protected void btnUpdate_Click(object sender, EventArgs e) 
{ 
    string TrixID = null; 
    string TrixName = null; 
    string Hardness = null; 
    string Webpage = null; 

    foreach (Control c in phdTrix.Controls) 
    { 
     if (c.GetType() == typeof(TextBox)) 
     { 
      TrixID = (c as Literal).Text; 

      switch (c.ID) 
      { 

       case "TrixName": 
        TrixName = (c as TextBox).Text; 
        break; 
       case "Hardness": 
        Hardness = (c as TextBox).Text; 
        break; 
       case "Webpage": 
        Webpage = (c as TextBox).Text; 
        break; 
      } 

      UpdateTrix(TrixID, TrixName, Hardness, Webpage); 
     } 
    } 
} 



private void GetTrix() 
{ 
    DataTable table = CategoryAccess.GetAllTrix(); 

    for (int i = 0; i < table.Rows.Count; i++) 
    { 
     Literal literalTrixID = new Literal(); 
     literalTrixID.Text = table.Rows[i]["TrixID"].ToString(); 
     literalTrixID.Visible = false; 

     TextBox textbox = new TextBox(); 
     textbox.ID = "TrixName"; 
     textbox.Text = table.Rows[i]["TrixName"].ToString(); 

     Literal literal1 = new Literal(); 
     literal1.Text = "<p>"; 

     TextBox textbox2 = new TextBox(); 
     textbox2.ID = "Hardness"; 
     textbox2.Text = table.Rows[i]["Hardness"].ToString(); 

     TextBox textbox3 = new TextBox(); 
     textbox3.ID = "Webpage"; 
     textbox3.Text = table.Rows[i]["Webpage"].ToString(); 

     Literal literal2 = new Literal(); 
     literal2.Text = "</p>"; 

     phdTrix.Controls.Add(literalTrixID); 
     phdTrix.Controls.Add(literal1); 
     phdTrix.Controls.Add(textbox); 
     phdTrix.Controls.Add(textbox2); 
     phdTrix.Controls.Add(textbox3); 
     phdTrix.Controls.Add(literal2); 
    } 
} 

を私はエラーを取得する:いくつかのコントロールの同じID(TrixName)とが見つかりました。 FindControlには一意のIDが必要です。

答えて

4

まあそこにはありません。

あなたが以下の行っているテキストボックスを見つけコントロールforeach (Control c in phdTrix.Controls)たびをループされています

 TrixName = (c as TextBox).Text; 
     Hardness = (c as TextBox).Text; 
     Webpage = (c as TextBox).Text; 

これは、各変数に同じテキストボックスを割り当てます。

最も簡単な解決策は、作成するときに各テキストボックスに一意の識別子を割り当て、それを使用して、必要なものを特定することです。 IDプロパティを使用できます。あなたの作成コードをする必要があります

string TrixName = null; 
string Hardness = null; 
string Webpage = null; 

foreach (Control c in phdTrix.Controls) 
{ 
    if (c.GetType() == typeof(TextBox)) 
    { 
     switch (c.ID) 
     { 
      case "TrixName": 
       TrixName = (c as TextBox).Text; 
       break; 
      case "Hardness": 
       Hardness = (c as TextBox).Text; 
       break; 
      case "Webpage": 
       Webpage = (c as TextBox).Text; 
       break; 
     } 

     UpdateTrix(TrixID, TrixName, Hardness, Webpage); 
    } 
} 

TextBox textbox = new TextBox(); 
    textbox.ID = "TrixName"; 
    textbox.Text = table.Rows[i]["TrixName"].ToString(); 

    TextBox textbox2 = new TextBox(); 
    textbox2.ID = "Hardness"; 
    textbox2.Text = table.Rows[i]["Hardness"].ToString(); 

    TextBox textbox3 = new TextBox(); 
    textbox3.ID = "Webpage"; 
    textbox3.Text = table.Rows[i]["Webpage"].ToString(); 

あなたが特定の決定論的な方法を持っている必要としてあなたはTrixIDを格納する他の方法が必要になるでしょう、あなたはこのような何かを持つことができますあなたがデータを読むために来たときのコントロール。

ただし、ページにこれらのコントロールのグループが複数ある場合、各IDが一意である必要があるため、これは機能しません。この場合は、あなたがそれらを作成するなどのコントロールにに固有のID(GUID)を追加する必要があります:

textbox.ID = "TrixName_" + Guid.NewGuid().ToString(); 

、あなたはGUIDを取り除くための制御用string.Split()を見つけたいとき:

string[] nameParts = c.ID.Split('_'); 
switch (nameParts[0]) 
{ 
    .... as before. 
} 
+0

私は最初のIDでIDを使用します:textbox.ID = table.Rows [i] ["TrixID"]。ToString();しかし、もし私がTrixName =(TextBoxとしてのc)を変更しないと、どのようにdiffrensを作る線量.Text; – user1007103

+0

@ user1007103私は答えを更新します – ChrisF

+0

ありがとう、しかし私はそれを働かせることはできません私は代わりにlitteralにTrixIDを入れました。私は新しいコードを書いて私の質問を編集しました。 – user1007103

関連する問題