用增量int进行加法
本文关键字:int | 更新日期: 2023-09-27 18:08:10
我有一个独特的文本框id的问题。我的代码是创建一个div与4个文本框在我的数据库中的每艘船。每个创建的文本框必须具有唯一的ID。我这样做与增量int (int I = 0和i++),但似乎不能得到它的工作。这是我的代码!
while (oReader.Read());
{
int i = 0;
System.Web.UI.HtmlControls.HtmlGenericControl createDiv =
new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
createDiv.ID = "createDiv";
this.Controls.Add(createDiv);
List<TextBox> tb_names = new List<TextBox>();
TextBox tb_name = new TextBox();
//TextBox tb_name = new TextBox();
tb_name.ID = "CreateT_" + i.ToString() + "_1";
tb_name.Text = oReader["SchipNaam"].ToString();
createDiv.Controls.Add(new LiteralControl
("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> Scheepspnaam: <input type='text' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
tb_names.Add(tb_name);
//TextBox tb_name1 = new TextBox();
tb_name.ID = "CreateT_" + i.ToString() + "_1";
tb_name.Text = oReader["RederijNr"].ToString();
createDiv.Controls.Add(new LiteralControl
("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> RederijNr:<input type='text' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
tb_names.Add(tb_name);
//TextBox tb_name2 = new TextBox();
tb_name.ID = "CreateT_" + i.ToString() + "_1";
tb_name.Text = oReader["Lengte"].ToString();
createDiv.Controls.Add(new LiteralControl
("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> Lengte :<input type='text' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
tb_names.Add(tb_name);
//TextBox tb_name3 = new TextBox();
tb_name.ID = "CreateT_" + i.ToString() + "_1";
tb_name.Text = oReader["Laadvermogen"].ToString();
createDiv.Controls.Add(new LiteralControl
("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> Laadvermogen:<input type='text' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
tb_names.Add(tb_name);
i++;
}
我的输出总是CreateT_0_1。我做错了什么?我认为它以前工作,但似乎找不到问题。
我希望你们看到我做错了什么。
提前感谢!
在每个while循环开始时创建值为0的i。在循环之前初始化整数,然后在循环中更新它,否则在每次迭代时它将重置为0。
在while循环中添加3个文本框,但只在while循环结束时增加i。您需要更改后缀,因为这始终是_1,或者在每个文本框之间使用i++以使标识符唯一。
正如Jon Skeet在上面的注释中提到的,不需要while起始行上的分号。
int i = 0;
while (oReader.Read())
{
System.Web.UI.HtmlControls.HtmlGenericControl createDiv =
new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
createDiv.ID = "createDiv";
this.Controls.Add(createDiv);
List<TextBox> tb_names = new List<TextBox>();
TextBox tb_name = new TextBox();
//TextBox tb_name = new TextBox();
tb_name.ID = "CreateT_" + i.ToString();
tb_name.Text = oReader["SchipNaam"].ToString();
createDiv.Controls.Add(new LiteralControl
("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> Scheepspnaam: <input type='text' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
tb_names.Add(tb_name);
//TextBox tb_name1 = new TextBox();
tb_name.ID = "CreateT_" + i.ToString() + "_1";
tb_name.Text = oReader["RederijNr"].ToString();
createDiv.Controls.Add(new LiteralControl
("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> RederijNr:<input type='text' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
tb_names.Add(tb_name);
//TextBox tb_name2 = new TextBox();
tb_name.ID = "CreateT_" + i.ToString() + "_2";
tb_name.Text = oReader["Lengte"].ToString();
createDiv.Controls.Add(new LiteralControl
("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> Lengte :<input type='text' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
tb_names.Add(tb_name);
//TextBox tb_name3 = new TextBox();
tb_name.ID = "CreateT_" + i.ToString() + "_3";
tb_name.Text = oReader["Laadvermogen"].ToString();
createDiv.Controls.Add(new LiteralControl
("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> Laadvermogen:<input type='text' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
tb_names.Add(tb_name);
i++;
}
您已经将1作为该行中的固定字符串tb_name。ID = "CreateT_" + i.ToString() + "_1";所以它不起作用试试下面的代码
while (oReader.Read());
{
int i = 0;
System.Web.UI.HtmlControls.HtmlGenericControl createDiv =
new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
createDiv.ID = "createDiv";
this.Controls.Add(createDiv);
List<TextBox> tb_names = new List<TextBox>();
TextBox tb_name = new TextBox();
var lstText=new List<string>
{
"Scheepspnaam",
"RederijNr",
"Lengte",
"Laadvermogen"
};
for (int j = 0; j < 4; j++)
{
tb_name.ID = "CreateT_" + i + "_" + (j+1);
//tb_name.Text = oReader["SchipNaam"].ToString();
createDiv.Controls.Add(new LiteralControl
("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> " + lstText[j] +
": <input type='text' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
tb_names.Add(tb_name);
}
i++;
}
如果你在这里使用for循环,你可以减少代码行数