在写一个留言本的后台管理时用到了GridView和DetailsView,顺便把它们的一些用法记下来
主要有GridView的排序操作和DetailsView的更新操作
前台代码:
<body> <form id="form1" runat="server"> <table style=" 100%"> <tr> <td style=" 40%; padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; vertical-align: top; padding-top: 0px; text-align: left;" valign="top"> <asp:GridView ID="GridView1" runat="server" Width="100%" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="Id" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" PageSize="3" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDeleting="GridView1_RowDeleting" OnSorting="GridView1_Sorting"> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <Columns> <asp:BoundField DataField="Title" HeaderText="标题" SortExpression="Title" /> <asp:BoundField DataField="AddDate" HeaderText="添加日期" SortExpression="AddDate" /> <asp:CheckBoxField DataField="IsChecked" HeaderText="通过审核" SortExpression="IsChecked" /> <asp:CommandField ShowSelectButton="True" /> <asp:CommandField ShowDeleteButton="True" /> </Columns> <RowStyle BackColor="#EFF3FB" /> <EditRowStyle BackColor="#2461BF" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView> </td> <td style=" 60%; padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; vertical-align: top; padding-top: 0px; text-align: left;"> <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="100%" AutoGenerateRows="False" CellPadding="4" DataKeyNames="Id" ForeColor="#333333" GridLines="None" OnItemUpdating="DetailsView1_ItemUpdating" OnModeChanging="DetailsView1_ModeChanging"> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" /> <EditRowStyle BackColor="#2461BF" /> <RowStyle BackColor="#EFF3FB" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <Fields>
<asp:TemplateField HeaderText= "ID "> <EditItemTemplate> <asp:Label ID="lblID" runat="server" Text='<%# Bind("Id") %>'></asp:Label> </EditItemTemplate> <InsertItemTemplate> <asp:Label ID="lblID" runat="server" Text='<%# Bind("Id") %>'></asp:Label> </InsertItemTemplate> <ItemTemplate> <asp:Label ID="lblID" runat="server" Text='<%# Bind("Id") %>'></asp:Label> </ItemTemplate> </asp:TemplateField>
<asp:BoundField DataField="Student" HeaderText="学生" SortExpression="Student" ReadOnly="True" /> <asp:BoundField DataField="Department" HeaderText="系部" SortExpression="Department" ReadOnly="True" />
<asp:BoundField DataField="Class" HeaderText="班级" SortExpression="Class" ReadOnly="True" /> <asp:BoundField DataField="Qq" HeaderText="QQ" SortExpression="Qq" ReadOnly="True" /> <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" ReadOnly="True" /> <asp:BoundField DataField="Telephone" HeaderText="电话" SortExpression="Telephone" ReadOnly="True" /> <asp:BoundField DataField="Title" HeaderText="标题" SortExpression="Title" ReadOnly="True" /> <asp:BoundField DataField="AddDate" HeaderText="添加日期" SortExpression="AddDate" ReadOnly="True" />
<asp:TemplateField HeaderText= "是否通过审核 "> <EditItemTemplate> <asp:CheckBox ID="check" runat="server" Checked='<%# Bind("IsChecked") %>' /> </EditItemTemplate> <InsertItemTemplate> <asp:CheckBox ID="check" runat="server" Checked='<%# Bind("IsChecked") %>' /> </InsertItemTemplate> <ItemTemplate>
<asp:CheckBox ID="check" runat="server" Checked='<%# Bind("IsChecked") %>' /> </ItemTemplate> </asp:TemplateField>
<asp:BoundField DataField="Content" HeaderText="留言内容" SortExpression="Content" ReadOnly="True" />
<asp:TemplateField HeaderText= "回复内容 "> <EditItemTemplate>
<asp:TextBox ID="txtReply" runat="server" Text='<%#Bind("Reply") %>' Width="100%" Height="202px" TextMode="MultiLine"></asp:TextBox> </EditItemTemplate> <InsertItemTemplate> <asp:TextBox ID="txtReply" runat="server" Text='<%#Bind("Reply") %>' Width="100%" Height="202px" TextMode="MultiLine"></asp:TextBox> </InsertItemTemplate> <ItemTemplate>
<asp:Label ID="lblReply" runat="server" Text='<%#Bind("Reply") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:CommandField ShowEditButton="True" EditText="回复" UpdateText="确定" />
</Fields> <FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" />
</asp:DetailsView> </td> </tr> </table> </form> </body> |
后台代码:
public partial class Admin_Module_Comment_ManageComment : System.Web.UI.Page { Comment myComment = new Comment(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //设置当前排序方式 ViewState["SortOrder"] = "AddDate"; ViewState["OrderDire"] = "ASC"; //绑定GridView和DetailsView数据 BindData();
} } //当GridView单击选择时 DetailsView1显示详细数据 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) {
this.DetailsView1.PageIndex = this.GridView1.SelectedRow.DataItemIndex; this.DetailsView1.ChangeMode(DetailsViewMode.ReadOnly); BindDetails(); } #region 数据绑定
//绑定GridView和DetailsView数据 private void BindData() { BindGridView(); BindDetails();
} //GridView数据源 private void BindGridView() { //GetAllComments是自己写的一个方法,返回所有数据 DataSet ds = myComment.GetAllComments(); DataView view = ds.Tables[0].DefaultView; string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"]; view.Sort = sort; GridView1.DataSource = view; this.GridView1.DataBind();
} //Details数据源 private void BindDetails() {
this.DetailsView1.DataSource = myComment.GetAllComments(); this.DetailsView1.DataBind(); }
#endregion //DetailsView更新操作 protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e) {
//当使用DataSource 可使用这各方法 //string strReply = e.NewValues["Reply"].ToString(); //int id =Int32.Parse (e.OldValues[0].ToString());
TextBox tb = ((TextBox)DetailsView1.FindControl("txtReply")); Label lb =((Label)DetailsView1.FindControl("lblId")); CheckBox ck = ((CheckBox)DetailsView1.FindControl("check"));
string strReply = tb.Text.Trim().ToString(); int id =Int32.Parse ( lb.Text.Trim().ToString()); bool chk = false; if (ck.Checked) { chk = true; } else { chk = false; } //这是一个添加回复方法 myComment.AddReply(id, strReply, chk); //切换模式 this.DetailsView1.ChangeMode(DetailsViewMode.ReadOnly); //重新绑定GridView和DetailsView数据 BindData();
} //gridview分页 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { this.GridView1.PageIndex = e.NewPageIndex; BindGridView(); } // 判断DetailsView模式 protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e) { //判断模式 if (e.NewMode == DetailsViewMode.Edit) { DetailsView1.ChangeMode(DetailsViewMode.Edit); } if (e.NewMode == DetailsViewMode.Insert) { DetailsView1.ChangeMode(DetailsViewMode.Insert); } if (e.NewMode == DetailsViewMode.ReadOnly) { DetailsView1.ChangeMode(DetailsViewMode.ReadOnly); } //绑定数据源 BindDetails();
} //GridView1删除操作 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { //DataKeys应该是一个集合,当GridView加载以后会把指定的值添加到这个集合中,例如ID int id = Int32.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString()); myComment.DeleteComment(id); BindData(); }
//GridView1排序操作 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) {
string sPage = e.SortExpression; if (ViewState["SortOrder"].ToString() == sPage) { if (ViewState["OrderDire"].ToString() == "Desc") ViewState["OrderDire"] = "ASC"; else ViewState["OrderDire"] = "Desc"; } else { ViewState["SortOrder"] = e.SortExpression; } //重新绑定GridView1数据源 BindGridView(); }
} |
数据库表:
表名:Comment
序号 |
列名 |
数据类型 |
长度 |
说明 |
1 |
Id |
int |
ID主键 |
|
2 |
Student |
varchar |
255 |
学生姓名 |
3 |
Department |
varchar |
255 |
系别 |
4 |
Class |
varchar |
255 |
班级 |
5 |
|
varchar |
255 |
|
6 |
|
varchar |
255 |
电子邮件 |
7 |
Telephone |
varchar |
255 |
电话 |
8 |
Face |
varchar |
255 |
头像 |
9 |
Title |
varchar |
255 |
留言标题 |
10 |
Content |
varchar |
0 |
留言内容 |
11 |
AddDate |
datetime |
留言添加日期 |
|
12 |
Reply |
varchar |
0 |
留言回复内容 |
13 |
ReplyDate |
datetime |
留言回复时间 |
|
14 |
IsChecked |
bool |
2 |
是否通过审核,未通过审核,将不会在前台显示 |