如何通过回发在asp.net中保持用户控制状态?

时间:2014-09-20 05:04:43

标签: c# asp.net user-controls telerik

如何通过回发在asp.net中保持用户控制状态? 我有一个用户控件,它包含一个radTreeView,一个dropDownList和一个图表以及一个用于搜索的按钮。在page_load中,我填写DropDownList和radTree和Chart。当我将用户控件加载到我的page.aspx并选择radTreeView或DropDownList的节点并单击Buttn(btnSearch)时,用户控件将丢失。如何保持用户控制状态并在dropDownList中保留所选节点或选定值

在MyUserControl.ascx中

<asp:Label ID="lblProductGroup" runat="server" CssClass="h4" Text="products:"></asp:Label>

<telerik:RadTreeView ID="radTreeProduct" runat="server" ShowLineImages="true" Skin="WebBlue">
<WebServiceSettings Path="~/Services/WebService.asmx" Method="GetAllProductList"></WebServiceSettings>
</telerik:RadTreeView>

                                               
   <div class="hundredW">
        <asp:ImageButton ID="imgBtnSearch" runat="server" CssClass="h4"  OnClick="imgBtnSearch_Click" />

                        </div>
<div>
<asp:Chart ID="chartInvoice" runat="server"
                                Palette="BrightPastel" ImageType="Png" BorderDashStyle="Solid" BackSecondaryColor="White"
                                BackGradientStyle="TopBottom" BorderWidth="2" BackColor="#D3DFF0" BorderColor="26, 59, 105"
                                Height="321px" Width="501px">
                                <BorderSkin SkinStyle="Emboss" />
                                <Legends>
                                    <asp:Legend Name="Legend1" BackColor="Transparent" Font="Trebuchet MS, 8.25pt, style=Bold"
                                        IsTextAutoFit="False">
                                    </asp:Legend>
                                </Legends>
                                <Titles>
                                    <asp:Title Alignment="TopCenter" BackColor="180, 165, 191, 228"  BackGradientStyle="TopBottom"
                                        BackHatchStyle="None" Font="Microsoft Sans Serif, 12pt, style=Bold" Name="Title"
                                        ToolTip="Revenue" ForeColor="26, 59, 105">
                                    </asp:Title>

                                </Titles>

                                <ChartAreas>
                                    <asp:ChartArea Name="ChartArea1" BackColor="64, 165, 191, 228" BackSecondaryColor="White"
                                        BorderColor="64, 64, 64, 64" ShadowColor="Transparent" BackGradientStyle="TopBottom">
                                        <AxisY LineColor="64, 64, 64, 64" IsLabelAutoFit="False" ArrowStyle="Triangle">
                                            <LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />
                                            <MajorGrid LineColor="64, 64, 64, 64" />
                                        </AxisY>
                                        <AxisX IsLabelAutoFit="False" LineColor="64, 64, 64, 64" ArrowStyle="Triangle"
                                            Interval="1">
                                            <LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" IsEndLabelVisible="False" Angle="0" />
                                            <MajorGrid LineColor="64, 64, 64, 64" />
                                        </AxisX>

                                    </asp:ChartArea>
                                </ChartAreas>

                            </asp:Chart>
</div>

在MyUserControl.ascx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            FillComboBox();//methode for Fill combobox
            CreateRadTreeView();
            CreateChart();
        }
    }

.............

在MuPage.aspx.cs

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               Control ucReport =this.LoadControl("~/UserControl/Repo/MyUserControl.ascx");

ucReport.EnableViewState = true;

                content.Controls.Add(ucReport);

            }
        }

1 个答案:

答案 0 :(得分:1)

加载动态控件的加载时间太晚,因此您希望至少在PreInit / Init时间帧中加载控件。你必须在每个回发时加载它,而不仅仅是初始版本。

相关问题