0

I have created a function to dynamically create textboxes based on the amount selected from the textbox, additionally I'm using these textboxes to display data from database. However when the user chooses for exactly five from the dropdownlist, and three textboxes was already there, instead of adding 2 more textboxes, it adds the additional 5 textboxes. What I do in order to just add the additionaly textboxes?

 protected void TotalSeal_SelectedIndexChanged(object sender, EventArgs e)
    {
        populate();
    }

  public void populate()
    {

        int count = Convert.ToInt32(TotalSeal.SelectedItem.Value);
        for (int i = 0; i < count; i++)
        {
            if (i < 0)
            {

            }
            else
            {

                TextBox tx = new TextBox();
                tx.MaxLength = 10;
                tx.Width = 100;
                phSealNum.Controls.Add(tx);
                phSealNum.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));

                ControlCache.Add(tx);
            }
        }
    }

UPDATE

  public void populate()
    {
        //ControlCache = new List<Control>();
        //phSealNum.Controls.Clear();

        int targetCount = Convert.ToInt32(TotalSeal.SelectedItem.Value);
        int currentItems = phSealNum.Controls.OfType<TextBox>().Count();
        int totalitems = targetCount - currentItems;
        if (totalitems <= 7)
        {
            for (int i = 0; i < totalitems; i++)
            {

                TextBox tx = new TextBox();
                tx.MaxLength = 10;
                tx.Width = 100;
                phSealNum.Controls.Add(tx);
                phSealNum.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));

                ControlCache.Add(tx);
            }
        }
        else
        {
            lblError.Text = targetCount + " exceeds number of seals";
        }
    }
3
  • 1
    TotalSeal.SelectedItem.Value is the value user selected (5 is this case). You also need another count (call it currentCount for example) for the ones you actually have created. Then you can make a loop from 0 to (count-currentCount) for the remaining. Commented May 11, 2020 at 19:20
  • I've updated my question, not sure if i did it correctly Commented May 11, 2020 at 20:41
  • @IndritKello, can you check on the update in the question if it was done correctly Commented May 13, 2020 at 13:48

1 Answer 1

1

Using @indrit-kello logic should be like this:

protected void TotalSeal_SelectedIndexChanged(object sender, EventArgs e)
    {
        populate();
    }

    public void populate()
    {

        int targetCount = Convert.ToInt32(TotalSeal.SelectedItem.Value);

        if(targetCount > 7)
          targetCount = 7;

        int currentItems = 0;//TODO
        for (int i = currentItems; i < targetCount; i++)
        {
            TextBox tx = new TextBox();
            tx.MaxLength = 10;
            tx.Width = 100;
            phSealNum.Controls.Add(tx);
            phSealNum.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));

            ControlCache.Add(tx);
        }
    }
Sign up to request clarification or add additional context in comments.

5 Comments

and what if I want set a control that the number of textboxes should exceed 7 even after the aditional textboxes
what you mean by "should exceed 7"? can you explain more?
Say for example there is already 5 textboxes and the select 4 more additional textboxes; that's a total of 9 textboxes; but you want them to have 7 textboxes in total
I've updated my question with what I have come up with
i have updated the code to limit the max textbox, see if that is what you want

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.