Home > Software design >  Create multiple select dropdown on server side
Create multiple select dropdown on server side

Time:06-22

I want to create multiple select dropdown on the server side. Is that possible and if that possible how? How can I write this code on the server- side?

<dx:ASPxDropDownEdit ClientInstanceName="checkComboBox" ID="ASPxDropDownEdit" runat="server" AnimationType="None" CssClass="otherItemTextBox" ItemStyle-CssClass="detailControl" Width="400px" Caption="<%$ Resources:tables, TabPageNameLabel %>" CaptionCellStyle-CssClass="otherItemLabel">
<DropDownWindowStyle BackColor="#EDEDED" />
<DropDownWindowTemplate>
    <dx:ASPxListBox Width="100%" ID="ListBoxLookup" ClientInstanceName="checkListBox" SelectionMode="CheckColumn"
        runat="server" Height="200" EnableSelectAll="true" EnableSynchronization="True" OnDataBound="ListBoxLookup_DataBound">
        <FilteringSettings ShowSearchUI="true" />
        <Border BorderStyle="None" />
        <BorderBottom BorderStyle="Solid" BorderWidth="1px" BorderColor="#DCDCDC" />
        <ClientSideEvents SelectedIndexChanged="updateText" Init="updateText" />
    </dx:ASPxListBox>
    <table style="width: 100%">
        <tr>
            <td style="padding: 4px">
                <dx:ASPxButton ID="CloseButton" AutoPostBack="False" runat="server" Text="Close" Style="float: right">
                    <ClientSideEvents Click="function(s, e){ checkComboBox.HideDropDown(); }" />
                    <Image IconID="actions_close_16x16devav"></Image>
                </dx:ASPxButton>
                <dx:ASPxButton ID="DataSearchButton" AutoPostBack="true" runat="server" Text="Search" Style="float: right" OnClick="DataSearchButton_Click">
                    <Image IconID="actions_search_16x16devav"></Image>
                </dx:ASPxButton>
            </td>
        </tr>
    </table>
</DropDownWindowTemplate>
<ClientSideEvents TextChanged="synchronizeListBoxValues" DropDown="synchronizeListBoxValues" />
</dx:ASPxDropDownEdit>

CodePudding user response:

If you are creating controls dynamically on the server-side then you need to make sure you re-create them for the postback. Otherwise, the viewstate will fail when you postback. Controls must be in the same position and with the same id on a postback as they were when they were originally written in the response.

For "Page" you need to re-create them during the OnInit For controls dynamically created in a "WebUserControl", you must re-create them in the onl oad event.

Generally:

Private Sub createMyControls()
    Panel1.Controls.Clear
    Dim dl1 As DropDownList = New DropDownList
    Dim dl2 As DropDownList = New DropDownList
    dl1.Id = "DropDownList1"
    dl1.Items.Add(New ListItem("Item1"))
    dl1.Items.Add(New ListItem("Item2"))
    dl2.Id = "DropDownList1"
    dl2.Items.Add(New ListItem("Item1"))
    dl2.Items.Add(New ListItem("Item2"))
    Panel1.Controls.Add(dl1)
    Panel1.Controls.Add(dl2)
End Sub

Protected Sub Page_OnInit(sender as Object, e as EventArgs) Handles Me.OnInit
   ' We must re-create these controls on a postback
   If IsPostback Then createMyControls()
End Sub

Protected Sub Page_PreRender(sender as Object, e as EventArgs) Handles Me.OnPreRender
   ' If some data has changed you may want to re-create these controls
   createMyControls()
End Sub

CodePudding user response:

I recommend that you refer to the following article for instructions on how to create controls dynamically: How to create controls dynamically

According to this article, once you have modified the entire controls hierarchy (i.e., added the control into the controls collection), it is necessary to restore this control with the same settings during the Page_Init stage.

Example -- How to create controls dynamically

using System;
using System.Web.UI;

public partial class LoadWebUserControl: System.Web.UI.Page {
    protected void Page_Init(object sender, EventArgs e) {
        if (Session["Name"] != null)
            LoadUserControl(Session["Name"].ToString());
    }

    protected void ASPxComboBox1_SelectedIndexChanged(object sender, EventArgs e) {
        string ucName = ASPxComboBox1.SelectedItem.Text;
        LoadUserControl(ucName);
        Session["Name"] = ucName;
    }

    private void LoadUserControl(string ucName) {
        Control control = LoadControl(ucName);
        control.ID = ucName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries)[0];
        control.EnableViewState = false;
        ASPxRoundPanel1.Controls.Clear();
        ASPxRoundPanel1.Controls.Add(control);
    }
}
  • Related