开发者

Customise Pages using configuration in SQL Query

I am using the following code to customise the UI of my pages. I have placed this code inside a base class which all pages inherit from. It works fine but can you help me make it a bit more generic and extendable? At the moment it only supports text boxes, buttons and labels for the controls and text or visible for the properties. I can of course add more controls and properties but I'd like to think there is a smarter way of doing this. The comments describe a bit of the code which is missing where the customisations are returned from a SQL query.

    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, st开发者_开发问答ring 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;
    }


I suggest you do it the way around, first find all the control that can be customized (eventually differentiated by a tag) then query your server giving the list of controls otherwise you will end up querying the whole control table every time or doing as many request as there are controls on your page.

Another approach come from the fact that probably, those values won't change during the execution of the webs site. If that's the case, intensively use caching and then you could go for the "by control" query which will be slow for the first pages then very fast.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜