0

XmlDataSource 绑定到 XmlDocument

我想做的事:

从用户读取 XML 文件并在 GridView 和 DetailsView 中显示。用户可以在此处进行修改或添加新条目。然后他可以将修改后的 XML 文件下载到他的硬盘上。

示例 XML:

<?xml version="1.0" encoding="utf-8"?>
<Mapping Version="2">
  <MappingRelation Active="True" DirectionVar1ToVar2="True" Offset="0" Factor="1" Assignment="OnChange">
    <ValueObject1 ItemType="symSystemVariable">
      <BusType>-1</BusType>
      <DatabaseName>HumiditySensor::p_Ambient</DatabaseName>
      <Description />
      <EnvVarName />
      <FullName>HumiditySensor::p_Ambient</FullName>
      <ID>1</ID>
      <MessageName />
      <Name>HumiditySensor::p_Ambient</Name>
      <NeedsMessage>False</NeedsMessage>
      <NetworkName />
      <NodeName />
      <SignalName />
      <VariableType>2</VariableType>
    </ValueObject1>
    <ValueObject2 ItemType="symSignal">
      <BusType>1</BusType>
      <DatabaseName>CAN1</DatabaseName>
      <Description />
      <EnvVarName>ECAN 1</EnvVarName>
      <FullName>CAN1::HumiditySensor::Luftfeuchte_HDT2800::_Ambient_Pressure</FullName>
      <ID>0</ID>
      <MessageName>Luftfeuchte_HDT2800</MessageName>
      <Name>_Ambient_Pressure</Name>
      <NeedsMessage>False</NeedsMessage>
      <NetworkName>ECAN 1</NetworkName>
      <NodeName>HumiditySensor</NodeName>
      <SignalName>_Ambient_Pressure</SignalName>
      <VariableType>0</VariableType>
    </ValueObject2>
  </MappingRelation>
  <MappingRelation Active="True" DirectionVar1ToVar2="True" Offset="0" Factor="1" Assignment="OnChange">
    <ValueObject1 ItemType="symSystemVariable">
      <BusType>-1</BusType>
      <DatabaseName>HumiditySensor::spec_Humidity</DatabaseName>
      <Description />
      <EnvVarName />
      <FullName>HumiditySensor::spec_Humidity</FullName>
      <ID>1</ID>
      <MessageName />
      <Name>HumiditySensor::spec_Humidity</Name>
      <NeedsMessage>False</NeedsMessage>
      <NetworkName />
      <NodeName />
      <SignalName />
      <VariableType>2</VariableType>
    </ValueObject1>
    <ValueObject2 ItemType="symSignal">
      <BusType>1</BusType>
      <DatabaseName>CAN1</DatabaseName>
      <Description />
      <EnvVarName>ECAN 1</EnvVarName>
      <FullName>CAN1::HumiditySensor::Luftfeuchte_HDT2800::_Specific_Humidity</FullName>
      <ID>0</ID>
      <MessageName>Luftfeuchte_HDT2800</MessageName>
      <Name>_Specific_Humidity</Name>
      <NeedsMessage>False</NeedsMessage>
      <NetworkName>ECAN 1</NetworkName>
      <NodeName>HumiditySensor</NodeName>
      <SignalName>_Specific_Humidity</SignalName>
      <VariableType>0</VariableType>
    </ValueObject2>
  </MappingRelation>
</Mapping>

到目前为止我在做什么:

我将提供的 XML 文档读入 XmlDocument。此 Xmldocument 绑定到 XmlDataSource (id="id_DataSourceMP"),而 XmlDataSource (id="id_DataSourceMP") 又绑定到 GridView (id="id_GridViewMP") 和 DetailsView (id="id_DetailsViewMP") 控件。在这个函数中,我可以从 XmlDataSource 读回数据,并且可以更改例如 DetailsView 的 PageIndex。

但在其他函数(mp_rowSelected、id_MapButton_Click)中,这会产生错误“对象引用未设置为对象的实例”。

这是后面的代码:

using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Xml;
using System.Xml.XPath;



public partial class Default : System.Web.UI.Page
{
    //-----------------------------------------------------------------------
    //PageCallBacks
    //-----------------------------------------------------------------------

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            //DataTable dt = new DataTable();
            //Session["DataTable"] = dt;

            //XmlDocument xmlMap = new XmlDocument();
            //Session["Mapping"] = xmlMap;

            //TreeNode sv_selectedNode = new TreeNode();
            //TreeNode pv_selectedNode = new TreeNode();
        }

    }

    //--------------------------------------------------------------------------------------
    //Callback Functions
    //--------------------------------------------------------------------------------------

    protected void sv_loadXml(Object sender, EventArgs e)
    {
    }

    protected void pv_loadXml(Object sender, EventArgs e)
    { 
    }

    protected void tree_nodechange(Object sender, EventArgs e)
    {
    }

    protected void mp_loadXml(Object sender, EventArgs e)
    {
        //String myFile = Server.MapPath("~/data/HumiditySensor_Map.xml");
        if (id_FileUploaderMP.HasFile)
        {
            Stream myStream = id_FileUploaderMP.FileContent;
            StreamReader sr = new StreamReader(myStream);

            XmlDocument myDoc = new XmlDocument();
            myDoc.Load(sr);

            if (myDoc.DocumentElement.Name != "Mapping")
            {
                id_writeOut.InnerText = "Laden fehlgeschlagen!";
                return;
            }

            id_DataSourceMP.Data = myDoc.InnerXml;
            id_DataSourceMP.DataBind();
            // Test
            id_writeOut.InnerText = id_DataSourceMP.Data;

            id_GridViewMP.DataSourceID = "id_DataSourceMP";
            id_GridViewMP.DataBind();

            id_DetailsViewMP.DataSourceID = "id_DataSourceMP";
            id_DetailsViewMP.DataBind();
            // Test
            id_DetailsViewMP.PageIndex = 4;
        }

    }

    protected void mp_rowSelected(Object sender, EventArgs e)
    {
        //id_DetailsViewMP.PageIndex = 4;

        id_writeOut.InnerText = id_DataSourceMP.Data;
    }

    protected void id_MapButton_Click(object sender, EventArgs e)
    {
        id_writeOut.InnerText = id_DataSourceMP.Data;
        id_DetailsViewMP.PageIndex = 1;
    }


    //--------------------------------------------------------------------------------------
    //Override Functions
    //--------------------------------------------------------------------------------------

    protected override void Render(HtmlTextWriter writer)
    {
        foreach (GridViewRow row in id_GridViewMP.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {

                row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
                row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
                row.ToolTip = "Click to select row";
                row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.id_GridViewMP, "Select$" + row.RowIndex, true);
            }
        }

        base.Render(writer);
    }
}

我很确定我有一些与 DataBinding 相关的思考错误。有人可以告诉我我的失败在哪里或提出替代解决方案吗?

在此先感谢您的帮助,请保持温和,这是我的第一篇文章。

4

1 回答 1

0

尤里卡!我有解决办法。

我不得不使用属性“DataSource”而不是“DataSourceIP”:

id_DataSourceMP.Data = myDoc.InnerXml;
id_DataSourceMP.DataBind();
id_GridViewMP.DataSource = id_DataSourceMP;
id_GridViewMP.DataBind();

id_DetailsViewMP.DataSource = id_DataSourceMP;
id_DetailsViewMP.DataBind();

随后的错误是DetailsView“忘记”了它的数据。我不知何故认为数据绑定意味着控件或数据源永远绑定,就像婚姻一样。也许在某处有戒指...

但是,您必须在每次查看它们时绑定这些东西,即,当 DetailsView 应该显示另一个页面时。像这样的功能:

private void bindDataXml()
{
    XmlDocument doc_MP = (XmlDocument)Session["Mapping"];

    id_DataSourceMP.Data = doc_MP.InnerXml;
    id_DataSourceMP.DataBind();

    id_Grid_ViewMP.DataSource = id_DataSourceMP;
    id_Grid_ViewMP.DataBind();

    id_DetailsViewMP.DataSource = id_DataSourceMP;
    id_DetailsViewMP.DataBind();
}

也许其他人会发现这很有帮助。

于 2017-05-12T10:39:44.257 回答