用户控件中的DropDownList在动态加载时会在第一次回发时丢失SelectedValue

时间:2014-09-16 19:36:03

标签: c# asp.net dynamic-usercontrols

我有一个ASPX页面,其中有一个PlaceHolder,其中动态加载了用户控件。 HTML标记如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="Test" %>

<!DOCTYPE html>

<html>
<head runat="server">
</head>
<body>
    <form id="form1" runat="server">

        <asp:Button ID="btnNumbers" Text="Select Numbers" OnClick="SelectNumbers" runat="server" />
        <br /><br />
        <asp:Button ID="btnLetters" Text="Select Letters" OnClick="SelectLetters" runat="server" />
        <br /><br />

        <asp:PlaceHolder ID="phContent" runat="server" />

    </form>
</body>
</html>

守则背后如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Test : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        if (!IsPostBack)
            HttpContext.Current.Session["control"] = "~/Controls/Numbers.ascx";
        LoadControl();        
    }

    protected void SelectNumbers(object sender, EventArgs e)
    {
        HttpContext.Current.Session["control"] = "~/Controls/Numbers.ascx";
        LoadControl();
    }

    protected void SelectLetters(object sender, EventArgs e)
    {
        HttpContext.Current.Session["control"] = "~/Controls/Letters.ascx";
        LoadControl();
    }

    private void LoadControl()
    {
        phContent.Controls.Clear();
        var control = Page.LoadControl((String)HttpContext.Current.Session["control"]);
        phContent.Controls.Add(control);
    }
}

第一个用户控件有一个DropDownList来选择数字。它的标记如下:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Numbers.ascx.cs" Inherits="Numbers" %>


<asp:DropDownList ID="ddlSelectNumbers"
                  AutoPostBack="true" runat="server"
                  OnSelectedIndexChanged="SelectNumbers_IndexChanged" >
    <asp:ListItem Text="One" Value="1" />
    <asp:ListItem Text="Two" Value="2" />
    <asp:ListItem Text="Three" Value="3" />
</asp:DropDownList>

<br /><br />

<asp:Label ID="lblSelectedNumber" runat="server" />

And Code Behind如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Superexpert.Controls;


public partial class Numbers : System.Web.UI.UserControl
{
    public override String ID
    {
        get { return "Numbers"; }
        set { }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void SelectNumbers_IndexChanged(object sender, EventArgs e)
    {
        lblSelectedNumber.Text = ((DropDownList)sender).SelectedItem.Text;
    }
}

第二个用户控件有一个DropDownList来选择字母。它的标记如下:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Letters.ascx.cs" Inherits="Letters" %>



<asp:DropDownList ID="ddlSelectLetters"
                  AutoPostBack="true" runat="server"
                  OnSelectedIndexChanged="SelectLetters_IndexChanged" >
    <asp:ListItem Text="A" Value="a" />
    <asp:ListItem Text="B" Value="b" />
    <asp:ListItem Text="C" Value="c" />
</asp:DropDownList>

<br /><br />

<asp:Label ID="lblSelectedLetter" runat="server" />

And Code Behind如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Superexpert.Controls;


public partial class Letters : System.Web.UI.UserControl
{
    public override String ID
    {
        get { return "Letters"; }
        set { }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void SelectLetters_IndexChanged(object sender, EventArgs e)
    {
        lblSelectedLetter.Text = ((DropDownList)sender).SelectedItem.Text;
    }
}

当页面首次加载时,所有内容都能找到(!IsPostBack)。你可以从DropDownList中选择数字,它的SelectedItemChanged事件会被激活,它的SelectedValue会被更新,Label会被更新。

单击其中一个按钮以动态加载其中一个用户控件时,会出现问题。之后,第一次从DropDownList中选择某些内容时,SelectedItemChanged事件不会触发,它的SelectedValue不会更新,因此Label不会更新。之后,一切正常。

是什么导致这种情况发生?

提前致谢!

1 个答案:

答案 0 :(得分:1)

你非常关闭。您只需要为动态创建的控件分配ID。

基本上,如果要在回发后检索它们,则需要为动态创建的控件分配ID。

在以下代码中查看3 <---

public partial class Test : System.Web.UI.Page
{
   ... Same as above

    private void LoadControl()
    {
        phContent.Controls.Clear();
        var control = Page.LoadControl((String)HttpContext.Current.Session["control"]);
        control.ID = "1"; <--- 1. Control ID is required. 
        phContent.Controls.Add(control);
    }
}

public partial class Numbers : System.Web.UI.UserControl
{
    // public override String ID ... <--- 2. Remove this

    protected void SelectNumbers_IndexChanged(object sender, EventArgs e)
    {
        lblSelectedNumber.Text = ((DropDownList)sender).SelectedItem.Text;
    }
}

public partial class Letters : System.Web.UI.UserControl
{
    // public override String ID ... <--- 3. Remove this

    protected void SelectLetters_IndexChanged(object sender, EventArgs e)
    {
        lblSelectedLetter.Text = ((DropDownList)sender).SelectedItem.Text;
    }
}