簡體   English   中英

Aspx下拉列表未顯示最高價值

[英]Aspx dropdownlist not displaying top value

我有一個aspx頁面(C#代碼頁)。 我有一些硬編碼的下拉列表,由於某種原因,他們沒有顯示頂部列表項(值)。 我添加了一個額外的頂部列表項(值),現在顯示已經存在的值的正確值,但是不顯示額外的值。

我在C#代碼中使用下拉列表執行的唯一功能是隱藏或顯示它們。 然后根據所選值進行驗證或不進行驗證。

我的aspx代碼:

<asp:DropDownList ID="ddlAction" runat="server" Visible="True" 
    AppendDataBoundItems="true" Height="25px" Width="149px">
    <asp:ListItem Value="Select">Please Select</asp:ListItem>
    <asp:ListItem>Yes</asp:ListItem>
    <asp:ListItem>No</asp:ListItem>
</asp:DropDownList>

C#代碼:

ddlAction.Visible = false;
ddlAction.Visible = true;

我經常使用dropdownlist,從來沒有遇到過這個問題。 有沒有人有任何想法可能是什么問題?

更新到這個問題:

我根據Rahul在C#代碼中添加了我的項目。 做了快速測試並且有效。 今天早上,我再一次得到第一項(“請選擇”)的空白。

Aspx代碼:

<asp:DropDownList ID="ddlAction" runat="server" 
 AppendDataBoundItems="True" Height="27px" Width="159px">
 </asp:DropDownList>

C#代碼:

ddlAction.Visible = true;
ddlAction.AppendDataBoundItems = true;
ddlAction.Items.Insert(0, new ListItem("Please Select","Select"));
ddlAction.Items.Insert(1, new ListItem("Yes", "Yes"));
ddlAction.Items.Insert(2, new ListItem("No", "No"));
ddlAction.DataBind();

呈現的源代碼:

  &nbsp;<select name="ctl00$ContentPlaceHolder1$ddlAction" id="ContentPlaceHolder1_ddlAction" style="height:27px;width:159px;">
<option selected="selected" value="Select"></option>
<option value="Yes">Yes</option>
<option value="No">No</option>

在aspx coe中使用AppendDataBound = true

<asp:DropDownList ID="ddlAction" AppendDataBound = true runat="server" Visible="True" Height="25px" 
                            Width="149px">
                            <asp:ListItem Value="Select">Please Select</asp:ListItem>
                            <asp:ListItem>Yes</asp:ListItem>
                            <asp:ListItem>No</asp:ListItem>
                        </asp:DropDownList>

編輯1

關於列表項的更多細節

 <asp:ListItem Value="-2" Text="Please Select"></asp:ListItem>
 <asp:ListItem Value="0" Text="Yes"></asp:ListItem>
 <asp:ListItem Value="-1" Text="No"></asp:ListItem>

嘗試將AppendDataBoundItems = true屬性用於.aspx頁面。

您也可以從后面的代碼中分配值

ddlAction.Items.Insert(0, new ListItem(String.Empty, String.Empty));

我建議您使用其內部屬性聲明DropDownList ListItems並定義ListItem必須是所選的ListItem

    <asp:DropDownList ID="ddlAction" runat="server" Visible="True" AppendDataBoundItems="true" Height="25px" Width="149px">
        <asp:ListItem Text="Please Select" Value="Select" Selected="True"></asp:ListItem>
        <asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
        <asp:ListItem Text="No" Value="No"</asp:ListItem>
     </asp:DropDownList>

這是ASP.NET使用的方式,並將在回發時返回服務器端正確的選定值。

我認為您不必使用也不必使用DataBind()方法設置AppendDataBoundItems ,因為您已經插入了ListItems並且您沒有從數據庫加載選項!

我想你需要通過為DropDownList.SelectedIndex屬性設置一個值來告訴ListItemIndex是什么。

編輯

另外,嘗試閱讀有關AppendDataBoundItems屬性的MSDN文檔以及此處輸入鏈接描述方法。

暫無
暫無

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

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