J'ai trois Textboxes
que je ne sais pas comment séparer les uns des autres pour obtenir la bonne valeur. Je crée le Textboxes
dans GetTrix();
et quand j'essaye de mettre à jour le la valeur les les valeurs sont erronées parce qu'il n'y a aucune différence entre elles.Comment séparer différentes zones de texte dans un contrôle?
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);
}
}
Ceci serait une solution logique. Mais travail dosent.
TrixName = (c as TextBox textbox).Text;
Hardness = (c as TextBox textbox2).Text;
Webpage = (c as TextBox textbox3).Text;
Le nouveau code:
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);
}
}
je reçois une erreur: Plusieurs de contrôle avec le même ID (TrixName
) ont été trouvés. FindControl
doivent avoir un identifiant unique.
J'utilise un ID sur le premier: textbox.ID = table.Rows [i] ["TrixID"]. ToString(); Mais comment dose qui fait un diffrens si je ne change pas TrixName = (c en TextBox) .Text; – user1007103
@ user1007103 Je vais mettre à jour ma réponse – ChrisF
Merci, mais je ne peux pas le faire fonctionner, j'ai mis le TrixID dans un litteral à la place. J'ai édité ma question avec mon nouveau code. – user1007103