动态用户控件获取和回传后保持值 [英] Dynamic User Controls get and maintain values after postbacks

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

问题描述

我有我已经构建了一个动态生成用户控件的页面。例如,用户控件有两个字段可以编辑: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 的集合。这似乎在inital回传做工精细,但随后的回传后,我仍然得到了其中一个字段的旧值。我怎样才能确保:

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)的信息被设置为用户控制(多个)正确。我需要把公共财产引用视图状态上的的用户控件的每一个的属性?

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,就必须如何产生并会有冲突,有一个以上的用户控件是present。我会建议你有某种逻辑来产生超过后回同一组的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天全站免登陆