Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在SQL查询中使用配置自定义页面_C#_Asp.net - Fatal编程技术网

C# 在SQL查询中使用配置自定义页面

C# 在SQL查询中使用配置自定义页面,c#,asp.net,C#,Asp.net,我使用以下代码来定制我的页面的UI。我已将此代码放置在所有页面都从中继承的基类中。它工作得很好,但你能帮我使它更通用和可扩展吗?目前,它只支持控件的文本框、按钮和标签以及属性的文本或可见。我当然可以添加更多的控件和属性,但我认为有一种更聪明的方法可以做到这一点。注释描述了从SQL查询返回定制时缺少的一些代码 protected void Page_Load(object sender, System.EventArgs e) { if (!IsPostBack)

我使用以下代码来定制我的页面的UI。我已将此代码放置在所有页面都从中继承的基类中。它工作得很好,但你能帮我使它更通用和可扩展吗?目前,它只支持控件的文本框、按钮和标签以及属性的文本或可见。我当然可以添加更多的控件和属性,但我认为有一种更聪明的方法可以做到这一点。注释描述了从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;
    }

我建议您这样做,首先找到所有可以定制的控件(最终通过标记进行区分),然后查询您的服务器,给出控件列表,否则您将每次查询整个控件表,或者执行与页面上控件数量相同的请求

另一种方法来自这样一个事实,即这些值在Web站点的执行过程中可能不会改变。如果是这样的话,请集中使用缓存,然后您可以使用“by control”查询,该查询在第一个页面中会比较慢,然后会非常快