[英]Need help in migrating methods and event handlers from a usercontrols code-behind to a class library. Or need to know if its possible
我想出一種使我的代碼更可重用的方法,但是我想知道是否有更好的方法。 我的方法要求將相同的代碼復制並粘貼到每個用戶控件的代碼背后。 我所做的就是最大程度地減少每次復制和過去之后需要進行的更改數量。
題
有沒有一種方法可以編寫代碼,以便無需復制和粘貼即可重新使用它? 就像將方法和事件處理程序放入類庫並讓每個用戶控件引用它並從那里調用它一樣。
下面是我目前正在做的事情以及其中一個用戶控件的更詳細的描述。
我有20個完全相同的用戶控件,除了它們使用不同的類/實體。 例如,一個實體是StudentInjury,另一個是PoliceReport。 基本上,每個用戶控件都會在網格中顯示實體的集合,並包含添加,編輯,刪除和取消刪除按鈕。 每個實體都有在網格中顯示的不同屬性,因此我不打算重復使用usercontrol的aspx部分。
基於我的有限知識,我使隱藏代碼盡可能易於重用。 我通過在代碼中添加注釋來做到這一點,例如:
// 1. Set the entityCollection
var entityCollection = incident.StudentInjuries;
和
// 2. Set entityCollectionWithDeletes
StudentInjuryCollection entityCollectionWithDeletes = StudentInjuryCollection.LoadByIncidentWithDeletes(incident);
因此,當為PoliceReport用戶控件創建后台代碼時,我可以將所有StudentInjury代碼后台代碼簡單地復制到PoliceReport用戶控件代碼后台,並在進行替換后如下所示。
// 1. Set the entityCollection
var entityCollection = incident.PoliceReports;
和
// 2. Set entityCollectionWithDeletes
PoliceReportCollection entityCollectionWithDeletes = PoliceReportCollection.LoadByIncidentWithDeletes(incident);
下面是其中一個用戶控件的代碼:
public partial class StudentInjuryControl : System.Web.UI.UserControl
{
// 1. Set the name of the entity
public const string EntityName = "StudentInjury";
public void Refresh(Incident incident, FormMode mode, bool showDeletedItems)
{
// 1. Set the entityCollection
var entityCollection = incident.StudentInjuries;
if (entityCollection.Count > 0)
{
YesNoRadioButtonList.SelectedValue = "Yes";
YesNoRadioButtonList.Enabled = false;
}
else
{
YesNoRadioButtonList.Enabled = true;
if (YesNoRadioButtonList.SelectedValue == "Yes")
{ // Needed in case cancel button clicked
YesNoRadioButtonList.ClearSelection();
}
if (mode != FormMode.Add)
{
YesNoRadioButtonList.SelectedValue = "No";
}
}
int numberOfRecords;
if (showDeletedItems)
{
// 2. Set entityCollectionWithDeletes
StudentInjuryCollection entityCollectionWithDeletes = StudentInjuryCollection.LoadByIncidentWithDeletes(incident);
numberOfRecords = entityCollectionWithDeletes.Count;
EntityGridView.DataSource = entityCollectionWithDeletes;
}
else
{
numberOfRecords = entityCollection.Count;
EntityGridView.DataSource = entityCollection;
}
EntityGridView.DataBind();
if (numberOfRecords > 0)
{
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "show" + EntityName + "Container", "document.getElementById('" + EntityName + "Container').style.display='block';", true);
}
else
{
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "hide" + EntityName + "Container", "document.getElementById('" + EntityName + "Container').style.display='none';", true);
}
}
protected void EntityGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Undelete")
{
Guid id = new Guid(e.CommandArgument.ToString());
// 1. Call the undelete method
StudentInjury.Undelete(id);
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "refresh" + EntityName, "RefreshForm('" + EntityName + "')", true);
}
}
protected void EntityGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// 1. Set the entity
StudentInjury entity = (StudentInjury)e.Row.DataItem;
Guid entityId = entity.Id;
Button deleteButton = (Button)e.Row.FindControl("DeleteButton");
Button undeleteButton = (Button)e.Row.FindControl("UndeleteButton");
Button editButton = (Button)e.Row.FindControl("EditButton");
if (entity.DeleteDateTime.HasValue)
{
e.Row.ForeColor = System.Drawing.Color.Red;
e.Row.Font.Strikeout = true;
if (undeleteButton != null)
{
undeleteButton.Enabled = true;
}
if (editButton != null)
{
editButton.Enabled = false;
}
if (deleteButton != null)
{
deleteButton.Enabled = false;
}
}
else
{
if (deleteButton != null)
{
deleteButton.Enabled = true;
deleteButton.OnClientClick = "return ShowDeleteForm('" + entityId.ToString() + "','" + EntityName + "');";
}
if (undeleteButton != null)
{
undeleteButton.Enabled = false;
}
if (editButton != null)
{
editButton.Enabled = true;
editButton.OnClientClick = "return ShowEditForm('" + entityId.ToString() + "', '" + EntityName + "');";
}
}
}
}
}
使代碼可重復使用的想法是在所有控件之間添加另一層,該層具有所有控件可以使用的通用功能。
例如,制作一個繼承System.Web.UI.UserControl的基本控件類,如下所示:
public class BaseControl : System.Web.UI.UserControl
{
//common control logic goes here
}
然后,您的具體控件將如下所示:
public partial class StudentInjuryControl : BaseControl
{
}
然后,您只需要確保使通用方法更通用即可將它們用於其他控件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.