开发者

Binding an xml node's children to ASP.Net DropDownList

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>
    &开发者_运维知识库lt;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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜