簡體   English   中英

動態控件消失了ASP.NET C#(加載控件取決於DropDownList選擇)

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

我是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個元素中)

問題是.NET不會在發回時自動為您重新創建動態控件。 您必須處理。

這是首頁請求的基本步驟:

  • 執行Page_load事件,該事件為CompresorDeAir調用LoadInfo。

然后,當您在下拉列表中選擇其他條目然后單擊“實現器”按鈕時,它將執行以下基本步驟的回發:

  • 執行Page_load事件,該事件為CompresorDeAir調用LoadInfo。

  • 執行cmbPrueba_SelectedIndexChanged,它將丟棄在頁面加載中添加的動態控件,並為所選索引加載該控件。

  • 執行btnActualizer_Click事件,該事件顯示動態占位符中的控件,這些控件是所選下拉值的控件。

然后,當您更改文本或日期並單擊“實現器”按鈕時,將執行以下步驟:

  • 執行Page_load事件,該事件為CompresorDeAir調用LoadInfo。

  • 執行btnActualizer_Click事件,該事件顯示動態占位符中的控件。 在這種情況下,將顯示頁面加載中的內容。 不會從先前選擇的下拉列表項中創建控件。

下拉列表中所選項目中的控件唯一添加到占位符的時間是當所選項目更改為下拉菜單時。

解決方案是在表單中放置一個隱藏變量,以保存下拉菜單中最后選擇的項目。 每次所選索引更改時,然后更新此隱藏值。 在頁面加載事件中,在回發時,基於該隱藏值加載適當的數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM