.Nettiers的事件:Page_Load,Search,Delete,Edit,翻页【原创】

此文章包含Page_Load,Search,Delete,Edit,翻页事件, 只用了.nettiers 的Data,Entites,Service层,没有用Web层

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    
<title>Home Page</title>
    
<link href="App_Themes/Default/Default.css" rel="stylesheet" type="text/css" />
</head>
<body>
    
<form id="form1" runat="server">
        
<div>
            
<br />
            
<strong>Look For CompanyName:</strong><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            
<asp:Button ID="btnSearch" runat="server" OnClick="btnSearch_Click" Text="Search" /><br />
            
<br />
            
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3" ForeColor="#333333" GridLines="None" DataKeyNames="CustomerId" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowDataBound="GridView1_RowDataBound" OnPageIndexChanging="GridView1_PageIndexChanging" Height="100%" Width="100%">
                
<Columns>
                
<asp:BoundField DataField="CustomerId" HeaderText="Customer Id" SortExpression="CustomerID" ReadOnly=True />
                
<asp:BoundField DataField="CompanyName" HeaderText="Company Name" SortExpression="CompanyName"  />
                
<asp:BoundField DataField="ContactName" HeaderText="Contact Name" SortExpression="ContactName"  />
                
<asp:BoundField DataField="ContactTitle" HeaderText="Contact Title"  />
                
<asp:BoundField DataField="Address" HeaderText="Address" Visible="False"  />
                
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City"  />
                
<asp:BoundField DataField="Region" HeaderText="Region" SortExpression="Region"  />
                
<asp:BoundField DataField="PostalCode" HeaderText="Postal Code" SortExpression="PostalCode"  />
                
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country"  />
                
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" Visible="False"  />
                
<asp:BoundField DataField="Fax" HeaderText="Fax"   />
                
<asp:BoundField DataField="CustomerPs" HeaderText="CustomerPs"   />
                    
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            
</Columns>
                
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"  />
                
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                
<EditRowStyle BackColor="#999999" />
                
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                
<PagerSettings Mode="NumericFirstLast" />
            
</asp:GridView>
            
<br />
            
<asp:Button ID="btnCustomers" runat="server" OnClientClick="javascript:location.href='EditDefault.aspx?CustomerId='; return false;" Text="Add New" />
            
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
            
<asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label></div>
    
</form>

</body>
</html>

 

 

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Northwind.Services;
using Northwind.Data;
using Northwind.Entities;

    
public partial class _Default : System.Web.UI.Page 
    {
        
/// <summary>
        
/// Handles the Load event of the Page class.
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        CustomersService customersService = new CustomersService();
        
protected void Page_Load(object sender, EventArgs e)
        {
        
/*页面加载GridView*/
            
if (!IsPostBack)
            {
                
this.GridView1.DataSource = customersService.GetAll();
                
this.GridView1.DataBind();
                
this.Label1.Text = "";
            }
           
// Response.Write(ValidateCustomers("BOTTM", "Bottom-Dollar Markets").CustomerId);

        }
       
        
/*查询Gridview某条记录*/
        
protected void btnSearch_Click(object sender, EventArgs e)
        {
            
//查询方法一:
            
//ParameterizedSqlFilterBuilder<CustomersColumn> sFB = new ParameterizedSqlFilterBuilder<CustomersColumn>();
            
//sFB.Junction = SqlUtil.AND;
            
//sFB.AppendGreaterThan("CompanyName", this.TextBox1.Text);
            
//sFB.AppendLessThan("CompanyName", this.TextBox1.Text);
            ////sFB.AppendRange(OpinionsColumn.OpinionDate, startDate.ToString(), endDate.ToString());
            //TList<Customers> customers = DataRepository.CustomersProvider.Find(sFB.GetParameters());

            
//查询方法五:
            SqlFilterBuilder<CustomersColumn> sqlfilter = new SqlFilterBuilder<CustomersColumn>();
            
//sqlfilter.Append(CustomersColumn.CompanyName, this.TextBox1.Text);
            sqlfilter.AppendContains(CustomersColumn.CompanyName, this.TextBox1.Text);
            
int cnt = 0;
            TList
<Customers> customersList = DataRepository.CustomersProvider.GetPaged(sqlfilter.ToString(), null020out cnt);
            
this.GridView1.DataSource = customersList;
            
this.GridView1.DataBind();

            
//查询方法二:
            
//this.GridView1.DataSource = customersService.GetByCompanyName(this.TextBox1.Text);     //精确查询

            
//查询方法三:
            
//this.GridView1.DataSource = customersService.Find(string.Format("[CompanyName] LIKE '%{0}%'", this.TextBox1.Text));
            
//this.GridView1.DataBind();
        }

        
//查询方法四:
        public Customers ValidateCustomers(string CustomerId, string CompanyName)
        {
            SqlFilterBuilder
<CustomersColumn> filter = new SqlFilterBuilder<CustomersColumn>();
            filter.Junction 
= SqlUtil.AND;
            filter.AppendEquals(CustomersColumn.CustomerId, CustomerId);
            filter.AppendEquals(CustomersColumn.CompanyName, CompanyName);
            
int count = 0;
            TList
<Customers> customers = DataRepository.CustomersProvider.GetPaged(filter.ToString(), null020out count);
            
if (customers.Count > 0)
                
return customers[0];
            
else
                
return null;
        }


        
/*删除Gridview某条记录*/
        
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            
string ID = (string)GridView1.DataKeys[e.RowIndex].Value;
            
bool isok = customersService.Delete(ID);   
            
if (isok)
            {
                
this.Label1.Text = "删除数据成功!";
                
this.GridView1.DataSource = customersService.GetAll();
                
this.GridView1.DataBind();
            }
            
else
            {
                
this.Label1.Text = "删除数据失败!";
            }
        }

        
/*点击'编辑',页面根据"ID值"转到编辑页面*/
        
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            
string urlParams = string.Format("CustomerId={0}", (string)GridView1.DataKeys[e.NewEditIndex].Value);
            Response.Redirect(
"EditDefault.aspx?" + urlParams, true);
        }

        
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
/////找到删除按钮
            //LinkButton deleteBtn = (LinkButton)e.Row.FindControl("Delete");
            
//if (deleteBtn != null)
            
//{   ///添加删除确认对话框
            
//    deleteBtn.Attributes.Add("onclick", "return confirm('你确定要删除所选择的数据项吗?');");
            
//}
        }

        
/*Gridview翻页*/
        
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            
this.GridView1.PageIndex = e.NewPageIndex;
            
this.GridView1.DataSource = customersService.GetAll();
            
this.GridView1.DataBind();
        }
       
}
posted @ 2008-08-27 12:08  KiNg.JiOnG  阅读(835)  评论(0编辑  收藏  举报
查看博客访问人数(点击):