将 xml 节点的子节点绑定到 ASP.Net DropDownList

发布于 2024-10-04 20:10:38 字数 3106 浏览 1 评论 0原文

我试图将 XML 节点的内容绑定到下拉列表,但没有成功。

最初,整个 XML 文档绑定到一个转发器 - 这工作得很好,但是现在我需要根据 "" 节点中的子节点显示一个下拉列表,但我在根节点获取数据级别无效。 dropDownList 上的 DataBind() 方法的第 1 行、位置 1 错误消息。

谁能告诉我我做错了什么吗?

我使用的代码片段是:

    IXPathNavigable x = (IXPathNavigable)e.Item.DataItem;
    XPathNavigator questionNode = x.CreateNavigator();
   string question = questionNode.SelectSingleNode("questionText").ToString();

   //string title = xePage.SelectSingleNode("q").InnerText;

    Literal questionText = (Literal)e.Item.FindControl("litQuestionText");
    questionText.Text = question;
    Panel iconDiv = (Panel)e.Item.FindControl("divIcon");
    iconDiv.CssClass = string.Format("icon {0}", questionNode.SelectSingleNode("iconType"));
    Panel sliderPanel = (Panel)e.Item.FindControl("pnlSlider");
    DropDownList answerDropDown = (DropDownList)e.Item.FindControl("ddlAnswer");
    TextBox answerText = (TextBox)e.Item.FindControl("txtAnswer");

    switch (questionNode.SelectSingleNode("answerType").ToString())
    {
        case "d": 
            sliderPanel.Visible = false;
            answerText.Visible = false;
            answerDropDown.Visible = true;

            XmlDataSource answersList = new XmlDataSource();
            answersList.Data = questionNode.Select("answers").ToString();
            Response.Write(answersList.ToString());
            //XPathNodeIterator answers = questionNode.Select("answers");
            //answers.AsQueryable();
            answersList.ID = questionNode.SelectSingleNode("questionId").ToString();
            answerDropDown.DataSource = answersList;
            answerDropDown.DataTextField = "@display";
            answerDropDown.DataValueField = "@value";
            answerDropDown.DataBind();
            break;

        case "s":
            sliderPanel.Visible = true;
            answerText.Visible = false;
            answerDropDown.Visible = false;
            break;
        case "t":
            sliderPanel.Visible = false;
            answerText.Visible = true;
            answerDropDown.Visible = false;
            break;

我使用的 XML 是:

<questions>
  <question>
    <questionId>1</questionId>
    <questionText>Question here?</questionText>
    <iconType>a</iconType>
    <answerType>d</answerType>
    <answers>
      <answer value="-3" display="Extremely badly"/>
      <answer value="-2" display="Very badly"/>
      <answer value="-1" display="Quite badly"/>
      <answer value="0" display="Neither well nor badly"/>
      <answer value="1" display="Quite well"/>
      <answer value="2" display="Very well"/>
      <answer value="3" display="Extremely well"/>
    </answers>
  </question>
  <question>
    <questionId>1</questionId>
    <questionText>Question again here?</questionText>
    <iconType>b</iconType>
    <answerType>s</answerType>
    <answers/>
  </question>
</questions>

I'm trying to bind the contents of a node of XML to a Drop Down List without much success.

Initially, the whole XML document is bound to a repeater - this works perfectly, but now I need to display a drop down list based on the children in the "" node, but I get Data at the root level is invalid. Line 1, position 1 error message on the DataBind() method on the dropDownList.

Can anyone tell me what I'm doing wrong please??

The code snippet I'm using is:

    IXPathNavigable x = (IXPathNavigable)e.Item.DataItem;
    XPathNavigator questionNode = x.CreateNavigator();
   string question = questionNode.SelectSingleNode("questionText").ToString();

   //string title = xePage.SelectSingleNode("q").InnerText;

    Literal questionText = (Literal)e.Item.FindControl("litQuestionText");
    questionText.Text = question;
    Panel iconDiv = (Panel)e.Item.FindControl("divIcon");
    iconDiv.CssClass = string.Format("icon {0}", questionNode.SelectSingleNode("iconType"));
    Panel sliderPanel = (Panel)e.Item.FindControl("pnlSlider");
    DropDownList answerDropDown = (DropDownList)e.Item.FindControl("ddlAnswer");
    TextBox answerText = (TextBox)e.Item.FindControl("txtAnswer");

    switch (questionNode.SelectSingleNode("answerType").ToString())
    {
        case "d": 
            sliderPanel.Visible = false;
            answerText.Visible = false;
            answerDropDown.Visible = true;

            XmlDataSource answersList = new XmlDataSource();
            answersList.Data = questionNode.Select("answers").ToString();
            Response.Write(answersList.ToString());
            //XPathNodeIterator answers = questionNode.Select("answers");
            //answers.AsQueryable();
            answersList.ID = questionNode.SelectSingleNode("questionId").ToString();
            answerDropDown.DataSource = answersList;
            answerDropDown.DataTextField = "@display";
            answerDropDown.DataValueField = "@value";
            answerDropDown.DataBind();
            break;

        case "s":
            sliderPanel.Visible = true;
            answerText.Visible = false;
            answerDropDown.Visible = false;
            break;
        case "t":
            sliderPanel.Visible = false;
            answerText.Visible = true;
            answerDropDown.Visible = false;
            break;

and the XML I'm using is thus:

<questions>
  <question>
    <questionId>1</questionId>
    <questionText>Question here?</questionText>
    <iconType>a</iconType>
    <answerType>d</answerType>
    <answers>
      <answer value="-3" display="Extremely badly"/>
      <answer value="-2" display="Very badly"/>
      <answer value="-1" display="Quite badly"/>
      <answer value="0" display="Neither well nor badly"/>
      <answer value="1" display="Quite well"/>
      <answer value="2" display="Very well"/>
      <answer value="3" display="Extremely well"/>
    </answers>
  </question>
  <question>
    <questionId>1</questionId>
    <questionText>Question again here?</questionText>
    <iconType>b</iconType>
    <answerType>s</answerType>
    <answers/>
  </question>
</questions>

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

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

发布评论

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

评论(1

你在看孤独的风景 2024-10-11 20:10:38

显然我错过了 xml 文档顶部的 xml 声明。聪明的。

然而,实现此功能的最佳方法是为问题和答案创建调查问卷对象和类,然后通过反序列化将 XML 转换为类。然后,您可以将调查问卷的问题绑定到下拉列表,在您必须执行创建对象的初始工作之后,它基本上可以更好、更轻松地工作。

Apparently I'd missed out the xml declaration at the top of the xml document. Clever.

However, the best way to get this working is to create a Questionnaire object and classes for the Question and Answer and then convert the XML to the classes by deserializing it. Then you can bind the Questionnaire's questions to the drop down list and it basically works a load better and easier after the initial work you have to do to create the objects.

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