1. 准備條件
a) 作來數據存儲的xml檔案(這里方便操作就不使用DB而改用xml了)
<?xml version="1.0" encoding="utf-8" ?>
<Members>
<Member>
<UID>administrator</UID>
<PWD>111111</PWD>
<Email>maxadministrator@maxense.com</Email>
</Member>
<Member>
<UID>limin</UID>
<PWD>222222</PWD>
<Email>maxlimin@maxense.com</Email>
</Member>
<Member>
<UID>henry</UID>
<PWD>333333</PWD>
<Email>maxhenry@maxense.com</Email>
</Member>
<Member>
<UID>bruce</UID>
<PWD>444444</PWD>
<Email>maxbruce@maxense.com</Email>
</Member>
<Member>
<UID>cindy</UID>
<PWD>555555</PWD>
<Email>maxcindy@maxense.com</Email>
</Member>
<Member>
<UID>target</UID>
<PWD>666666</PWD>
<Email>maxtarget@maxense.com</Email>
</Member>
</Members>
b) 定義這個XML文件對象化的類
public class EntityMember
{
private string _uid;
public string UID
{
get { return _uid; }
set { _uid = value; }
}
private string _pwd;
public string PWD
{
get { return _pwd; }
set { _pwd = value; }
}
private string _email;
public string Email
{
get { return _email; }
set { _email = value; }
}
public EntityMember() { }
public EntityMember(string uid, string pwd, string email)
{
_uid = uid;
_pwd = pwd;
_email = email;
}
public EntityMember() { }
}
2. 創建一個業務處理類
public class Member
{
string _path = HttpContext.Current.Server.MapPath("~/member.xml");
public List<EntityMember> AllItems()
{
List<EntityMember> result = new List<EntityMember>();
XmlDocument doc = new XmlDocument();
doc.Load(_path);
XmlNodeList nodes = doc.SelectNodes("/Members/Member");
foreach (XmlNode node in nodes)
{
result.Add(new EntityMember(node.SelectSingleNode("./UID").InnerText, node.SelectSingleNode("./PWD").InnerText, node.SelectSingleNode("./Email").InnerText));
}
return result;
}
}
3. 正式使用ObjectDataSource對GridView等數據邦定控件進行邦定
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="AllItems" TypeName="Member"></asp:ObjectDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource1">
<Columns>
<asp:BoundField DataField="UID" HeaderText="UID" SortExpr
<asp:BoundField DataField="PWD" HeaderText="PWD" SortExpr
<asp:BoundField DataField="Email" HeaderText="Email" SortExpr
</Columns>
</asp:GridView>
說明:TypeName的值為業務處理類的類名稱
SelectMethod的值為業務處理類中需要執行查詢語句將查詢結果邦定到GridView控件的方法名稱
至於GridView都是自個進行邦定的,不用我們設置即可
以上僅僅只是通過ObjectDataSource將數據直接邦定到GridView,而在現實操作中往往需要對數據進行篩選後再邦定到GridView中, 以下的講解將會實現這個效果
4. 在業務處理類中添加以下方法
public List<EntityMember> SelectItems(string uid, string email)
{
List<EntityMember> result = new List<EntityMember>();
XmlDocument doc = new XmlDocument();
doc.Load(_path);
string xpath = @"/Members/Member";
if (!String.IsNullOrEmpty(uid) && !String.IsNullOrEmpty(email))
{
xpath += "[contains(UID,\"" + uid + "\") or contains(Email,\"" + email + "\")]";
}
else if (!String.IsNullOrEmpty(uid))
{
xpath += "[contains(UID,\"" + uid + "\")]";
}
else if (!String.IsNullOrEmpty(email))
{
xpath += "[contains(Email,\"" + email + "\")]";
}
XmlNodeList nodes = doc.SelectNodes(xpath);
foreach (XmlNode node in nodes)
{
result.Add(new EntityMember(node.SelectSingleNode("./UID").InnerText, node.SelectSingleNode("./PWD").InnerText, node.SelectSingleNode("./Email").InnerText));
}
return result;
}
5. 前端aspx頁面的內容為
<asp:Label ID="Label1" runat="server" Text="帳號:"></asp:Label>
<asp:TextBox ID="txtUID" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="電子郵件"></asp:Label>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="查詢" on
<hr />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource1">
<Columns>
<asp:BoundField DataField="UID" HeaderText="UID" SortExpr
<asp:BoundField DataField="PWD" HeaderText="PWD" SortExpr
<asp:BoundField DataField="Email" HeaderText="Email" SortExpr
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="SelectItems" TypeName="Member"></asp:ObjectDataSource>
說明:從以上看起來與上面的例子並沒有什麼差別,但是我們可以看到這裏邦定的方法SelectItems具有兩個參數uid與email.如果我們這時直接執行的話肯定會提示錯誤了。我們還需在aspx.cs文件中為這個ObjectDataSource添加查詢的參數
6. Aspx.cs文件的內容為
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ObjectDataSource1.SelectParameters.Add("uid", "");
ObjectDataSource1.SelectParameters.Add("email", "");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
ObjectDataSource1.SelectParameters.Clear();
ObjectDataSource1.SelectParameters.Add("uid", this.txtUID.Text.Trim());
ObjectDataSource1.SelectParameters.Add("email", this.txtEmail.Text.Trim());
}
說明:如果我們指定ObjectDataSource對象的SelectMethod方法帶有參數的話,那麼我們一定需要通過SelectParameters集合為SelectMethod方法指定參數,其集合中的成員個數要與SelectMethod方法的參數一樣,如: SelectParameters.Add("uid", "")…這裏的uid與email與SelectItems方法中的參數名稱要一樣了。還有一點需要注意的就是如果我們執行如下操作
ObjectDataSource1.SelectParameters.Add("uid", "")
ObjectDataSource1.SelectParameters.Add("uid", "123")
那麼ObjectDataSource就會去查找SelectMethod指定方法上帶有uid與uid1的這個方法了..所以我上面在button1_click中重新邦定之前選清空了原來的參數了…
在上面AllItems()的定义时,读者可以看到该方法返回的类型是List<T>,由于ObjectDataSource将来需要作为绑定控件的数据来源,所以它的返回类型必须如下的返回类型之一:
Ienumerable、DataTable、DataView、DataSet或者Object。