<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<html>
<body>
<form method=post runat="server">
<asp:Label runat="server" ID="MoreInfo" />
<br><br>
<asp:DataGrid runat="server" ID="Datagrid1"
DataKeyField="EmployeeID"
AutoGenerateColumns="False"
Width="100%"
OnSelectedIndexChanged="Datagrid1_SelectionChanged"
AllowPaging="true"
AllowCustomPaging="true"
PageSize="3"
PagerStyle-HorizontalAlign="Right"
PagerStyle-PageButtonCount="4"
PagerStyle-Mode="NumericPages"
PagerStyle-BackColor="Beige"
PagerStyle-ForeColor="Red"
OnPageIndexChanged="Datagrid1_PageChanged"
HeaderStyle-Font-Size="10"
HeaderStyle-Font-Bold="true"
HeaderStyle-ForeColor="Red"
HeaderStyle-BackColor="Yellow"
HeaderStyle-BorderColor="Red"
HeaderStyle-BorderWidth="5"
FooterStyle-BorderColor="Red"
FooterStyle-BorderWidth="5"
ItemStyle-BackColor="LightCyan"
ItemStyle-ForeColor="DarkBlue"
AlternatingItemStyle-BackColor="LightYellow"
AlternatingItemStyle-ForeColor="Maroon"
AlternatingItemStyle-Font-Italic="true"
SelectedItemStyle-ForeColor="Yellow"
SelectedItemStyle-BackColor="Red"
SelectedItemStyle-Font-Bold="true">
<Columns>
<asp:BoundColumn HeaderText="ID" ItemStyle-Width="30px" DataField="EmployeeID"/>
<asp:BoundColumn HeaderText="Title" ItemStyle-Width="50px" DataField="TitleOfCourtesy"/>
<asp:BoundColumn HeaderText="Last Name" ItemStyle-Width="150px" DataField="LastName"/>
<asp:BoundColumn HeaderText="First Name" DataField="FirstName"/>
<asp:ButtonColumn CommandName="Select" ItemStyle-Width="20px" Text="<img border=0 src=message.gif>" />
</Columns>
</asp:DataGrid>
</form>
</body>
</html><script runat="server" language="C#">
void Page_Load()
{
if (!Page.IsPostBack)
{
SetGridItemCount();
BindGrid();
}
}
void SetGridItemCount()
{
// create the command and the connection
string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
string sql = "SELECT COUNT(*) FROM Employees";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
// execute the command and use the return value for the grid's VirtualItemCount prop
Datagrid1.VirtualItemCount = (int)cmd.ExecuteScalar();
conn.Close();
}
void BindGrid()
{
// create the command and the connection
string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("sp_GetEmployeesByPage", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@PageNumber", SqlDbType.Int, 4));
cmd.Parameters["@PageNumber"].Value = Datagrid1.CurrentPageIndex + 1;
cmd.Parameters.Add(new SqlParameter("@PageSize", SqlDbType.Int, 4));
cmd.Parameters["@PageSize"].Value = Datagrid1.PageSize;
// open the connection and get the Reader
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
// bind the reader to the DataList
Datagrid1.DataSource = reader;
Datagrid1.DataBind();
// close the reader and the connection
reader.Close();
conn.Close();
}
protected void Datagrid1_SelectionChanged(object sender, EventArgs e)
{
// create the command and the connection
string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
int empID = (int)Datagrid1.DataKeys[Datagrid1.SelectedIndex];
string sql = "SELECT * FROM Employees WHERE EmployeeID = " + empID.ToString();
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
// open the connection and get the Reader
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
// show the results
StringBuilder str = new StringBuilder("");
reader.Read();
str.Append("<b>");
str.Append(reader["FirstName"].ToString());
str.Append(" ");
str.Append(reader["LastName"].ToString());
str.Append("<br>");
str.Append(reader["Title"].ToString());
str.Append("<br>");
str.Append(reader["Address"].ToString());
str.Append("<br>");
str.Append(reader["City"].ToString());
str.Append(", ");
str.Append(reader["Region"].ToString());
str.Append("</b><br>");
str.Append(reader["Notes"].ToString());
MoreInfo.Text = str.ToString();
// close the reader and the connection
reader.Close();
conn.Close();
// rebind the grid
BindGrid();
}
protected void Datagrid1_PageChanged(Object sender, DataGridPageChangedEventArgs e)
{
// deselect the currently selected row, if any
Datagrid1.SelectedIndex = -1;
MoreInfo.Text = "";
// change the current page and rebind
Datagrid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}</script>if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_GetEmployeesByPage]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_GetEmployeesByPage]
GOSET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GOCREATE PROCEDURE sp_GetEmployeesByPage
@PageNumber int,
@PageSize int
AS-- create a temporary table with the columns we are interested in
CREATE TABLE #TempEmployees
(
ID int IDENTITY PRIMARY KEY,
EmployeeID int,
LastName nvarchar(20),
FirstName nvarchar(10),
Title nvarchar(30),
TitleOfCourtesy nvarchar(25),
Address nvarchar(60),
City nvarchar(15),
Region nvarchar(15),
Country nvarchar(15),
Notes ntext
)-- fill the temp table with all the employees
INSERT INTO #TempEmployees
(
EmployeeID,
LastName,
FirstName,
Title,
TitleOfCourtesy,
Address,
City,
Region,
Country,
Notes
)
SELECT
EmployeeID,
LastName,
FirstName,
Title,
TitleOfCourtesy,
Address,
City,
Region,
Country,
Notes
FROM
Employees ORDER BY EmployeeID ASC-- declare two variables to calculate the range of records to extract for the specified page
DECLARE @FromID int
DECLARE @ToID int
-- calculate the first and last ID of the range of records we need
SET @FromID = ((@PageNumber - 1) * @PageSize) + 1
SET @ToID = @PageNumber * @PageSize-- select the page of records
SELECT * FROM #TempEmployees WHERE ID >= @FromID AND ID <= @ToID
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
<%@ Import Namespace="System.Data" %>
<html>
<body>
<form method=post runat="server">
<asp:Label runat="server" ID="MoreInfo" />
<br><br>
<asp:DataGrid runat="server" ID="Datagrid1"
DataKeyField="EmployeeID"
AutoGenerateColumns="False"
Width="100%"
OnSelectedIndexChanged="Datagrid1_SelectionChanged"
AllowPaging="true"
AllowCustomPaging="true"
PageSize="3"
PagerStyle-HorizontalAlign="Right"
PagerStyle-PageButtonCount="4"
PagerStyle-Mode="NumericPages"
PagerStyle-BackColor="Beige"
PagerStyle-ForeColor="Red"
OnPageIndexChanged="Datagrid1_PageChanged"
HeaderStyle-Font-Size="10"
HeaderStyle-Font-Bold="true"
HeaderStyle-ForeColor="Red"
HeaderStyle-BackColor="Yellow"
HeaderStyle-BorderColor="Red"
HeaderStyle-BorderWidth="5"
FooterStyle-BorderColor="Red"
FooterStyle-BorderWidth="5"
ItemStyle-BackColor="LightCyan"
ItemStyle-ForeColor="DarkBlue"
AlternatingItemStyle-BackColor="LightYellow"
AlternatingItemStyle-ForeColor="Maroon"
AlternatingItemStyle-Font-Italic="true"
SelectedItemStyle-ForeColor="Yellow"
SelectedItemStyle-BackColor="Red"
SelectedItemStyle-Font-Bold="true">
<Columns>
<asp:BoundColumn HeaderText="ID" ItemStyle-Width="30px" DataField="EmployeeID"/>
<asp:BoundColumn HeaderText="Title" ItemStyle-Width="50px" DataField="TitleOfCourtesy"/>
<asp:BoundColumn HeaderText="Last Name" ItemStyle-Width="150px" DataField="LastName"/>
<asp:BoundColumn HeaderText="First Name" DataField="FirstName"/>
<asp:ButtonColumn CommandName="Select" ItemStyle-Width="20px" Text="<img border=0 src=message.gif>" />
</Columns>
</asp:DataGrid>
</form>
</body>
</html><script runat="server" language="C#">
void Page_Load()
{
if (!Page.IsPostBack)
{
SetGridItemCount();
BindGrid();
}
}
void SetGridItemCount()
{
// create the command and the connection
string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
string sql = "SELECT COUNT(*) FROM Employees";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
// execute the command and use the return value for the grid's VirtualItemCount prop
Datagrid1.VirtualItemCount = (int)cmd.ExecuteScalar();
conn.Close();
}
void BindGrid()
{
// create the command and the connection
string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("sp_GetEmployeesByPage", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@PageNumber", SqlDbType.Int, 4));
cmd.Parameters["@PageNumber"].Value = Datagrid1.CurrentPageIndex + 1;
cmd.Parameters.Add(new SqlParameter("@PageSize", SqlDbType.Int, 4));
cmd.Parameters["@PageSize"].Value = Datagrid1.PageSize;
// open the connection and get the Reader
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
// bind the reader to the DataList
Datagrid1.DataSource = reader;
Datagrid1.DataBind();
// close the reader and the connection
reader.Close();
conn.Close();
}
protected void Datagrid1_SelectionChanged(object sender, EventArgs e)
{
// create the command and the connection
string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
int empID = (int)Datagrid1.DataKeys[Datagrid1.SelectedIndex];
string sql = "SELECT * FROM Employees WHERE EmployeeID = " + empID.ToString();
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
// open the connection and get the Reader
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
// show the results
StringBuilder str = new StringBuilder("");
reader.Read();
str.Append("<b>");
str.Append(reader["FirstName"].ToString());
str.Append(" ");
str.Append(reader["LastName"].ToString());
str.Append("<br>");
str.Append(reader["Title"].ToString());
str.Append("<br>");
str.Append(reader["Address"].ToString());
str.Append("<br>");
str.Append(reader["City"].ToString());
str.Append(", ");
str.Append(reader["Region"].ToString());
str.Append("</b><br>");
str.Append(reader["Notes"].ToString());
MoreInfo.Text = str.ToString();
// close the reader and the connection
reader.Close();
conn.Close();
// rebind the grid
BindGrid();
}
protected void Datagrid1_PageChanged(Object sender, DataGridPageChangedEventArgs e)
{
// deselect the currently selected row, if any
Datagrid1.SelectedIndex = -1;
MoreInfo.Text = "";
// change the current page and rebind
Datagrid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}</script>if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_GetEmployeesByPage]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_GetEmployeesByPage]
GOSET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GOCREATE PROCEDURE sp_GetEmployeesByPage
@PageNumber int,
@PageSize int
AS-- create a temporary table with the columns we are interested in
CREATE TABLE #TempEmployees
(
ID int IDENTITY PRIMARY KEY,
EmployeeID int,
LastName nvarchar(20),
FirstName nvarchar(10),
Title nvarchar(30),
TitleOfCourtesy nvarchar(25),
Address nvarchar(60),
City nvarchar(15),
Region nvarchar(15),
Country nvarchar(15),
Notes ntext
)-- fill the temp table with all the employees
INSERT INTO #TempEmployees
(
EmployeeID,
LastName,
FirstName,
Title,
TitleOfCourtesy,
Address,
City,
Region,
Country,
Notes
)
SELECT
EmployeeID,
LastName,
FirstName,
Title,
TitleOfCourtesy,
Address,
City,
Region,
Country,
Notes
FROM
Employees ORDER BY EmployeeID ASC-- declare two variables to calculate the range of records to extract for the specified page
DECLARE @FromID int
DECLARE @ToID int
-- calculate the first and last ID of the range of records we need
SET @FromID = ((@PageNumber - 1) * @PageSize) + 1
SET @ToID = @PageNumber * @PageSize-- select the page of records
SELECT * FROM #TempEmployees WHERE ID >= @FromID AND ID <= @ToID
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
解决方案 »
- 服务连接不上sqlserver问题
- 高分求如何实现类似于Strig.Trim()的功能
- visio只有“反向工程”,那么做好的E-R图,怎么把表从数据库里建起来??
- 哈希表排序问题,如何按照加入哈希表顺序访问或取出哈希表的内容?
- 求一个c# 局域网语音聊天的实例
- C# 中如何实现磁性窗体
- 求助。。。TreeView 的Microsoft.Web.UI.WebControls.DLL 在虚拟目录下的一个文件夹下怎么使用?
- C# 异步回调完成时打开窗体
- 那里有最新的vs2005的combobox控件下载,并告诉大概用法
- c#如何定义一个所有form可以访问的全局变量?
- iis问题,请教高手(100分)
- 请问在C#中怎样做类似VC中多文档的程序
<asp:BoundColumn HeaderText="ID" ItemStyle-Width="30px" DataField="EmployeeID"/>
是在HTML里写的代码,并不是在。CS里所写的代码,这样不能动太的改变我想显示的字段名称,或者显示名称!!
如果在。CS里所写的代码就能动态的改变的字段名称和显示名称
??有没有更好的方法??
THANK YOU!