Reputation: 425
I have three Textboxes
that I don't know how to separate from each others to get the correct value. I Create the Textboxes
in GetTrix();
and when I try to update the value the values are wrong because there's no difference between them.
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);
}
}
This would be a logic solution. But dosent work.
TrixName = (c as TextBox textbox).Text;
Hardness = (c as TextBox textbox2).Text;
Webpage = (c as TextBox textbox3).Text;
The new 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);
}
}
I get an error: Several control's with the same ID (TrixName
) were found. FindControl
have to have a Unique ID.
Upvotes: 0
Views: 284
Reputation: 137158
Well there won't be.
You are looping over the controls foreach (Control c in phdTrix.Controls)
and each time you find a text box you are doing the following:
TrixName = (c as TextBox).Text;
Hardness = (c as TextBox).Text;
Webpage = (c as TextBox).Text;
This will assign the same text box to each variable.
The simplest solution is to assign a unique identifier to each text box when you create it and then use that to determine which one you want. You can use the ID
property. Then you can have something like this:
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);
}
}
Your creation code needs to be:
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();
You'll need some other way of storing the TrixID
as you have to have a deterministic way of identifying the controls when you come to read the data out.
However, if you have several groups of these controls on a page this isn't going to work as each ID needs to be unique. In this case you'll need to append a unique id (GUID) on to the controls as you create them:
textbox.ID = "TrixName_" + Guid.NewGuid().ToString();
then when you want to find the control use string.Split()
to strip off the GUID:
string[] nameParts = c.ID.Split('_');
switch (nameParts[0])
{
.... as before.
}
Upvotes: 4