locked
dropdownlist Selected Index RRS feed

  • Question

  • User-253940806 posted

    I'm using web forms. I have dropdownlist  for a crud system. When I go to the edit (update), I want to use database values for the the selected index . I've look through the forums and on google, but I'm trying to find the easiest way to achieve this.

    <asp:dropdownlist id="Active" runat="server">
    <asp:ListItem Value="1">Active</asp:ListItem>
    <asp:ListItem Value="0">Inactive</asp:ListItem> 
    </asp:dropdownlist>
    protected void Active_SelectedIndexChanged(object sender, System.EventArgs e)
    
    {
    ddlActive.SelectedIndex = ce.Active;
    }

    Wednesday, September 26, 2018 1:46 PM

Answers

All replies

  • User475983607 posted

    The first step is to learn how to use/read the API documentation. 

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.dropdownlist?redirectedfrom=MSDN&view=netframework-4.7.2

    The DropDownList is a type of ListControl. The ListControl has a SelectedValue and a SelectedItem property that sets the selections  simply set the SelectedValue to the value in your database as shown in the API docs.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.listcontrol.selectedvalue?view=netframework-4.7.2

    Keep in mind, it is not possible to provide further assistance as we can not see your database schema and have no idea how your data is stored or fetched.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 26, 2018 2:29 PM
  • User-253940806 posted

    I've read through some of those articles too. It would be nice if the API documentation had working examples too.

    Wednesday, September 26, 2018 3:30 PM
  • User475983607 posted

    It would be nice if the API documentation had working examples too.

    As far as I know the API source code functions as expected. 

    Complete the Feedback section at the end of the documentation if you found a bug in the code.

    Wednesday, September 26, 2018 4:09 PM
  • User-253940806 posted

    If you could pass it along that including a working example would be beneficial, I would appreciate it.

    Wednesday, September 26, 2018 4:12 PM
  • User475983607 posted

    I'm not sure what you're asking as the linked document clearly shows how to assign the select value.  If you are looking for an example that matches your exact business requirement then you'll need to post the relevant bits of code and SQL.  It should be as simple as executing a query and assigning the value.

          void Button_Click(Object sender, EventArgs e)
          {
    
             // Perform this operation in a try-catch block in case the item is not found.
             try
             {
                List.SelectedValue = ItemTextBox.Text;
                MessageLabel.Text = "You selected " + List.SelectedValue + ".";
             }
             catch (Exception ex)
             {
                List.SelectedValue = null;
                MessageLabel.Text = "Item not found in ListBox control.";
             }
                 
          }

    In the sample above the select value comes from a textbox.  Replace that highlighted with code that gets the selected value from your DB table.  

    Wednesday, September 26, 2018 4:51 PM