【问题标题】:Best way to do the runtime UI changes on User Control?在用户控件上进行运行时 UI 更改的最佳方法是什么?
【发布时间】:2013-08-24 16:53:09
【问题描述】:

我是 .net 的新手,我正在开发办公室中的一些功能,如下所示

Jobs.aspx.cs

protected void gvActionItems_RowEditing(object sender, GridViewEditEventArgs e)
{
    //setting the value of the user control property 
}

JobUserControl.ascx.cs

public int _usrcontrolproperty
{
  get{return _usrcontrolproperty;}
  set{
    //depending on the value of the property fetch the data from the database and binding those data on the user controls FormView
    }
}

protected void fvJob_DataBound(object sender, EventArgs e)
{
   //Making the dynamic UI changes that is setting properties of controls  depending upon the values of binding data
}

这就是我在表单视图的数据绑定事件中进行所需 UI 更改的方式,但一位资深人士说 '这是糟糕的架构代码设计,它有额外的内存问题,并在 _usrcontrolproperty 设置方法之后进行 UI 更改数据绑定。'。所以我想知道

1) Is this really bad architectural code ? If bad then why ?
2) And if my seniors way is bad then also Why ? 

因为我认为 UI 更改应该在绑定数据时完成

【问题讨论】:

  • 他能解释一下“额外的内存问题”吗?
  • @Simon 我试图和他讲道理,但他没有告诉我。这就是我在这里问的原因。
  • @Curiosity 他/她似乎有他/她自己的记忆问题,对于编码部分的设计,Simon 给出的方式比你做的要好得多现在

标签: c# asp.net architecture user-controls


【解决方案1】:

如果您的上级无法支持他/她的主张..那么他/她不是您应该尝试学习的人。我不确定他/她所指的“内存问题”是什么,但是用你的精简代码很难说清楚。

话虽如此,我会重新考虑属性集中的数据绑定,纯粹是因为当人们开始设置此属性时,你会在以后的轨道上打开自己的“陷阱”。

相反,我会使用Refresh() 方法。因此,调用代码将是:

UserControl.Property = value;
UserControl.RefreshData();

这为调用 API 提供了在该点刷新或推迟决定的选项。

【讨论】:

  • 所以数据绑定应该与 UI 更改一起在 Refresh() 方法中完成
  • 获取数据和数据绑定应该在刷新时完成。我会更新我的答案以更清楚。
  • 好的,感谢您的帮助以及关于在数据绑定时应该完成的 UI 更改,对吗?
【解决方案2】:

我与@Simon 合作,但我有一个 RefreshData(value) 方法。因此,调用代码将是:

UserControl.RefreshData(value);

这为调用 API 提供了在该点刷新或推迟决定的选项。 在这个方法中你可以使用 as ,

 Public static RefreshData(<datatype>data)
 {
   //Assign the value to the property 
   //Get the data from database 
   //Bind the data 
 }

如果您不想将属性公开给其他类,您还可以将属性设为私有或受保护。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    相关资源
    最近更新 更多