2011-09-20 2 views
1

다음 코드를 사용하여 내 페이지의 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; 
    } 

답변

0

나는 그 컨트롤의 목록을 제공하는 서버를 조회하면 (태그에 의해 결국 차별화) 사용자 정의 할 수있는 모든 컨트롤을 찾을 수 첫째, 주위에게 길을 제안 그렇지 않으면 전체 제어를 조회 끝날 것 테이블에 매번 또는 많은 요청을 페이지에 컨트롤이 있습니다.

또 다른 접근법은 아마도 웹 사이트 실행 중에 이러한 값이 변경되지 않는다는 사실에서 비롯된 것입니다. 이 경우 집중적으로 캐싱을 사용하면 첫 번째 페이지의 속도가 매우 느릴 수있는 "by control"쿼리를 수행 할 수 있습니다.

관련 문제