locked
Dropdown RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have below code on Page Load in !IsPOstBack . When i click on Apple nothing Happens . When i define first index as space then it works {"","Apple","Banana","Orange"};

    ddlfruits.DataSource = new List<string>() {"Apple","Banana","Orange"};
    ddlfruits.DataBind();

    Thanks

    Saturday, September 22, 2018 5:16 PM

Answers

  • User-1499457942 posted

    Hi

      I have used like below

      ListItemCollection items = new ListItemCollection();
                            items.Add(new ListItem("Orange", "0"));
                            items.Add(new ListItem("Banana", "1"));
                            items.Add(new ListItem("Mango", "2"));
                            ddlFruit.DataSource = items;
                            ddlFruit.DataBind();
    Thanks
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 24, 2018 6:25 AM

All replies

  • User-893317190 posted

    Hi JagjitSingh,

    Your description is not complete.

    What do you mean by "nothing happens"? What do you expect to happen?

    What's the meaning of "it works"?

    If you don't  post  more of your related code and specify your description , we could not know what you want.

    Best regards,

    Ackerly Xu

    Monday, September 24, 2018 2:34 AM
  • User-1499457942 posted

    Hi

      I have used like below

      ListItemCollection items = new ListItemCollection();
                            items.Add(new ListItem("Orange", "0"));
                            items.Add(new ListItem("Banana", "1"));
                            items.Add(new ListItem("Mango", "2"));
                            ddlFruit.DataSource = items;
                            ddlFruit.DataBind();
    Thanks
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 24, 2018 6:25 AM
  • User-1499457942 posted

    Hi

      I have below code . When i click on Apple nothing happens. It does not go in Apple()

    ListItemCollection items = new ListItemCollection();
                            items.Add(new ListItem("Apple", "0"));
                            items.Add(new ListItem("Mango", "1"));
                            items.Add(new ListItem("Orange", "2"));
    
    
                            ddlProcess.DataSource = items;
                            ddlProcess.DataBind();
    
    protected void ddlProcess_SelectedIndexChanged(object sender, EventArgs e)
            {
    
                    Int32 index = ddlProcess.SelectedIndex;
    
                    if (index == 0)
                    {
                        Apple();
                    }
                    if (index == 1)
                    {
                        Mango();
                    }
                    if (index == 2)
                    {
                        Orange();
                    }
    }
    

    Thanks

    Tuesday, September 25, 2018 3:01 PM
  • User475983607 posted

    This symptom is generally caused by re-binding the dropdown on every request rather binding the dropdown on the initial page request.   It also highlights your reluctance to perform even basic troubleshooting even after 1500+ posts and repeated recommendations to set a breakpoint.

    Example code.

        <div class="row">
            <div class="col-md-12">
                <asp:DropDownList ID="ddlProcess" runat="server" OnSelectedIndexChanged="ddlProcess_SelectedIndexChanged"></asp:DropDownList>
                <asp:Button ID="Button1" runat="server" Text="Button" />
                <hr />
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </div>
        </div>
    namespace WebFormsApp
    {
        public partial class _Default : Page
        {
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    ListItemCollection items = new ListItemCollection();
                    items.Add(new ListItem("Apple", "0"));
                    items.Add(new ListItem("Mango", "1"));
                    items.Add(new ListItem("Orange", "2"));
    
    
                    ddlProcess.DataSource = items;
                    ddlProcess.DataBind();
                }
            }
    
            protected void ddlProcess_SelectedIndexChanged(object sender, EventArgs e)
            {
                Int32 index = ddlProcess.SelectedIndex;
    
                if (index == 0)
                {
                    //Apple();
                    Label1.Text = "Apple";
                }
                if (index == 1)
                {
                    //Mango();
                    Label1.Text = "Mango";
                }
                if (index == 2)
                {
                    //Orange();
                    Label1.Text = "Orange";
                }
            }
        }
    }

    Tuesday, September 25, 2018 3:48 PM