在 asp.net 中的绑定下拉列表中添加自定义选项

发布于 2024-08-27 09:13:26 字数 260 浏览 4 评论 0原文

我有一个绑定的下拉列表,通过选择和数据绑定填充了名称表。它拍摄 selectedindexchanged (通过回发)更新某个网格视图。

发生的情况是,由于它是通过更改索引来运行的,因此只有当您选择另一个索引,然后选择亚历山大时,才可以选择始终被选中的索引(亚历山大)。可怜的亚历山大。

我想要的是在开头放置一个 blanc 选项(默认),并在第二个选项(如果可能)放置一个选项。

我无法手动添加此选项,因为绑定会擦除下拉列表中的所有内容并放置数据源的内容。

I have a bound dropdown list populated with a table of names through a select, and databinding. it shoots selectedindexchanged that (through a postback) updates a certain gridview.

What happens is, since it runs from changing the index, the one that always comes selected (alexander) can only me chosen if you choose another one, then choose alexander. poor alexander.

What I want is to put a blanc option at the beginning (default) and (if possible) a option as second.

I can't add this option manually, since the binding wipes whatever was in the dropdown list and puts the content of the datasource.

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

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

发布评论

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

评论(2

温暖的光 2024-09-03 09:13:26

设置 AppendDataBoundItems 属性为真。添加空白,然后数据绑定。

ddl.AppendDataBoundItems = true;
ddl.Items.Add("Choose an item");
ddl.DataSource = foo;
ddl.DataBind();

AppendDataBoundItems 属性
允许您将项目添加到
数据之前的 ListControl 对象
发生绑定。数据绑定后,
items 集合包含
数据源中的项目和
之前添加的项目。

Set the AppendDataBoundItems property to True. Add your blank, then data bind.

ddl.AppendDataBoundItems = true;
ddl.Items.Add("Choose an item");
ddl.DataSource = foo;
ddl.DataBind();

The AppendDataBoundItems property
allows you to add items to the
ListControl object before data
binding occurs. After data binding,
the items collection contains both the
items from the data source and the
previously added items.

錯遇了你 2024-09-03 09:13:26
        protected void SetAddrList()
    {
        TestDataClassDataContext dc = new TestDataClassDataContext();
        dc.ObjectTrackingEnabled = false;

        var addList = from addr in dc.Addresses
                      from eaddr in dc.EmployeeAddresses
                      where eaddr.EmployeeID == _curEmpID && addr.AddressID == eaddr.AddressID && addr.StateProvince.CountryRegionCode == "US"
                      select new
                      {
                          AddValue = addr.AddressID,
                          AddText = addr.AddressID,
                      };
        if (addList != null)
        {
            ddlAddList.DataSource = addList;
            ddlAddList.DataValueField = "AddValue";
            ddlAddList.DataTextField = "AddText";
            ddlAddList.DataBind();
        }

        ddlAddList.Items.Add(new ListItem("<Add Address>", "-1"));
    }

我使用 Adventure Works 创建了这个代码示例,以便对 Linq 进行一些练习,它与之前的答案非常相似。使用 linq 对于最后一个 dddlAddList.Items.Add 的答案来说仍然不重要,这是您所需要的。 “添加地址”= 第一个选择的选项,-1 = 值。

        protected void SetAddrList()
    {
        TestDataClassDataContext dc = new TestDataClassDataContext();
        dc.ObjectTrackingEnabled = false;

        var addList = from addr in dc.Addresses
                      from eaddr in dc.EmployeeAddresses
                      where eaddr.EmployeeID == _curEmpID && addr.AddressID == eaddr.AddressID && addr.StateProvince.CountryRegionCode == "US"
                      select new
                      {
                          AddValue = addr.AddressID,
                          AddText = addr.AddressID,
                      };
        if (addList != null)
        {
            ddlAddList.DataSource = addList;
            ddlAddList.DataValueField = "AddValue";
            ddlAddList.DataTextField = "AddText";
            ddlAddList.DataBind();
        }

        ddlAddList.Items.Add(new ListItem("<Add Address>", "-1"));
    }

I created this code example using adventure works to do a little practice with Linq and it is very similar to the previous answer. Using linq still shouldn't matter for the answer the last dddlAddList.Items.Add is what you need. "Add Address" = first selected option and -1 = the value.

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