asp.net 下拉列表

发布于 2024-11-30 07:07:48 字数 1026 浏览 1 评论 0原文

目前我有一个下拉列表,如下所示:

<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 技术交流群。

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

发布评论

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

评论(1

树深时见影 2024-12-07 07:07:48

1.为了从下拉列表中删除项目,您要做的就是循环遍历下拉列表的 ListItem 并比较 的值字段 (DataValueField) ListItem 项目包含已更改或删除的选择,如果匹配,则将其从下拉列表中删除。例如:

private void removeItem(string ID)
    {
        for (int i = 0; i < dropdownList.Items.Count; i++)
            if (dropdownList.Items[i].Value == ID)
            {
                dropdownList.Items.RemoveAt(i);
                break;
            }
    }

2.绑定下拉列表后,在代码后面,在Index 0处添加一个新的ListItem,它将解决您将“Select”或Blank显示为“Select”或“Blank”的问题最上面的选择:

dropdownList.DataBind();
dropdownList.Items.Insert(0, new ListItem("Select"));

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) of ListItem item with the selection that has changed or deleted, remove it from dropdown list if it matches. e.g.:

private void removeItem(string ID)
    {
        for (int i = 0; i < dropdownList.Items.Count; i++)
            if (dropdownList.Items[i].Value == ID)
            {
                dropdownList.Items.RemoveAt(i);
                break;
            }
    }

2.Just after binding your dropdownlist, in code behind, add a new ListItem at Index 0, it will solve your problem of displaying "Select" or Blank as a top most selection:

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