简体   繁体   中英

ASP.NET force dynamically generated control to be treated as new

I'm working on a web ui for data manipulation based on the entity framework.

Let's assume I have the following two models:

public class Bottle
{
    [Key]
    public Int32 BottleId { get; set; }

    [Required]
    [MinLength(3)]
    [MaxLength(128)]
    public String Name { get; set; }

    [Required]
    public virtual BottleType BottleType { get; set; }
}

public class BottleType
{
    [Key]
    public Int32 BottleTypeId { get; set; }

    [Required]
    [MinLength(3)]
    [MaxLength(128)]
    public String Name { get; set; }

    public virtual ICollection<Bottle> Bottles { get; set; }
}

Possible values for those models would be

Bottle {
    Name = "Foo"
    BottleType = BottleType {
        Name = "Unicorn Bottle"
    }
}

The web ui will show a dropdownlist with two entries, one for Bottle and one for BottleType . If the selection is changed, new controls will be generated based on the properties of the model.

As example, if Bottle is selected, 6 controls will be generated - 3 labels and 3 input boxes:

  1. label, text = "BottleId"
  2. textbox disabled, text = "(auto)" - because its value will be auto generated in the database
  3. label, text = "Name"
  4. textbox
  5. ...

also, mandatory will be appendet to the label of each control if the input is required.


Talking about my problem now; When I change the selected index of the dropdown the controls will be removed with panel.controls.clear() . Afterwards new controls will be generated for the selected model.

I'm not sure what the cause of the problem is, it might the 'position' of the control on the page that causes asp.net to treat it as 'the same'.

Here is a code snipped

 1 |  Panel p = new Panel();
 2 |  Label l = new Label
 3 |            {
 4 |                Text = "bar" // this would be the name of the property
 5 |            };
 6 |  p.Controls.Add(l);
 7 |  if ( ... required check ... )
 8 |  {
 9 |      // is required
10 |      l.Text += " (mandatory)";
11 |  }

after line 2, l.Text equals bar (like it's supposed to be). But if the model before had a property with the same name, it will change the value of l.Text after line 6 to bar (mandatory) (which was the value of the label for the model before), which then will result in bar (mandatory) (mandatory) after line 10.


Long story, short question: can I force asp.net to treat a generated control as new?

I tried giving the label a unique id each time it's generated with Guid.NewGuid().ToString() to indicate a different instance, but this doesn't seem to work.

I couldn't find any related issues on this topic because most of the times the people WANT to regenerate the controls and keep their values, .. not in my case though.

I managed to solve to problem myself, although I'm not sure how.

I mentioned in the question that I had tried to assign a unique id to each control - which didn't work.

As I continued coding i figgured I need to access the generated controls, so I added a predictable id for each element (panels, controls, labels, ..).

I assume that the solution to this problem is the parent panel which now has a uniqe id among the different models which wasn't the case in my test scenario.

This solved the issue.

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