ASP.Net/XML/XML GridView — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:51, 26 мая 2010
asp:GridView column alignment
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Configuration"%>
<%@ Import Namespace="System.Data"%>
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
DataSet authorsDataSet;
string filePath = Server.MapPath("Authors.xml");
authorsDataSet = new DataSet();
//Read the contents of the XML file into the DataSet
authorsDataSet.ReadXml(filePath);
authorsGird.DataSource = authorsDataSet.Tables[0].DefaultView;
authorsGird.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Reading XML Data into a DataSet object </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView id="authorsGird" runat="server"
AutoGenerateColumns="False" CellPadding="4" HeaderStyle-BackColor="blue" HeaderStyle-ForeColor="White"
HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True">
<Columns>
<asp:BoundField HeaderText="Last Name" DataField="lastName" />
<asp:BoundField HeaderText="First Name"
DataField="firstName" ItemStyle-HorizontalAlign="Right" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Displaying XML Data in a GridView and a ListBox?
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Displaying XML Data in a GridView and a ListBox</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="lstTitles" Runat="server" DataSourceID="bookSource" DataValueField="ISBN"
DataTextField="Title"/>
</div>
<div>
<asp:GridView ID="bookView" Runat="server" DataSourceID="bookSource" AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="ISBN" DataField="ISBN" SortExpression="ISBN"></asp:BoundField>
<asp:BoundField HeaderText="Title" DataField="Title" SortExpression="Title"></asp:BoundField>
<asp:BoundField HeaderText="Price" DataField="Price" SortExpression="Price"></asp:BoundField>
</Columns>
</asp:GridView>
</div>
<div>
<asp:XmlDataSource ID="bookSource" Runat="server" DataFile="~/Data.xml"
XPath="Data/genre[@name ="Fiction"]/book">
</asp:XmlDataSource> </div>
</form>
</body>
</html>
File: ~/Data.xml
<Data>
<genre name="Fiction">
<book ISBN="1" Title="title 1" Price="19.99" Discount="1.999">
<chapter num="1" name="Introduction">
Abstract...
</chapter>
<chapter num="2" name="Body">
Abstract...
</chapter>
<chapter num="3" name="Conclusion">
Abstract...
</chapter>
</book>
</genre>
<genre name="NonFiction">
<book ISBN="2" Title="title 2" Price="27.95" Discount="2.795">
<chapter num="1" name="Introduction">
Abstract...
</chapter>
<chapter num="2" name="Body">
Abstract...
</chapter>
<chapter num="3" name="Conclusion">
Abstract...
</chapter>
</book>
</genre>
</Data>
Load XML data to GridView
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Grades" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Grades</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnViewGrades" runat="server" OnClick="btnViewGrades_Click" Text="View Grades" /><br />
<asp:GridView ID="GridView1" runat="server"/>
<asp:Label ID="lblResult" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Security.Principal;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Grades : System.Web.UI.Page
{
protected void btnViewGrades_Click(object sender, EventArgs e)
{
Response.Write(WindowsIdentity.GetCurrent().Name + "<br>");
WindowsImpersonationContext ctx = ((WindowsIdentity)User.Identity).Impersonate();
Response.Write(WindowsIdentity.GetCurrent().Name + "<br>");
try
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("Data.xml"));
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
catch (UnauthorizedAccessException)
{
lblResult.Text = "Not Authorized!";
}
ctx.Undo();
Response.Write(WindowsIdentity.GetCurrent().Name + "<br>");
}
}
File: Data.xml
<?xml version="1.0" encoding="utf-8" ?>
<students>
<student>
<name>John Smith</name>
<grade>B</grade>
</student>
<student>
<name>Jane Doe</name>
<grade>A</grade>
</student>
</students>