如何仅在列表打开时将下拉菜单中的选项设为第一个选项?

发布于 2024-12-09 00:53:49 字数 413 浏览 2 评论 0原文

我有一个 DropDownList,其中填充了 SQL 查询返回的值。填充列表后,我手动向此下拉菜单添加两个选项:-- Select --ALL

页面加载时,DropDownList 显示 -- Select -- 作为默认值。当用户打开 DropDownList 时,SQL 查询返回的列表也会与 ALL 选项一起打开。

我想让这个 ALL 在列表打开时显示为列表中的第一项。我无法使用: DropDownList.SelectedIndex 因为我想已经将 -- Select -- 作为所选索引。

如何使 ALL 成为列表打开时的第一个选项?

I have a DropDownList that is filled with values returned by a SQL Query. After the list is filled, I add two options manually to this DropDown: -- Select -- and ALL.

When the page loads, the DropDownList shows -- Select -- as the default value. When the user opens the DropDownList, the SQL Query returned list opens along with ALL option also.

I want to make this ALL appear as the fist item in the list when the list opens. I can't used: DropDownList.SelectedIndex because I want have already made -- Select -- as the selected index.

How to make ALL the first option when the list opens?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青萝楚歌 2024-12-16 00:53:49

我想一种解决方案是结合使用文本框/下拉列表。并在文本框中选择默认文本。下拉列表中的第一个选项是“全部”。然后禁用文本框的编辑,使其显示为常规下拉列表。

像这样: 文本框和下拉列表组合

希望这有帮助。

I guess one solution would be to use a textbox / dropdownlist combined. And have the select text as default in the textbox. And the first option in dropdownlist would be ALL. Then disable the editing of the textbox to make it appear as a regular dropdownlist.

Like this: Textbox and dropdownlist combined

Hope this helps.

咿呀咿呀哟 2024-12-16 00:53:49

据我所知,您的梦想是拥有这样的下拉列表

SELECT(this one comes as selected)
ALL(the first item in the dropdownlist)    
V1(from your query)
V2(from your query)
V3(from your query)
..
.   

您可以这样做

string selectStr = "SELECT";
string allStr = "ALL"

ListItem allLI = new ListItem(allStr,allStr);        
ListItem selectLI = new ListItem(selectStr,selectStr);

DropDownList.Items.Add(selectLI);
DropDownList.Items.Add(allLI);


//code to fill the DropDownList with the list that your query returns

DropDownList.SelectedValue = selectStr;

As much as I understand your dream is to have a dropdownlist like this

SELECT(this one comes as selected)
ALL(the first item in the dropdownlist)    
V1(from your query)
V2(from your query)
V3(from your query)
..
.   

You can do this

string selectStr = "SELECT";
string allStr = "ALL"

ListItem allLI = new ListItem(allStr,allStr);        
ListItem selectLI = new ListItem(selectStr,selectStr);

DropDownList.Items.Add(selectLI);
DropDownList.Items.Add(allLI);


//code to fill the DropDownList with the list that your query returns

DropDownList.SelectedValue = selectStr;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文