我收到错误:
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'image'.
我使用xml和asp.net。
<%@ Import Namespace="System.Xml"%>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.Xml.XPath" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
binddata();
binddata2();
}
if (Session["userlevel"] == null)
Response.Redirect("AdminLogin.aspx");
if (Session["error_msg"] != null)
{
ErrorMessage.Text = Session["error_msg"].ToString();
Session["error_msg"] = null;
ErrorMessage.Visible = true;
}
}
void binddata()
{
DataSet ds = new DataSet();
try
{
ds.ReadXml(Server.MapPath("../App_Data/DefaultImage.xml"));
gv.DataSource = ds;
gv.DataBind();
}
catch (Exception e)
{
ErrorMessage.Text = e.Message;
ds = null;
}
}
protected void Editdata(object s, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
binddata();
}
protected void Deletedata(object s, GridViewDeleteEventArgs e)
{
binddata();
DataSet ds = gv.DataSource as DataSet;
ds.Tables[0].Rows[gv.Rows[e.RowIndex].DataItemIndex].Delete();
ds.WriteXml(Server.MapPath("../App_Data/DefaultImage.xml"));
binddata();
Session["error_msg"] = "The welcome image was deleted successfully";
Response.Redirect("AdminDefault.aspx");
}
protected void Canceldata(object s, GridViewCancelEditEventArgs e)
{
gv.EditIndex = -1;
binddata();
}
protected void Updatedata(object s, GridViewUpdateEventArgs e)
{
int i = e.RowIndex;
string id = (gv.Rows[e.RowIndex].FindControl("txtimage") as TextBox).Text;
string name = (gv.Rows[e.RowIndex].FindControl("txttext") as TextBox).Text;
gv.EditIndex = -1;
binddata();
DataSet ds = (DataSet)gv.DataSource;
ds.Tables[0].Rows[i]["image"] = id;
ds.Tables[0].Rows[i]["text"] = name;
ds.WriteXml(Server.MapPath("../App_Data/DefaultImage.xml"));
binddata();
Session["error_msg"] = "The welcome image was updated successfully";
Response.Redirect("AdminDefault.aspx");
}
protected void pageddata(object s, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
binddata();
}
protected void insert(object sender, EventArgs e)
{
binddata();
DataSet ds = gv.DataSource as DataSet;
DataRow dr = ds.Tables[0].NewRow();
dr[0] = Image1.Text;
dr[1] = Text1.Text;
ds.Tables[0].Rows.Add(dr);
ds.AcceptChanges();
ds.WriteXml(Server.MapPath("../App_Data/DefaultImage.xml"));
binddata();
Image1.Text = string.Empty;
Text1.Text = string.Empty;
Session["error_msg"] = "The welcome image was added successfully";
Response.Redirect("AdminDefault.aspx");
}
//---------------------------------------------------------
void binddata2()
{
DataSet ds2 = new DataSet();
try
{
ds2.ReadXml(Server.MapPath("../App_Data/DefaultBefore.xml"));
gv2.DataSource = ds2;
gv2.DataBind();
}
catch (Exception e)
{
ErrorMessage.Text = e.Message;
ds2 = null;
}
}
protected void Editdata2(object s, GridViewEditEventArgs e)
{
gv2.EditIndex = e.NewEditIndex;
binddata2();
}
protected void Deletedata2(object s, GridViewDeleteEventArgs e)
{
binddata2();
DataSet ds2 = gv2.DataSource as DataSet;
ds2.Tables[0].Rows[gv2.Rows[e.RowIndex].DataItemIndex].Delete();
ds2.WriteXml(Server.MapPath("../App_Data/DefaultBefore.xml"));
binddata2();
Session["error_msg"] = "The before-after image was deleted successfully";
Response.Redirect("AdminDefault.aspx");
}
protected void Canceldata2(object s, GridViewCancelEditEventArgs e)
{
gv2.EditIndex = -1;
binddata2();
}
protected void Updatedata2(object s, GridViewUpdateEventArgs e)
{
int i = e.RowIndex;
string id = (gv2.Rows[e.RowIndex].FindControl("txtimagebefore") as TextBox).Text;
gv2.EditIndex = -1;
binddata2();
DataSet ds2 = (DataSet)gv2.DataSource;
ds2.Tables[0].Rows[i]["image"] = id;
ds2.WriteXml(Server.MapPath("../App_Data/DefaultBefore.xml"));
binddata2();
Session["error_msg"] = "The before-after image was updated successfully";
Response.Redirect("AdminDefault.aspx");
}
protected void pageddata2(object s, GridViewPageEventArgs e)
{
gv2.PageIndex = e.NewPageIndex;
binddata2();
}
protected void insert2(object sender, EventArgs e)
{
binddata2();
DataSet ds2 = gv2.DataSource as DataSet;
DataRow dr2 = ds2.Tables[0].NewRow();
dr2[0] = ImageBefore.Text;
ds2.Tables[0].Rows.Add(dr2);
ds2.AcceptChanges();
ds2.WriteXml(Server.MapPath("../App_Data/DefaultBefore.xml"));
binddata2();
ImageBefore.Text = string.Empty;
Session["error_msg"] = "The before-after image was added successfully";
Response.Redirect("AdminDefault.aspx");
}
</script>
<div id="content">
<div class="about">
<b><i><asp:Label ID="ErrorMessage" runat="server" ForeColor="Red" /></i></b>
<h3>Add a welcome image and text</h3> <br />
<asp:Label ID="lblImage" runat="server" Text="Image Path" />
<asp:TextBox ID="Image1" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3"
ControlToValidate="Image1" Font-Italic="true"
Display="Dynamic" runat="server" ErrorMessage="(Required)"
ForeColor="Red" ValidationGroup="AddWelcome" />
<br />
<asp:Label ID="Label1" runat="server" Text="Image Text" />
<asp:TextBox ID="Text1" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator4"
ControlToValidate="Text1" Font-Italic="true"
Display="Dynamic" runat="server" ErrorMessage="(Required)"
ForeColor="Red" ValidationGroup="AddWelcome" />
<asp:Button ID="btninsert" runat="server" Text="Insert" onclick="insert" ValidationGroup="AddWelcome" />
<br /><br />
<asp:GridView ID="gv" Width="80%" runat="server" OnRowEditing="Editdata" AllowSorting="true"
OnPageIndexChanging="pageddata" OnRowDeleting="Deletedata" BorderColor="Black"
OnRowUpdating="Updatedata" OnRowCancelingEdit="Canceldata" AllowPaging="True"
AutoGenerateColumns="False" PageSize="3" ForeColor="Black" GridLines="Both" >
<RowStyle BackColor="white" />
<Columns>
<asp:TemplateField HeaderText="Image name">
<ItemTemplate>
<asp:Label ID="lblimage" runat="server" Text='<%#Eval("image") %>' />
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID="txtimage" runat ="server" Text='<%#Eval("image") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ControlToValidate="txtimage" Font-Italic="true"
Display="Dynamic" runat="server" ErrorMessage="(Required)"
ForeColor="Red" ValidationGroup="Welcome" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image text" >
<ItemTemplate>
<asp:Label ID="lbltext" runat="server" Text='<%#Eval("text") %>' />
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID="txttext" runat ="server" Text='<%#Eval("text") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"
ControlToValidate="txttext" Font-Italic="true"
Display="Dynamic" runat="server" ErrorMessage="(Required)"
ForeColor="Red" ValidationGroup="Welcome" />
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ValidationGroup="Welcome" />
<asp:CommandField ShowDeleteButton="True" ValidationGroup="Welcome" />
</Columns>
<PagerStyle ForeColor="Maroon" HorizontalAlign="Center" Font-Bold="true"/>
<SelectedRowStyle BackColor="Crimson" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="White" Font-Bold="true" ForeColor="Maroon" />
<HeaderStyle ForeColor="White" BackColor="Maroon" Font-Bold="true" />
<AlternatingRowStyle BackColor="AntiqueWhite" />
</asp:GridView>
<br />
<br /><br /><br />
xml file
DefaultBefore.xml
<?xml version="1.0" standalone="yes"?>
<images xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<image>
App_Themes/theme/html/images/beforeafter1.jpg
</image>
<image>
App_Themes/theme/html/images/beforeafter3.jpg
</image>
<image>
App_Themes/theme/html/images/beforeafter5.jpg
</image>
</images>
DefaultImage.xml
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<images>
<image>
App_Themes/theme/html/images/title1.jpg
</image>
<text>
A dog is a man's best friend... Dog Club is a dogs best friend.
</text>
</images>
</NewDataSet>
答案 0 :(得分:0)
尝试重构XML:
<images>
<image>
<path>
App_Themes/theme/html/images/beforeafter1.jpg
</path>
</image>
<image>
<path>
App_Themes/theme/html/images/beforeafter3.jpg
</path>
</image>
<image>
<path>
App_Themes/theme/html/images/beforeafter5.jpg
</path>
</image>
</images>
然后修改数据绑定表达式:
<%# Eval("path") %>