【问题标题】:Combines two fields in asp.net dropdownlist结合 asp.net 下拉列表中的两个字段
【发布时间】:2011-08-16 15:32:00
【问题描述】:

我需要创建一个下拉列表,将 xml 文件中的两个字段组合在一起...

XML 文件:

<?xml version="1.0" encoding="utf-8" ?>
<exchangeRates>
<rate country="aud" >0.97</rate>
<rate country="usd" >1.01</rate>
</exchangeRates>

我需要下拉列表显示国家和汇率

aud    0.97
usd    1.01

类似

SELECT country + rate AS NewColumn FROM XML above

And then use NewColumn for the DataValueFIeld:

<asp:DropDownList ID="DropDownList1" runat="server" 
DataSourceID="SqlDBSrc" DataTextField="NAME" DataValueField="NewColumn"
>

<asp:XmlDataSource ID="ListItems" runat="server" DataFile="~/ExchangeRates.xml">
</asp:XmlDataSource>

如何在 asp.net 中正确执行此操作?

【问题讨论】:

    标签: asp.net html xml data-binding drop-down-menu


    【解决方案1】:

    您或许可以使用 LINQ to XML。 类似的东西(未测试)

    dim selectList = (from x in xmldoc
                    select new SelectListItem { Text = x.Attribute("country") + ' '+ x.Value, Value = x.Id });
    

    【讨论】:

      【解决方案2】:

      您可以向控件添加预渲染事件并在那里修改控件

      <asp:DropDownList ID="DropDownList1" runat="server" onprerender="DropDownList1_PreRender"
      DataSourceID="SqlDBSrc" DataTextField="Country" DataValueField="Value"
      >
      

      注意:调整 DataText 和 DataValue 字段以填充未修改的数据。

      在您后面的代码中,这会修改:

      protected void DropDownList1_PreRender(object sender, EventArgs e)
      {
          foreach (ListItem item in DropDownList1.Items)
          {
              item.Text = item.Text + " " + item.Value;
          }
      }
      

      这是一种方法,但在绑定列表之前可能还有其他更好的方法。

      【讨论】:

        【解决方案3】:

        <asp:DropDownList ID="DropDownList9" runat="server" DataSourceID="SqlDataSource3" DataTextField="**CALL_NAMESURNAME**" DataValueField="CALL_NAME"> </asp:DropDownList> <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:McCarthyConnectionString %>" SelectCommand="SELECT [CALL_NAME], [SURNAME],**CALL_NAME + SURNAME AS [CALL_NAMESURNAME]** FROM [Sheet1$] where COY_NAME LIKE '%abc%'"> </asp:SqlDataSource>

        检查 ** ** 部分。

        【讨论】:

          猜你喜欢
          • 2020-11-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多