简体   繁体   English

动态控件消失了ASP.NET C#(加载控件取决于DropDownList选择)

[英]Dynamic controls disappear ASP.NET C# (Loading controls depending on a DropDownList selection)

I'm new in ASP.NET; 我是ASP.NET的新手。 I have a DropDownList in a page (with a masterpage): 我在一个页面(带有母版页)中有一个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>

Depending of the DropDownList (cmbPrueba) the placeHolder creates controls using the string array; 根据DropDownList(cmbPrueba),placeHolder使用字符串数组创建控件; (I made the string arrays simulating string result of database). (我做了字符串数组来模拟数据库的字符串结果)。

So, if I take itemIndex=0 ("CompresorDeAire) I will create: "TextBox", "Calendar", "TextBox"; 因此,如果我使用itemIndex=0 (“ CompresorDeAire),我将创建:” TextBox“,” Calendar“,” TextBox“;

if I take index=1 (CompresorUnidadDeRefrigeracion ) the controls are: "DropDownList", "TextBox", "Calendar", "Calendar", "TextBox"... but there is a "DropDownList" control, so I will full it with this info: 如果我采用index=1 (CompresorUnidadDeRefrigeracion),则控件为:“ DropDownList”,“ TextBox”,“ Calendar”,“ Calendar”,“ TextBox” ...,但是有一个“ DropDownList”控件,因此我将使用此信息:

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

And so on. 等等。 Here is the code: 这是代码:

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 = "";
    }
}

When I run the code by default is Index = 0 , I use the textbox and calendar, and click "Actualizar" and I can see the info in the text box, when I choose Index=1 (and load the 2nd array) all the new controls show up, but if I choose a date or I write in the textbox and click in the buttom "Actualizar" the page return to the previous page (array 1). 当我默认运行代码为Index = 0 ,我使用文本框和日历,然后单击“ Actualizar”,当我选择Index=1 (并加载第二个数组)时,我可以在文本框中看到信息。新的控件会出现,但是如果我选择一个日期,或者在文本框中输入内容,然后单击按钮“ Actualizar”,则该页面将返回上一页(数组1)。

I appreciate your help! 我感谢您的帮助! thanks. 谢谢。

I am assuming that when you say “the page return to the previous page (array 1).” That you mean the first array (in the zero-th element) 我假设当您说“页面返回到上一页(数组1)”时,您的意思是第一个数组(在第0个元素中)

The problem is that .NET will not automatically re-create the dynamic controls for you on the post back. 问题是.NET不会在发回时自动为您重新创建动态控件。 You have to handle that. 您必须处理。

Here are the basic steps for the first page request: 这是首页请求的基本步骤:

  • Execute the Page_load event, which calls LoadInfo for CompresorDeAir. 执行Page_load事件,该事件为CompresorDeAir调用LoadInfo。

Then when you selected a different entry in the dropdown and then click Actualizer button then it does a post back with these basic steps: 然后,当您在下拉列表中选择其他条目然后单击“实现器”按钮时,它将执行以下基本步骤的回发:

  • Execute the Page_load event, which calls LoadInfo for CompresorDeAir. 执行Page_load事件,该事件为CompresorDeAir调用LoadInfo。

  • Execute cmbPrueba_SelectedIndexChanged which throws away the dynamic controls that were added in the page load and loads the control for the selected index. 执行cmbPrueba_SelectedIndexChanged,它将丢弃在页面加载中添加的动态控件,并为所选索引加载该控件。

  • Execute btnActualizer_Click event which shows the controls in the dynamic place holder, these being the ones for the selected dropdown value. 执行btnActualizer_Click事件,该事件显示动态占位符中的控件,这些控件是所选下拉值的控件。

Then when you change the text or date and then click Actualizer button it does these steps: 然后,当您更改文本或日期并单击“实现器”按钮时,将执行以下步骤:

  • Execute the Page_load event, which calls LoadInfo for CompresorDeAir. 执行Page_load事件,该事件为CompresorDeAir调用LoadInfo。

  • Execute btnActualizer_Click event which shows the controls in the dynamic place holder. 执行btnActualizer_Click事件,该事件显示动态占位符中的控件。 In this case the ones from the page load are shown. 在这种情况下,将显示页面加载中的内容。 the controls from the prior selected dropdown list item do not get created. 不会从先前选择的下拉列表项中创建控件。

The only time the controls from the selected item in the dropdown list are added to the place holder is when the selected item changes for the dropdown. 下拉列表中所选项目中的控件唯一添加到占位符的时间是当所选项目更改为下拉菜单时。

The solution is put a hidden variable in the form to hold the last selected item from the drop down. 解决方案是在表单中放置一个隐藏变量,以保存下拉菜单中最后选择的项目。 Everytime the selected index changes then update this hidden value. 每次所选索引更改时,然后更新此隐藏值。 In the page load event, on a postback, load the appropriate array based on that hidden value. 在页面加载事件中,在回发时,基于该隐藏值加载适当的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式将dropdownList控件映射到ASP.NEt C#中的标签控件 - How to programatically map dropdownList Controls to Label Controls in ASP.NEt C# 关于表,动态控件,C#和asp.net的评论 - Comments on table, dynamic controls, C# and asp.net 动态C#asp.net控件的问题更新 - Troubles with dynamic c# asp.net controls update 通过DropDownList将动态ASP.NET控件添加到页面 - Add dynamic ASP.NET controls via a DropDownList to the page 在Asp.net C#中创建动态控件,并缓存控件和绑定数据 - Creating Dynamic Controls in Asp.net C#, and caching the controls and binding the Data 动态控件ASP.NET - Dynamic controls ASP.NET 检索动态asp.net复选框控件-控件已添加到转发器的页面中,但无法在回发(C#)中进行检索 - Retrieving dynamic asp.net checkbox controls - controls are added to page in repeater, but can't retrieve in postback (C#) (动态数据,ASP.Net,C#,下拉列表自定义.ascx控件)如何在下拉列表之间传递值? - (Dynamic Data, ASP.Net, C#, dropdownlists custom .ascx controls ) HOW TO PASS VALUES AMONG DROPDOWNLISTS? 在asp.net C#中的GridView中,动态控件在回发期间丢失了 - Dynamic Controls gets lost durig Postback inside GridView in asp.net C# DropDownList清除C#ASP.Net中的选择 - DropDownList Clear Selection in C# ASP.Net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM