【问题标题】:How can I explicitly maintain view state for dynamically created user controls?如何显式维护动态创建的用户控件的视图状态?
【发布时间】:2011-09-21 05:56:23
【问题描述】:

我有一个搜索表单,它提供了许多不同的搜索选项,例如“搜索自然名称”、“搜索法定名称”、“搜索地址”等。这些都是由静态“工厂”方法定义的PropertySearchOption 类。每个搜索选项指定一个用户控件,其中包含该选项所需的搜索字段,例如自然名称选项具有名称和姓氏字段。使用的搜索选项由主搜索页面上的 RadioButtonList 确定,当此列表中的选择发生变化时,我会动态加载所需的用户控件。我的问题是我找不到保留动态控件的视图状态及其搜索字段的方法。这是我正在使用的核心代码:

<asp:RadioButtonList ID="searchTypeOptions" runat="server" AutoPostBack="true" RepeatDirection="Horizontal" RepeatLayout="Flow" OnSelectedIndexChanged="SearchTypeOptionsSelectedIndexChanged">
    <asp:ListItem Value="NaturalName">Name (Natural)</asp:ListItem>
    <asp:ListItem Value="LegalName">Legal Name (Business)</asp:ListItem>
    <asp:ListItem Value="ErfDetails">Erf Details</asp:ListItem>
    <asp:ListItem Value="PhysicalAddress">Physical Address</asp:ListItem>
    <asp:ListItem Value="FarmDetails">Farm</asp:ListItem>
</asp:RadioButtonList>

protected void Page_Init(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var typeOption = searchTypeOptions.SelectedItem;
        if (typeOption == null)
        {
            searchTypeOptions.SelectedValue = "ErfDetails";
            LoadSearchForm();
        }
    }
}

protected void SearchTypeOptionsSelectedIndexChanged(object sender, EventArgs e)
{
    LoadSearchForm();
}

protected virtual void LoadSearchForm()
{
    SearchOption = (PropertySearchOption)typeof(PropertySearchOption).GetProperty(searchTypeOptions.SelectedValue, BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null);
    searchFormPlaceHolder.Controls.Clear();
    var searchForm = LoadControl(SearchOption.FormControlUrl);
    searchFormPlaceHolder.Controls.Add(searchForm);
    searchTypeLabel.Text = SearchOption.Description;
}

我必须在searchFormPlaceHolder 上将 EnableViewState 设置为 false,否则当我尝试更改当前选项时会出现视图状态异常,并且像这样动态选择搜索表单可以正常工作。但是,在按钮上单击回发以执行搜索,我丢失了搜索表单,因为它仅在第一次加载时在 Page_Init 中创建,而不是回发。如果我将 LoadSearchForm 放在 if 块之外,它会失败,因为在回发时,单选按钮列表的 SelectedValue 是空的。

在这种情况下,除了逃跑,我还能做什么,或者我应该怎么做?

【问题讨论】:

    标签: asp.net webforms viewstate asp.net-4.0


    【解决方案1】:

    您可以将搜索类型选项保存在 HiddenField 中,这样您就可以在每次回发时创建搜索表单(并记住为您动态创建的控件设置唯一的 id)

    protected void Page_Init(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var typeOption = searchTypeOptions.SelectedItem;
            if (typeOption == null)
            {
                searchTypeOptions.SelectedValue =
                     myHiddenField.Value = "ErfDetails";
                LoadSearchForm("ErfDetails");
            }
        }
        else
           LoadSearchForm(Request.Form[myHiddenField.UniqueID]);
    }
    
    override void OnPreRender(EventArgs e) {
        myHiddenField.Value = searchTypeOptions.SelectedValue;
    }
    
    protected void SearchTypeOptionsSelectedIndexChanged(object sender, EventArgs e)
    {
        LoadSearchForm(searchTypeOptions.SelectedValue);
    }
    
    protected virtual void LoadSearchForm(string option)
    {
        SearchOption = (PropertySearchOption)typeof(PropertySearchOption).GetProperty(option, BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null);
        searchFormPlaceHolder.Controls.Clear();
        var searchForm = LoadControl(SearchOption.FormControlUrl);
    
        searchForm.ID = "mySearchForm";
    
        searchFormPlaceHolder.Controls.Add(searchForm);
        searchTypeLabel.Text = SearchOption.Description;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-05
      • 1970-01-01
      相关资源
      最近更新 更多