簡體   English   中英

下拉菜單默認選擇值

[英]Dropdown default select value

這是一個下拉控件,用於綁定數據,在綁定之后,我將放置select語句。 即使索引保持為0,總會這樣選擇最后一個:

電流輸出:

india
Auz
US
--select--

要求的輸出:

--select--
india
AUZ
US

我的密碼

ddlcounty.DataSource = dtNew;
ddlcounty.DataTextField = "Weight";
ddlcounty.DataValueField = "Weight";
ddlcounty.DataBind();

ddlcounty.Items.Add("--Select--");
ddlcounty.SelectedValue = "0";

此處需要進行哪些更改?

謝謝

您首先要進行綁定。

當您到達要添加默認條件的部分時,實際上是在列表的末尾。

代替 :-

ddlcounty.Items.Add("--Select--");

做:-

ddlcounty.Items.Insert(0, new ListItem("--Select--"));

這會將您的默認選項作為Items的第一個元素插入。

宣布編輯

您將不需要:-

ddlcounty.SelectedValue = 0;

..就像您沒有明確指定一樣,將自動選擇下拉列表中的第一項。

但是,如果您想對此進行明確說明,可以執行以下操作:

ddlcounty.Items.Insert(0, new ListItem("--Select--","0"));
ddlcounty.SelectedValue = 0;

請您嘗試以下方式:

只需將AppendDataBoundItems設置為true並在選定項之前插入ListItem為選定項,然后插入“ ClearSelection”,如下所示。

ddlcounty.AppendDataBoundItems = true;
ddlcounty.DataSource = dtNew;
ddlcounty.DataTextField = "Weight";
ddlcounty.DataValueField = "Weight";
ddlcounty.DataBind();

ddlcounty.ClearSelection();
ddlcounty.Items.Insert(0, new ListItem { Value = "0", Text = "--Select--", Selected = true });
ddlcounty.SelectedValue = "0";

您還可以在aspx頁面中聲明性地聲明“選擇一個” ListItem,如下所示

 <asp:DropDownList ID="ddUIC" runat="server" AppendDataBoundItems="true" Width="200px" BackColor="White" Font-Size="10px" SelectedValue='<%# Bind("Weight") %>' DataTextField="Weight" DataValueField="Weight" >
     <asp:ListItem Text="Select One" Value=""></asp:ListItem>
 /asp:DropDownList>

但是您的AppendDataBoundItems必須設置為true

而且您仍然可以在后端執行數據綁定。

暫無
暫無

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

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