添加增量int

时间:2016-09-22 08:00:24

标签: c# unique increment

我的独特文本框ID存在问题。 我的代码是为我的数据库中的每艘船创建一个带有4个文本框的div。 每个创建的文本框都必须具有唯一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。我究竟做错了什么?我认为它之前有用,但似乎无法找到问题。

我希望你们看到我做错了什么。

提前致谢!

2 个答案:

答案 0 :(得分:0)

在每个while循环的开头创建值为0的i。在循环之前初始化整数,然后在循环内更新它,否则在每次迭代时它将重置为0。

您在while循环中添加了3个文本框,但只在while循环结束时增加i。您需要更改后缀,因为它总是_1或在每个文本框之间使用i ++以使标识符唯一。

正如Jon Skeet在上面的评论中提到的那样,不需要在启动时使用分号。

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 :(得分:0)

你在行中给出了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循环,你可以减少代码行。