1.界面:
2.数据库还是用的微软提供的本身NORTHWIND库.本界面用datagird控件实现了分页,按照用户定制要求排序,.对某个栏位的隐藏,删除,编辑功能.遍历控件cell实现批量删除
可以用手动的配置或则是数据库的自动的配置.其实一般都没有在代码上写,都是通过可视话界面进行改动
ASP代码如下:
<asp:DataGrid ID="DataGrid1" runat="server" Style="z-index: 100; left: 26px; position: absolute;
top: 28px" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" AllowPaging="True" AutoGenerateColumns="False" OnPageIndexChanged="DataGrid1_PageIndexChanged" PageSize="3" ShowFooter="True" OnItemDataBound="DataGrid1_ItemDataBound" AllowSorting="True" OnDeleteCommand="DataGrid1_DeleteCommand" OnSortCommand="DataGrid1_SortCommand" OnCancelCommand="DataGrid1_CancelCommand" OnEditCommand="DataGrid1_EditCommand" OnUpdateCommand="DataGrid1_UpdateCommand">
<FooterStyle BackColor="Tan" />
<SelectedItemStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" NextPageText="下一页" PrevPageText="上一页" />
<AlternatingItemStyle BackColor="PaleGoldenrod" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<Columns>
<asp:HyperLinkColumn DataNavigateUrlField="employeeID" DataNavigateUrlFormatString="showDetails.aspx?empID={0}"
DataTextField="employeeID" HeaderText="员工编号" Target="_blank"></asp:HyperLinkColumn>
<asp:BoundColumn DataField="employeeID" HeaderText="员工编号" ReadOnly="True"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="员工生日">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.BirthDate", "{0:D}") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.BirthDate", "{0:D}") %>'></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" Display="Dynamic" ErrorMessage="日期不合法"
Type="Date" ControlToValidate="TextBox1" Operator="DataTypeCheck"></asp:CompareValidator>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Title" HeaderText="标题"></asp:BoundColumn>
<asp:BoundColumn DataField="FirstName" HeaderText="名"></asp:BoundColumn>
<asp:BoundColumn DataField="LastName" HeaderText="姓"></asp:BoundColumn>
<asp:ButtonColumn CommandName="Delete" Text="删除"></asp:ButtonColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton runat="server" CausesValidation="false" CommandName="Edit" ID="Edit" Text="编辑"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton runat="server" CommandName="Update" Text="更新"></asp:LinkButton>
<asp:LinkButton runat="server" CausesValidation="false" CommandName="Cancel" Text="取消"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="删除">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxSelect" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
3.后台代码:
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 System.Data.SqlClient;

public partial class DataGridTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindToDataGird();
}
}
private void BindToDataGird()
{
SqlConnection conn = DB.getConnection();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select * from employees", conn);
DataSet ds = new DataSet();
sda.Fill(ds, "emp");
DataGrid1.DataKeyField = "employeeID";

DataGrid1.DataSource = ds.Tables["emp"];
DataGrid1.DataBind();
}
protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindToDataGird();
}

protected void ButtonHide_Click(object sender, EventArgs e)
{
//直接把第二项的可见性设置为FALSE
DataGrid1.Columns[1].Visible = false;
BindToDataGird();
}
//当数据每一项绑定的时候
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//小技巧,实际上是本地JS脚本事件
e.Item.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#559944'");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
//Cell表示是单元格Controls表示里边的控件
((LinkButton)(e.Item.Cells[6].Controls[0])).Attributes.Add("onclick","return confirm('你确认删除???');");
}

}
protected void DataGrid1_SortCommand(object source, DataGridSortCommandEventArgs e)
{
//ViewState存储在客户端.
if (ViewState["Order"] == null)
{
ViewState["Order"] ="ASC";
}
else
{
if (ViewState["Order"].ToString() == "ASC")
{
ViewState["Order"] = "DESC";
}
else
{
ViewState["Order"] = "ASC";
}
}
//数据绑定显示
SqlConnection conn = DB.getConnection();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select * from employees", conn);
DataSet ds = new DataSet();
sda.Fill(ds, "emp");
ds.Tables["emp"].DefaultView.Sort = e.SortExpression+" " +ViewState["Order"].ToString();
DataGrid1.DataSource = ds.Tables["emp"].DefaultView;
DataGrid1.DataBind();
}
protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
{
string empID = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
SqlConnection conn = DB.getConnection();
SqlCommand cmd = new SqlCommand("delete from employees where employeeID = '" + empID + "'", conn);
conn.Open();
cmd.ExecuteNonQuery();
BindToDataGird();
}
protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindToDataGird();
}
protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindToDataGird();
}
protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
{
string empID = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
//这个就是取的值的方法.
string lastName = ((TextBox)(e.Item.Cells[2].Controls[0])).Text;
//之后连接数据库之后UPDATE就可以了!不再写了!
}
protected void ButtonDel_Click(object sender, EventArgs e)
{
//遍历DataGrid1所有行
foreach (System.Web.UI.WebControls.DataGridItem dll in DataGrid1.Items)
{
//找到名为"CheckBoxSelect"的控件
CheckBox chk = (CheckBox)dll.FindControl("CheckBoxSelect");
if (chk.Checked)
{
Response.Write(dll.Cells[1].Text);
}
}
}
}
| 员工编号 | 员工编号 | 员工生日 | 标题 | 名 | 姓 | 删除 | ||
| 1 | 1 | 1948年12月8日 | Sales Representative | Nancy | Davolio | 删除 | 编辑 | |
| 2 | 2 | 1952年2月19日 | Vice President, Sales | Andrew | Fuller | 删除 | 编辑 | |
| 3 | 3 | 1963年8月30日 | Sales Representative | Janet | Leverling | 删除 | 编辑 | |
| 上一页 下一页 | ||||||||
2.数据库还是用的微软提供的本身NORTHWIND库.本界面用datagird控件实现了分页,按照用户定制要求排序,.对某个栏位的隐藏,删除,编辑功能.遍历控件cell实现批量删除
可以用手动的配置或则是数据库的自动的配置.其实一般都没有在代码上写,都是通过可视话界面进行改动
ASP代码如下:
<asp:DataGrid ID="DataGrid1" runat="server" Style="z-index: 100; left: 26px; position: absolute;
top: 28px" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" AllowPaging="True" AutoGenerateColumns="False" OnPageIndexChanged="DataGrid1_PageIndexChanged" PageSize="3" ShowFooter="True" OnItemDataBound="DataGrid1_ItemDataBound" AllowSorting="True" OnDeleteCommand="DataGrid1_DeleteCommand" OnSortCommand="DataGrid1_SortCommand" OnCancelCommand="DataGrid1_CancelCommand" OnEditCommand="DataGrid1_EditCommand" OnUpdateCommand="DataGrid1_UpdateCommand">
<FooterStyle BackColor="Tan" />
<SelectedItemStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" NextPageText="下一页" PrevPageText="上一页" />
<AlternatingItemStyle BackColor="PaleGoldenrod" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<Columns>
<asp:HyperLinkColumn DataNavigateUrlField="employeeID" DataNavigateUrlFormatString="showDetails.aspx?empID={0}"
DataTextField="employeeID" HeaderText="员工编号" Target="_blank"></asp:HyperLinkColumn>
<asp:BoundColumn DataField="employeeID" HeaderText="员工编号" ReadOnly="True"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="员工生日">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.BirthDate", "{0:D}") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.BirthDate", "{0:D}") %>'></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" Display="Dynamic" ErrorMessage="日期不合法"
Type="Date" ControlToValidate="TextBox1" Operator="DataTypeCheck"></asp:CompareValidator>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Title" HeaderText="标题"></asp:BoundColumn>
<asp:BoundColumn DataField="FirstName" HeaderText="名"></asp:BoundColumn>
<asp:BoundColumn DataField="LastName" HeaderText="姓"></asp:BoundColumn>
<asp:ButtonColumn CommandName="Delete" Text="删除"></asp:ButtonColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton runat="server" CausesValidation="false" CommandName="Edit" ID="Edit" Text="编辑"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton runat="server" CommandName="Update" Text="更新"></asp:LinkButton>
<asp:LinkButton runat="server" CausesValidation="false" CommandName="Cancel" Text="取消"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="删除">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxSelect" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>3.后台代码:
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 System.Data.SqlClient;
public partial class DataGridTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindToDataGird();
}
}
private void BindToDataGird()
{
SqlConnection conn = DB.getConnection();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select * from employees", conn);
DataSet ds = new DataSet();
sda.Fill(ds, "emp");
DataGrid1.DataKeyField = "employeeID";
DataGrid1.DataSource = ds.Tables["emp"];
DataGrid1.DataBind();
}
protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindToDataGird();
}
protected void ButtonHide_Click(object sender, EventArgs e)
{
//直接把第二项的可见性设置为FALSE
DataGrid1.Columns[1].Visible = false;
BindToDataGird();
}
//当数据每一项绑定的时候
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//小技巧,实际上是本地JS脚本事件
e.Item.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#559944'");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
//Cell表示是单元格Controls表示里边的控件
((LinkButton)(e.Item.Cells[6].Controls[0])).Attributes.Add("onclick","return confirm('你确认删除???');");
}

}
protected void DataGrid1_SortCommand(object source, DataGridSortCommandEventArgs e)
{
//ViewState存储在客户端.
if (ViewState["Order"] == null)
{
ViewState["Order"] ="ASC";
}
else
{
if (ViewState["Order"].ToString() == "ASC")
{
ViewState["Order"] = "DESC";
}
else
{
ViewState["Order"] = "ASC";
}
}
//数据绑定显示
SqlConnection conn = DB.getConnection();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select * from employees", conn);
DataSet ds = new DataSet();
sda.Fill(ds, "emp");
ds.Tables["emp"].DefaultView.Sort = e.SortExpression+" " +ViewState["Order"].ToString();
DataGrid1.DataSource = ds.Tables["emp"].DefaultView;
DataGrid1.DataBind();
}
protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
{
string empID = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
SqlConnection conn = DB.getConnection();
SqlCommand cmd = new SqlCommand("delete from employees where employeeID = '" + empID + "'", conn);
conn.Open();
cmd.ExecuteNonQuery();
BindToDataGird();
}
protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindToDataGird();
}
protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindToDataGird();
}
protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
{
string empID = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
//这个就是取的值的方法.
string lastName = ((TextBox)(e.Item.Cells[2].Controls[0])).Text;
//之后连接数据库之后UPDATE就可以了!不再写了!
}
protected void ButtonDel_Click(object sender, EventArgs e)
{
//遍历DataGrid1所有行
foreach (System.Web.UI.WebControls.DataGridItem dll in DataGrid1.Items)
{
//找到名为"CheckBoxSelect"的控件
CheckBox chk = (CheckBox)dll.FindControl("CheckBoxSelect");
if (chk.Checked)
{
Response.Write(dll.Cells[1].Text);
}
}
}
}


浙公网安备 33010602011771号