TAGS :Viewed: 11 - Published at: a few seconds ago

[ ASP.NET Custom control and Page.Request.Form[] ]

I have a control that inherits from System.Web.UI.Control and contains generic HTML controls rather than asp.net server side controls. I use Page.Request.Form[Control_id] to get the value of one of the controls. This basically works accept if I have a gridview that contaiins a column of these custom controls and I add a new row [row6] and then delete a row from above tht row [row3], the control in the new row [row6 becoming row5] assumes the value of the row immediately above it [row5 becomming row4].

I beleive this is because I use Page.Request.Form[] to get the value for each control but my control doesnt know that those values belonged to controls that had previously occupied the same row. How do I fix this? I'd aprreciate any suggestions!!

Answer 1


You don't need to mess with the Page.Request.Form collection. What you need is a proper composite control. Here's a simple example:

public class InputTextWithLabelControl : CompositeControl {

  HtmlGenericControl _label;
  HtmlInputText _text;

  public string Label {
    get {
      EnsureChildControls();
      return _label.InnerText;
    }
    set {
      EnsureChildControls();
      _label.InnerText = value;
    }
  }

  public string Text {
    get {
      EnsureChildControls();
      return _text.Value;
    }
    set {
      EnsureChildControls();
      _text.Value = value;
    }
  }

  protected override void CreateChildControls() {
    Controls.Clear();

    _label = new HtmlGenericControl("span");
    _label.ID = "label";

    _text = new HtmlInputText();
    _text.ID = "text";

    Controls.Add(_label);
    Controls.Add(_text);
  }

}