在回调中呈现服务器控件

时间:2012-10-22 17:08:29

标签: c# asp.net custom-server-controls

我有一堆不同的面板,它们是自定义服务器控件,它继承了CompositeControl。使用CreateChildControls()呈现面板。我有一个页面,我根据comboBox中的用户选择显示它们。当您更改选择时,它会为回调面板(DevExpress控件)执行回调,这类似于更新面板。根据选择,它将面板添加到回调面板。但似乎如果从回调中执行此操作,则服务器控件生命周期不会启动,因此它从不调用CreateChildControls()或OnPreRender()。关于如何使这项工作的任何想法? EnsureChildControls()似乎没有帮助。

以下是其中一个面板的示例代码

    protected override void CreateChildControls()
    {
        base.CreateChildControls();

        String strParentID = this.ClientInstanceName.Length > 0 ? this.ClientInstanceName : this.ClientID;
        String strID = "";//Used as ID and ClientInstanceName.
        String strKey = "";//Used in the ID and as the ControlInfo key.

        if (!DesignMode)
        {
            //*******************************************************************

            ASPxLabel lblUnit = new ASPxLabel();
            lblUnit.Text = "Select Unit(s)";
            callbackEdit.Controls.Add(lblUnit);

            //*******************************************************************

            strKey = "btnUnitSelector";
            strID = strParentID + "_" + strKey;
            btnUnitSelector = new ASPxButton()
            {
                ID = strID,
                ClientInstanceName = strID,
                CssFilePath = this.CssFilePath,
                CssPostfix = this.CssPostfix,
                SpriteCssFilePath = this.SpriteCssFilePath,
            };

            btnUnitSelector.Width = Unit.Pixel(180);
            btnUnitSelector.AutoPostBack = false;

            this.listControlInfo.Add(new ControlInfo(strKey, btnUnitSelector.ClientInstanceName, btnUnitSelector.ClientID));
            callbackEdit.Controls.Add(btnUnitSelector);

            //*******************************************************************

            strKey = "unitPopup";
            strID = strParentID + "_" + strKey;

            unitPopup = new UnitPopup.UnitPopup();
            unitPopup.ID = strID;
            unitPopup.ClientInstanceName = strID;
            unitPopup.btnOk_AutoPostBack = false;
            unitPopup.ShowOnlyUnits = false;
            unitPopup.DataSource = GlobalProperties.Company_UnitsAndAreas;
            unitPopup.DataBind();
            btnUnitSelector.ClientSideEvents.Click = "function (s, e) { " + unitPopup.ClientInstanceName + ".Show(); }";
            unitPopup.ClientSideEvents.MemberSet = "function (s, e) { " + btnUnitSelector.ClientInstanceName + ".SetText(" + unitPopup.ClientInstanceName + ".selectedMemberName); }";

            Controls.Add(unitPopup);
            //*******************************************************************
        }
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        ClientScriptManager cs = this.Page.ClientScript;

        //Register an embedded JavaScript file. The JavaScript file needs to have a build action of "Embedded Resource".
        String resourceName = "QSR_ServerControls.Controls.DashboardControls.SalesChart.SalesChart.js";
        cs.RegisterClientScriptResource(typeof(SalesChart), resourceName);
    }

以下是添加面板的示例代码

    private void PopulatePanel(string panel)
    {
        tblDescCell.Controls.Add(new LiteralControl(GetPanels().Select("[PanelName] = '" + panel + "'")[0]["PanelDescription"].ToString()));
        if (panel == "SalesChart")
            tblTopLeftCell.Controls.Add(new SalesChart.SalesChart());
    }

    void callbackMain_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        PopulatePanel(e.Parameter);
    }

1 个答案:

答案 0 :(得分:0)

在回调请求期间,不执行PreRender和Render方法(这是与PostBack请求的主要区别)。但是应该执行CreateChildControls。请提供更多信息或代码示例。