动态控件消失ASP.NET C#(根据DropDownList选择加载控件)

时间:2014-07-29 23:04:35

标签: c# asp.net dynamic placeholder

我是ASP.NET的新手;我在页面中有一个DropDownList(带有一个母版页):

<asp:DropDownList ID="cmbPrueba" runat="server" OnSelectedIndexChanged="cmbPrueba_SelectedIndexChanged" AutoPostBack="true">
    <asp:ListItem Value="0">Compresor de Aire</asp:ListItem>
    <asp:ListItem Value="1">Compresor/Unidad de Refrigeración</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnActualizar" runat="server" Text="Actualizar" OnClick="btnActualizar_Click" />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

根据DropDownList(cmbPrueba),placeHolder使用字符串数组创建控件; (我使字符串数组模拟数据库的字符串结果)。

所以,如果我采用itemIndex=0(“CompresorDeAire”),我将创建:“TextBox”,“Calendar”,“TextBox”;

如果我采用index=1(CompresorUnidadDeRefrigeracion)控件是:“DropDownList”,“TextBox”,“Calendar”,“Calendar”,“TextBox”......但是有一个“DropDownList”控件,所以我将用这个信息填写它:

private string[] CompresorUnidadDeRefrigeracionTipoCompresor = new string[] { "Compresor Alternativo", "Compresor de Tornillo", "Unidad de Refrigeración" };

等等。这是代码:

public partial class Controles : System.Web.UI.Page
{
    private Label _Label;
    private TextBox _TextBox = new TextBox();
    private Calendar _Calendar = new Calendar();
    private DropDownList _DropDownList = new DropDownList();

    private string[] CompresorDeAire = new string[] { "TextBox", "Calendar", "TextBox" };
    private string[] CompresorUnidadDeRefrigeracion = new string[] { "DropDownList", "TextBox", "Calendar", "Calendar", "TextBox" };
    private string[] CompresorUnidadDeRefrigeracionTipoCompresor = new string[] { "Compresor Alternativo", "Compresor de Tornillo", "Unidad de Refrigeración" };
    private string[] BombaElectrica = new string[] { "TextBox", "TextBox", "TextBox", "TextBox", "TextBox", "TextBox" };

    protected void Page_Load(object sender, EventArgs e)
    {
       LoadInfo(CompresorDeAire);
    }

    private void LoadInfo(string[] Arreglo)
    {
        for (int i = 0; i < Arreglo.Length; i++)
        {
            _Label = new Label();
            _TextBox = new TextBox();
            _Calendar = new Calendar();
            _DropDownList = new DropDownList();

            _Label.Text = Arreglo[i].ToString() + i.ToString();
            _Label.ID = _Label.Text;
            PlaceHolder1.Controls.Add(_Label);
            PlaceHolder1.Controls.Add(new LiteralControl("<br />"));

            if (Arreglo[i] == _TextBox.GetType().Name.ToString())
            {
                _TextBox.ID = "txt" + _Label.ID;
                //_TextBox.AutoPostBack = true;
                PlaceHolder1.Controls.Add(_TextBox);
            }
            else if (Arreglo[i] == _Calendar.GetType().Name.ToString())
            {
                _Calendar.ID = "cln" + _Label.ID;
                PlaceHolder1.Controls.Add(_Calendar);
            }
            else if (Arreglo[i] == _DropDownList.GetType().Name.ToString())
            {
                _DropDownList.ID = "cmb" + _Label.ID;

                //_DropDownList.AutoPostBack = true;
                foreach (var item in CompresorUnidadDeRefrigeracionTipoCompresor)
                {
                    int j = 0;
                    _DropDownList.Items.Add(item);
                    j++;
                }

                PlaceHolder1.Controls.Add(_DropDownList);
            }

            PlaceHolder1.Controls.Add(new LiteralControl("<br /><br />"));
        }
    }

    protected void cmbPrueba_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtMensaje.Text = "";
        PlaceHolder1.Controls.Clear();

        switch (cmbPrueba.SelectedIndex)
        {
            case 0:
                this.LoadInfo(CompresorDeAire);
                break;

            case 1:
                this.LoadInfo(CompresorUnidadDeRefrigeracion);
                break;

            case 2:
                this.LoadInfo(BombaElectrica);
                break;
        }
    }

    protected void btnActualizar_Click(object sender, EventArgs e)
    {
        txtMensaje.Text = "";

        for (int i = 0; i < PlaceHolder1.Controls.Count; i++)
        {
            switch (PlaceHolder1.Controls[i].GetType().Name.ToString())
            {
                case "TextBox":
                    TextBox TB = PlaceHolder1.FindControl(PlaceHolder1.Controls[i].ID) as TextBox;
                    txtMensaje.Text += PlaceHolder1.Controls[i].GetType().Name + " " + PlaceHolder1.Controls[i].ID + " " + TB.Text + "\n";
                    TB.Text += "*";

                    break;

                case "Calendar":
                    Calendar Cal = PlaceHolder1.FindControl(PlaceHolder1.Controls[i].ID) as Calendar;
                    txtMensaje.Text += PlaceHolder1.Controls[i].GetType().Name + " " + PlaceHolder1.Controls[i].ID + " " + Cal.SelectedDate.ToShortDateString() + "\n";
                    break;

                case "DropDownList":
                    DropDownList DD = PlaceHolder1.FindControl(PlaceHolder1.Controls[i].ID) as DropDownList;

                    txtMensaje.Text += PlaceHolder1.Controls[i].GetType().Name + " " + PlaceHolder1.Controls[i].ID + " " + DD.Text + "\n";
                    break;
            }
        }
    }

    protected void btnLimpiar_Click(object sender, EventArgs e)
    {
        PlaceHolder1.Controls.Clear();
        txtMensaje.Text = "";
    }
}

当我默认运行代码为Index = 0时,我使用文本框和日历,然后点击“Actualizar”,当我选择Index=1时,我可以在文本框中看到信息(和加载第二个数组)显示所有新控件,但如果我选择一个日期或我在文本框中写入并单击按钮“Actualizar”页面将返回上一页(数组1)。

感谢您的帮助!感谢。

1 个答案:

答案 0 :(得分:0)

我假设你说“页面返回上一页(数组1)。”你的意思是第一个数组(在第零个元素中)

问题是.NET不会在回发时自动为您重新创建动态控件。你必须处理它。

以下是第一页请求的基本步骤:

  • 执行Page_load事件,该事件为CompresorDeAir调用LoadInfo。

然后,当您在下拉列表中选择了不同的条目,然后单击“实施器”按钮,然后它会回复这些基本步骤:

  • 执行Page_load事件,该事件为CompresorDeAir调用LoadInfo。

  • 执行cmbPrueba_SelectedIndexChanged,它会丢弃页面加载中添加的动态控件并加载所选索引的控件。

  • 执行btnActualizer_Click事件,该事件显示动态占位符中的控件,这些是所选下拉值的控件。

然后,当您更改文本或日期,然后单击“实施器”按钮时,它将执行以下步骤:

  • 执行Page_load事件,该事件为CompresorDeAir调用LoadInfo。

  • 执行btnActualizer_Click事件,该事件显示动态占位符中的控件。在这种情况下,显示页面加载的那些。来自先前所选下拉列表项的控件不会被创建。

下拉列表中所选项目的控件添加到占位符的唯一时间是所选项目更改为下拉列表。

解决方案在表单中放入一个隐藏变量,以保存下拉列表中的最后一个选定项。每次选定的索引更改时,都会更新此隐藏值。在页面加载事件中,在回发上,根据该隐藏值加载相应的数组。