asp.net 下拉列表
目前我有一个下拉列表,如下所示:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="SelectionHasChanged"
DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="ID"
Width="214px">
/asp:DropDownList>
asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDBConnectionString1 %>"
SelectCommand="SELECT [ID], Name], [Name] FROM [Names]">
/asp:SqlDataSource>
当更新或删除选择时,我试图让列表执行两件事: 1)从DropDownList
中删除已删除的内容 2)加载页面时在字段中找到数据库中的第一个条目,我希望它为空白或显示“选择”
当前我需要重新刷新页面以刷新已删除项目的下拉列表。
我尝试在各种方法(page_load、update、delete)中添加 DropDownList1.DataBind();
和 DropDownList1.DataSource = SqlDataSource1;
(但我收到一条消息,要求删除(SqlDataSource1?) 的对象
我添加了一个名为 EnableViewState="false"
的标记/控件,当我选择另一个项目时,这会刷新下拉列表,但是当我删除一个项目时,我需要立即刷新列表。
Currently I have a drop downlist as follows:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="SelectionHasChanged"
DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="ID"
Width="214px">
/asp:DropDownList>
asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDBConnectionString1 %>"
SelectCommand="SELECT [ID], Name], [Name] FROM [Names]">
/asp:SqlDataSource>
There are two things I'm trying to get the list to do when selection is either updated or deleted:
1)Remove deleted contents from DropDownList
2)The first entry in the database is found in the field when the page is loaded, I would like it to either be blank or say "Select"
Currently I need to refress the page to refresh the dropdownlist of deleted items.
I have tried adding DropDownList1.DataBind();
in various methods (page_load, update, delete) and DropDownList1.DataSource = SqlDataSource1;
(but I get a message to delete an object of (SqlDataSource1?)
I have added a tag/controll called EnableViewState="false"
, this refreshs the dropdownlist when I select another item, but when I delete an item I need the list to refresh right away.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1.为了从下拉列表中删除项目,您要做的就是循环遍历下拉列表的
ListItem
并比较的值字段 (
项目包含已更改或删除的选择,如果匹配,则将其从下拉列表中删除。例如:DataValueField
) ListItem2.绑定下拉列表后,在代码后面,在
Index
0处添加一个新的ListItem
,它将解决您将“Select”或Blank显示为“Select”或“Blank”的问题最上面的选择:1.In order to delete an item from your dropdown list, what you do is to loop through the
ListItem
of dropdown list compare the value field (DataValueField
) ofListItem
item with the selection that has changed or deleted, remove it from dropdown list if it matches. e.g.:2.Just after binding your dropdownlist, in code behind, add a new
ListItem
atIndex
0, it will solve your problem of displaying "Select" or Blank as a top most selection: