简体   繁体   中英

Removing dynamically created controls

I am attempting to remove dynamically created controls in C# 2008 asp.net

The controls are created here:

        int i;
        for (i = 0; i < myCount; i += 1)
        {
            TextBox txtAuto = new TextBox();
            TextBox txtModel = new TextBox();
            TextBox txtMiles = new TextBox();
            TextBox txtVINumber = new TextBox();
            TextBox txtPlateNumber = new TextBox();

            txtAuto.ID = "txtVehAuto" + i.ToString();
            txtModel.ID = "txtVehModel" + i.ToString();
            txtMiles.ID = "txtVehMilage" + i.ToString();
            txtVINumber.ID = "txtVehVINumber" + i.ToString();
            txtPlateNumber.ID = "txtVehPlate" + i.ToString();

            phAuto.Controls.Add(txtAuto);
            phModel.Controls.Add(txtModel);
            phMiles.Controls.Add(txtMiles);
            phVINumber.Controls.Add(txtVINumber);
            phPlateNumber.Controls.Add(txtPlateNumber);

            dyntxtAuto[i] = txtAuto;
            dyntxtModel[i] = txtModel;
            dyntxtMiles[i] = txtMiles;
            dyntxtVINumber[i] = txtVINumber;
            dyntxtPlateNumber[i] = txtPlateNumber;

            LiteralControl literalBreak = new LiteralControl("<br />");

            phAuto.Controls.Add(literalBreak);
            phModel.Controls.Add(literalBreak);
            phMiles.Controls.Add(literalBreak);
            phVINumber.Controls.Add(literalBreak);
            phPlateNumber.Controls.Add(literalBreak);
        }
    }

How can I remove the controls? The user will click "submit" at which point the user entered data will be written to the db. The page will retrun back to itself (with blank fields). I want to remove the dynamic controls after the data has been written to the db. Remove meaning delete, not hide. I tried to set ViewState to false, but it doesn't help.

Can you do this?

phAuto.Controls.Clear();
phModel.Controls.Clear();
phMiles.Controls.Clear();
phVINumber.Controls.Clear();
phPlateNumber.Controls.Clear();

Are there other controls in these controls that I'm not aware of that you do not want cleared?

It depends on what you are trying to do...

If you are simply wanting to remove ALL the controls within your placeholder "phAuto", "phModel", etc... you can use the *.Controls.Clear(); method.

However, if you're looking to remove individual controls... you can use the *.FindControl(string id) method. It will return a generic Control object of with the id you pass to it. You could then pass that object to the *.Controls.Remove(...) method.

If you're wanting to use Page.FindControl(), Page.Controls.Remove(), etc AND you're using master pages... it gets kinda tricky. Read this article to learn about Recursive Control searching.

I'm still new to .NET myself and I'm finding new (and easier) ways of doing things daily. I would not be surprised if there's a better way of doing this.

Good Luck!

private List<Control> ControlList = new List<Control>;

private void CreateControls();
{
    int i;
    for (i = 0; i < myCount; i += 1)
    {
        TextBox txtAuto = new TextBox();
        TextBox txtModel = new TextBox();
        TextBox txtMiles = new TextBox();
        TextBox txtVINumber = new TextBox();
        TextBox txtPlateNumber = new TextBox();

        txtAuto.ID = "txtVehAuto" + i.ToString();
        txtModel.ID = "txtVehModel" + i.ToString();
        txtMiles.ID = "txtVehMilage" + i.ToString();
        txtVINumber.ID = "txtVehVINumber" + i.ToString();
        txtPlateNumber.ID = "txtVehPlate" + i.ToString();

        ControlList.Add(txtAuto);
        ControlList.Add(txtModel);
        ControlList.Add(txtMiles);
        ControlList.Add(txtVINumber);
        ControlList.Add(txtPlateNumber);

        phAuto.Controls.Add(txtAuto);
        phModel.Controls.Add(txtModel);
        phMiles.Controls.Add(txtMiles);
        phVINumber.Controls.Add(txtVINumber);
        phPlateNumber.Controls.Add(txtPlateNumber);

        dyntxtAuto[i] = txtAuto;
        dyntxtModel[i] = txtModel;
        dyntxtMiles[i] = txtMiles;
        dyntxtVINumber[i] = txtVINumber;
        dyntxtPlateNumber[i] = txtPlateNumber;

        LiteralControl literalBreak = new LiteralControl("<br />");

        phAuto.Controls.Add(literalBreak);
        phModel.Controls.Add(literalBreak);
        phMiles.Controls.Add(literalBreak);
        phVINumber.Controls.Add(literalBreak);
        phPlateNumber.Controls.Add(literalBreak);
    }
}

private void RemoveControls(List<Control> ControlList)
{
    foreach (Control item in ControlList)
    {
        item.Remove();
    }
}

I haven't used ASP.NET in a while, and this is untested, but I would assume this (or a variation of it) would work. I can't remember if List<Control> works or not... Might have to make it List<TextBox> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM