且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从填充的XmlDataSource的DropDownList

更新时间:2023-11-15 15:56:52

我无法从我的头顶还记得,但我认为在这的XmlDataSource prevents你绑定到XML值中的错误节点。它与唯一的属性。如果我错了这个请大家指正。有你需要对你的XML文件略作修改:

I can't recall it from the top of my head but I think there was a bug in XmlDataSource that prevents you to bind to values of xml nodes. It works with attributes only. Please correct me if I am wrong with this. There's a slight modification you need to make to your XML file:

<%@ Page Language="C#" %>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string xml =
@"<?xml version=""1.0"" encoding=""utf-8"" ?>
<Databases>
  <Database name=""foo"" />
  <Database name=""bar"" />
  <Database name=""baz"" />
</Databases>";
            databasesSource.Data = xml;
            databasesSource.DataBind();
        }
    }
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="databases" runat="server" DataSourceID="databasesSource" DataValueField="name" DataTextField="name" />
        <asp:XmlDataSource ID="databasesSource" runat="server" XPath="/Databases/Database" />
    </div>
    </form>
</body>
</html>

请注意,我加入的名称的属性,而不是直接使用节点的价值。

Note that I added the name attribute instead of using the value of the node directly.

如果您不能修改原始的XML文件的结构,你可以在其上使用应用XSLT转换的TransformFile财产在这个帖子描述。

If you can't modify the structure of your original XML file you can apply an XSLT transformation on it using the TransformFile property as described in this post.