다음 코드를 사용하여 내 페이지의 UI를 사용자 정의합니다. 이 코드를 모든 페이지가 상속하는 기본 클래스 안에 배치했습니다. 그것은 잘 작동하지만 좀 더 일반적이고 확장 할 수 있도록 도와 줄 수 있습니까? 현재 컨트롤 및 텍스트에 대한 텍스트 상자, 단추 및 레이블 만 지원되거나 속성에 대해 표시됩니다. 물론 더 많은 컨트롤과 속성을 추가 할 수 있지만이를 수행하는 더 똑똑한 방법이 있다고 생각하고 싶습니다. 주석은 사용자 정의가 SQL 조회에서 리턴되는 곳에서 누락 된 일부 코드를 설명합니다.SQL 쿼리의 구성을 사용하여 페이지 사용자 정의
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
CustomisePage();
}
}
protected void CustomisePage()
{
//Loops around SQL query results to get customisation values
//for each loop iteration it populates controlName, propertyName, propertyValue and then calls.....
Control controlToCustomise = FindControlRecursive(Page.Master, controlName);
CustomiseControl(controlToCustomise, propertyName, propertyValue);
}
protected void CustomiseControl(Control controlToCustomise, string propertyNameToCustomise, string propertyValueToCustomise)
{
//Visible is common to all controls
if (propertyNameToCustomise == "Visible")
{
controlToCustomise.Visible = bool.Parse(propertyValueToCustomise);
}
switch (controlToCustomise.GetType().ToString())
{
case "System.Web.UI.WebControls.Button":
Button buttonToCustomise = controlToCustomise as Button;
if (propertyNameToCustomise == "Text")
{
buttonToCustomise.Text = propertyValueToCustomise;
}
break;
case "System.Web.UI.WebControls.Label":
Label labelToCustomise = controlToCustomise as Label;
if (propertyNameToCustomise == "Text")
{
labelToCustomise.Text = propertyValueToCustomise;
}
break;
case "System.Web.UI.WebControls.TextBox":
TextBox textBoxToCustomise = controlToCustomise as TextBox;
if (propertyNameToCustomise == "Text")
{
textBoxToCustomise.Text = propertyValueToCustomise;
}
break;
}
}
protected Control FindControlRecursive(Control Root, string Id)
{
if (Root.ID == Id)
return Root;
foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null)
return FoundCtl;
}
return null;
}