DropDownList Items.FindByText 正在插入:" 选定="真



我有一个 ASP.NET(VB)下拉列表,其中列出了供用户在表单上选择的国家/地区。

前面的代码:

<asp:DropDownList ID="DropDownListCountry" runat="server" CssClass="form-control" 
    DataSourceID="SqlDataSourcecountryList" DataTextField="country_name_long" DataValueField="country_name_short">
</asp:DropDownList>

仅供参考,country_name_longcountry_name_short在此数据库中包含相同的值/文本。

在后面的代码中,我选择"英国"作为默认的选定项:

DropDownListCountry.DataBind()
DropDownListCountry.Items.FindByText("United Kingdom").Selected = True

我也尝试使用它,但它产生了相同的结果:

DropDownListCountry.DataBind()
DropDownListCountry.SelectedIndex = DropDownListCountry.Items.IndexOf(DropDownListCountry.Items.FindByText("United Kingdom"))

当我查看最终结果 HTML 的源代码时,这段代码似乎插入了一些非常错误的东西:

<option value="Ukraine">Ukraine</option>
<option value="United Arab Erimates">United Arab Emirates</option>
<option selected="selected" value="United Kingdom&quot; Selected=&quot;True">United Kingdom</option>
<option value="United States of America">United States of America</option>
<option value="Uraguay">Uruguay</option>

您将从英国行中看到一些 HTML 似乎是动态生成的。

预期成果:

    <option selected="selected" value="United Kingdom">United Kingdom</option>

实际结果:

    <option selected="selected" value="United Kingdom&quot; Selected=&quot;True">United Kingdom</option>

这里有什么问题/损坏/故障?

与其

.SelectedIndex尝试:

    DropDownListCountry.SelectedValue = DropDownListCountry.Items(
DropDownListCountry.Items.FindByText("United Kingdom"))

最新更新