Listview set dropdown selected value in code behind RRS feed

  • Question

  • User444756084 posted

    I have a dropdown within a Listview. I am trying to set the dropdown selected value in code behind to the value = "Special" which is a value in the list. However it is not working.

    ListView lv = (ListView)Wizard1.FindControl("ListView3");
    DropDownList cno2 = (DropDownList)lv.InsertItem.FindControl("AddIfHoursLessClassDD");
      // ALSO TRIED THIS - didnot work        
      // cno2.Items.FindByValue("Special").Selected = true;
    //setting value in jquery to access in code behind
     $('#AddIfHoursLessCourseDD').val("Special").attr('selected', 'true');
    //dropdown (it is being preset)
     <asp:DropDownList runat="server" ID="AddIfHoursLessCourseDD" DataSourceID="sdsClasses" DataTextField="Name" DataValueField="CATALOG_NBR" AutoPostBack="true" ToolTip="Select the Class."  onselectedindexchanged="ddlClass_SelectedIndexChanged" /><asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" ErrorMessage="Class number required" Display="Dynamic" ValidationGroup="AddEval" ControlToValidate="ddlClass" Font-Bold="True" ForeColor="#CC0000"></asp:RequiredFieldValidator>

    Friday, October 4, 2019 1:54 AM


  • User-719153870 posted

    Hi gordon1221,

    I am trying to set the dropdown selected value in code behind to the value = "Special" which is a value in the list. However it is not working.

    The reason why this behid code failed is because in fact you can't find your dropdownlists with FindControl("AddIfHoursLessClassDD"), the id of these ddls will be changed when your page get rendered to html and your code might generate below error:

    The id of these ddls will be changed to like, Wizard1_ListView3_AddIfHoursLessClassDD_0, in my demo case, it changed to below:

    You can use your browser devtool to see how your ddls' id get changed.

    Thus, on the one hand, if you want to change the selected item in the behind code, the code should be updated like below:

    ListView lv = (ListView)Wizard1.FindControl("ListView3");
    DropDownList cno2 = (DropDownList)lv.InsertItem.FindControl("Wizard1_ListView3_AddIfHoursLessClassDD_0");
    cno2.Items.FindByValue("Special").Selected = true;

    Or you can use a loop to make this event dynamical.

    On the other hand, if you want to do this job in the client side with jquery, you can follow below code and also refer to How to set value for dropdown list in javascript :

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <script src="Scripts/jquery-3.3.1.min.js"></script>
            $(function () {
        <form id="form1" runat="server">
                <asp:SqlDataSource ID="SqlDataSource1" ConnectionString="Data Source=.;Initial Catalog=DatabaseTestPool;Integrated Security=True" SelectCommand="select * from Users" runat="server"></asp:SqlDataSource>
                <asp:Wizard ID="Wizard1" runat="server">
                        <asp:WizardStep ID="WizardStep1" runat="server" Title="Step 1">
                            <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
                                            <%#Eval("UID") %>
                                            <%#Eval("UName") %>
                                            <%#Eval("UAge") %>
                                            <asp:DropDownList ID="DropDownList1" runat="server">
                                                <asp:ListItem Text="1" Value="1">
                                                <asp:ListItem Text="2" Value="2">
                                                <asp:ListItem Text="3" Value="3">
                                                <asp:ListItem Text="Special" Value="Special">
                        <asp:WizardStep ID="WizardStep2" runat="server" Title="Step 2"></asp:WizardStep>

    You will get this result:

    In this case, you will only need to change the jquery selector to select specific ddl.

    Hope this could help.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 7, 2019 5:30 AM