簡體   English   中英

僅當項目列表不為空時如何強制選擇 DropDownList

[英]How to make DropDownList selection mandatory only if item list is not empty

我有兩個 DropDownLists; ddl1 和 ddl2,以及我的 web 表單中的其他控件。 當用戶 select 來自 ddl1 的項目時,項目會相應地從數據庫添加到 ddl2 中。 所以有可能沒有項目添加到 ddl2 並且它仍然是空的。 如果在提交 web 表單之前列表項不為空(在 ddl2 上驗證提交按鈕),我想強制 select 來自 ddl2 的適當值。

更新:

<td class="col-46">
            <asp:DropDownList ID="DropDownList14" runat="server" 
                AutoPostBack="True" 
                onselectedindexchanged="DropDownList14_SelectedIndexChanged">
                <asp:ListItem>Select Current Class</asp:ListItem>
                <asp:ListItem Value="-2">PG</asp:ListItem>
                <asp:ListItem Value="-1">LKG</asp:ListItem>
                <asp:ListItem Value="0">UKG</asp:ListItem>
                <asp:ListItem>1</asp:ListItem>
                <asp:ListItem>2</asp:ListItem>
                <asp:ListItem>3</asp:ListItem>
                <asp:ListItem>4</asp:ListItem>
                <asp:ListItem>5</asp:ListItem>
                <asp:ListItem>6</asp:ListItem>
                <asp:ListItem>7</asp:ListItem>
                <asp:ListItem>8</asp:ListItem>
                <asp:ListItem>9</asp:ListItem>
                <asp:ListItem>10</asp:ListItem>
                <asp:ListItem>11</asp:ListItem>
                <asp:ListItem>12</asp:ListItem>
            </asp:DropDownList>        
        </td>
        
                                <td class="col-46">
            <asp:DropDownList ID="DropDownList15" runat="server">
            </asp:DropDownList>        
        </td>

and.aspx頁面:

protected void DropDownList14_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList15.Items.Clear();
        DropDownList15.ClearSelection();
        if (DropDownList14.SelectedIndex > 0)
        {
            DropDownList15.Items.Add("Select Section");
            SqlCommand cmd = new SqlCommand("select distinct section from feestructure where class = '" + DropDownList14.SelectedValue + "' and session =@ssn", agr);
            cmd.Parameters.Add("ssn", DateTime.Parse(TextBox1.Text));
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (dr[0].ToString().Trim() != "")
                {
                    DropDownList15.Items.Add(dr[0].ToString().Trim());
                }
            }
            dr.Dispose();            
        }
    }

ddl1 => DropDownList14

ddl2 => DropDownList15

如果它有項目,我希望強制選擇 DropDownList15(需要驗證器)。

它非常簡單。 您可以在任何下拉列表中添加所需的屬性。 像這樣創建一個下拉列表,並為該下拉列表添加一個 RequiredFieldValidator。

<asp:DropDownList ID="FirstDD" CssClass="form-control custom-select" runat="server"></asp:DropDownList>
<asp:RequiredFieldValidator runat="server" ControlToValidate="FirstDD"
                                        CssClass="text-danger" ErrorMessage="Required." />

和 oncodebehind 添加這一行..

 FirstDD.Items.Add(new ListItem("Select", ""));

或者在 aspx 頁面上,在 asp:DropDownList 標簽內添加這一行

<asp:ListItem Text="Select" Value=" "></asp:ListItem>

暫無
暫無

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

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