[英]setting selected option in asp.net using c#
我在aspx頁面中有以下代碼
<asp:DropDownList ID="txtSupplierCountry" class="form-control" Width="230px" runat="server">
<asp:ListItem Text="Select gender" Value="0"></asp:ListItem>
<asp:ListItem Text="male" Value="0"></asp:ListItem>
<asp:ListItem Text="female" Value="1"></asp:ListItem>
</asp:DropDownList>
我在數據庫中具有男性/女性的價值。 我需要使用C#代碼設置此文本。 基本上,我試圖基於數據庫中的文本設置選定的選項。 我沒有主意。 我試過下面的代碼,但是不起作用。
String t = "male" // coming from db
txtSupplierCountry.Items.FindByText(t).Selected = true;
當您保存數據Male和Female時,為什么將下拉值保留為1,2等數字,請更改代碼。
<asp:DropDownList ID="txtSupplierCountry" class="form-control" Width="230px" runat="server">
<asp:ListItem Text="Select Country" Value="0"></asp:ListItem>
<asp:ListItem Text="male" Value="male"></asp:ListItem>
<asp:ListItem Text="female" Value="female"></asp:ListItem>
然后
txtSupplierCountry.SelectedValue="male";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.