动态用户控件在回发后获取和维护值 [英] Dynamic User Controls get and maintain values after postbacks

查看:13
本文介绍了动态用户控件在回发后获取和维护值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个动态生成用户控件的页面.例如,用户控件有两个可以编辑的字段:SomeId 和 SomeData.我希望能够基于 UserControlObject 设置这些值.当我之后读取控件时,我希望能够从控件返回一个 UserControlObject 并将数据库值设置为该值.我无法让这些值保持最新状态.

I have a page that I have built that is dynamically generating user controls. For example, the user control has two fields that can be edited: SomeId and SomeData. I want to be able to set these values based on a UserControlObject. When I read the control afterwards, I want to be able to return a UserControlObject from the control and set the database values to that. I am having some trouble getting the values to stay current.

//Page
public class SomePage
{
    private List<MyUserControl> _MyUserControls;

    private void InitControls()
    {
      var controlsToBuild = SomeDatabaseCallToGetControlObjects();

          foreach(MyUserControl control in controlsToBuild)
          {
        MyUserControl control = this.Page.LoadControl("MyControl.ascx") as MyUserControl;

          UserControlPlaceHolder.Controls.Add(myControl);
          _MyUserControls.Add(myControl);
      }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
       InitControls();
    }

    protected void SaveButton_Click(object sender, EventArgs e)
    {
      if (this.IsValid)
      {
        List<MyUserObject>listObj = new List<MyUserObject>();
        foreach(MyUserControl control in _MyUserControls)
        {
           MyUserObject obj = control.GetObjectFromControl();
           listObjs.Add(obj);
        }
        SaveToDatabase(listObjs);
        //Clear placeholder in case number of controls has changed based on the save
        UserControlPlaceHolder.Clear();
        InitControls();
        GetObjectFromDatabase();
      }
    }

}

//Object to put in control
public class UserControlObject
{
    public string DataForUser { get;set;}
    public double MoreDataForUser { get;set;}
    public int HiddenIdFromUser { get; set;}
}


//User control
public class MyUserControl : UserControl
{

  public MyUserControlObject GetObjectFromControl()
  {
     UserControlObject obj = new UserControlObject();
     obj.DataForUser = textBoxOnTheControl.Text;
     obj.MoreDataForUser = anotherBoxOnTheControl.Text;
     obj.HiddenIdFromUser = hiddenField.Value;

     return obj;
  }

  public void SetUserControlObject(UserControlObject obj)
  {
     textBoxOnTheControl.Text = obj.DataForUser;
     anotherBoxOnTheControl.Text = obj.MoreDataForUser;
     hiddenField.Value = obj.HiddenIdFromUser;
  }
}

基本上,我想用数据库中的值加载每个 MyUserControl.用户应该能够更改大部分(但不是全部)这些字段.用户应该不能更改 ID,但我需要它来更新数据库中的记录.

Basically, I want to load up each MyUserControl with values from a database. A user should be able to change most (but not all) of those fields. A user shouldn't be able to change the ID, but I need it to update the record in the database.

我在获取信息以正确保存和保留时遇到了一些问题,因为我正在重新创建 MyUserControl 的集合.它似乎在初始回发时工作正常,但在随后的回发之后,我仍然得到其中一个字段的旧值.我如何确保:

I am having some trouble getting the information to save and persist correctly, because I am re-creating a collection of MyUserControl. It seems to work fine on the inital postback, but after subsequent postbacks, I still get the old value for one of the fields. How can I ensure that:

1) 信息已正确设置到用户控件.我是否需要在 UserControl 的 每个 属性上放置一个引用视图状态的公共属性?

1) The information is set to the user control(s) correctly. Do I need to put a public property referencing viewstate on every property of the UserControl?

2) 单击保存事件后,在回发后,重建控件不会导致我保留初始加载的任何旧"值?我应该如何处理这里的视图状态?我希望数据能够反映用户所做的任何更改.

2) After clicking a save event, after postback, rebuilding the controls won't cause me to retain any "old" values from the initial load? How should I handle the viewstate here? I want the data to reflect whatever the user has changed.

推荐答案

要使视图状态/控件状态正常工作,您的控件必须具有相同的 ID.由于您没有明确分配 ID,因此必须生成它,并且存在多个用户控件时会发生冲突.我会建议您使用某种逻辑来通过回发生成相同的 ID 集.例如,

For view-state/control-state to work properly, you must have same ID for your controls. As you are not assigning ID explicitly, it must getting generated and there would be conflicts there with more than one user control being present. I will recommend you to have some kind of logic to generate same set of IDs over post-back. For example,

private void InitControls()
{
  var numControlsToBuild = SomeDatabaseCallToGetNumControls();
  int idCounter = 1;
  foreach(var controlData in numControlsToBuild)
  {
    var control = this.Page.LoadControl("MyControl.ascx") as MyUserControl;
    control.ID = "MyControl" + idCounter.ToString();
    idCounter++;
    UserControlPlaceHolder.Controls.Add(myControl);
    _MyUserControls.Add(control);

    // probably code load control data into control
  }
}

这篇关于动态用户控件在回发后获取和维护值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆