我有三个Textboxes
,我不知道如何分开彼此以获得正确的值。我在Textboxes
中创建GetTrix();
,当我尝试更新值时,值是错误的,因为它们之间没有区别。
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。
答案 0 :(得分: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();
然后当你想找到控件时使用string.Split()
去除GUID:
string[] nameParts = c.ID.Split('_');
switch (nameParts[0])
{
.... as before.
}