1

我正在尝试开发一个自定义 ASP.Net 服务器控件,它可以在客户端进行操作。要在回发后保存更改,有一个隐藏字段。在 OnLoad 事件中,我检索了写入属性的值,但似乎为时已晚,因为控件已经构建。我知道我可以操纵 PreRender 事件上的控件,但对我来说似乎有更好的方法来处理这个问题。任何人的想法?

public class Control : CompositeControl {

  private bool mProperty;
  private HiddenField hiddenField;


  public virtual bool Property {
    get {
      return mProperty;
    }
    set {
      mProperty = value;
    }
  }
  protected override void CreateChildControls() {
    Controls.Clear();
    CreateControlHierarchy();
    ClearChildViewState();
  }
  protected virtual void CreateControlHierarchy() {
    CreateHiddenField();
    CreateContent();
  }

  protected virtual void CreateHiddenField() {
    hiddenField = new HiddenField();
    hiddenField.ID = "hiddenField";
    hiddenField.Value = Property.ToString().ToLower();
    Controls.Add(hiddenField);
  }  
  protected virtual void CreateContent() {
    contentPanel = new Panel();
    contentPanel.ID = "content";
    contentPanel.Vsiible = Property;
    Controls.Add(contentPanel);
  }
  protected override void OnLoad(EventArgs e) {
    base.OnLoad(e);
    if(Page.IsPostback) {
      Property = Convert.ToBoolean(Page.Request.Form[hiddenField.UniqueId]);
    }
  }
}

编辑可能的解决方案:

我摆脱了 OnLoad 事件并像这样编辑了属性:

public virtual bool Property { 
  get { 
    if (Page.IsPostBack) {
      EnsureChildControls();
      return Convert.ToBoolean(Page.Request.Form[hiddenField.UniqueID]);
    }
    return mProperty; 
  }
  set { 
    mProperty = value;
  }
}

这是一个好方法吗?

4

2 回答 2

1

通过为隐藏字段实现 ValueChanged EventHandler 并编辑属性的设置器以处理所有依赖项,我实现了我想做的事情。

public class Control : CompositeControl {

  private bool mProperty;
  private HiddenField hiddenField;


  public virtual bool Property {
    get {
      return mProperty;
    }
    set {
      mProperty = value;
      if (contentPanel != null) contentPanel.Visible = value;
      if (hiddenField != null && hiddenField.Value != value.ToString().ToLower()) hiddenField.Value = value.ToString().ToLower();
    }
  }
  protected override void CreateChildControls() {
    Controls.Clear();
    CreateControlHierarchy();
    ClearChildViewState();
  }
  protected virtual void CreateControlHierarchy() {
    CreateHiddenField();
    CreateContent();
  }

  protected virtual void CreateHiddenField() {
    hiddenField = new HiddenField();
    hiddenField.ID = "hiddenField";
    hiddenField.Value = Property.ToString().ToLower();
    hiddenField.ValueChanged += hiddenField_ValueChanged;
    Controls.Add(hiddenField);
  }  
  protected virtual void CreateContent() {
    contentPanel = new Panel();
    contentPanel.ID = "content";
    contentPanel.Vsiible = Property;
    Controls.Add(contentPanel);
  }
  void hiddenField_ValueChanged(object sender, EventArgs e) {
    Property = Convert.ToBoolean(hiddenField.Value);
  }
  protected override void OnInit(EventArgs e) {
    EnsureChildControls();
    base.OnInit(e);
  }
}
于 2013-03-25T12:20:34.030 回答
1

一种建议是在属性和控件上都设置回发值,因为该属性仅在创建控件时使用。

 protected override void OnLoad(EventArgs e) {
    base.OnLoad(e);
    if(Page.IsPostback) {
      if(hiddenField != null)
         hiddenField.Value = Page.Request.Form[hiddenField.UniqueId].ToString();

      Property = Convert.ToBoolean(Page.Request.Form[hiddenField.UniqueId].ToString());    
    }
  }
于 2013-03-25T11:16:35.003 回答