ASP.NET设计GridView控件的使用方法
2013-06-20 18:34
447 查看
页面GridView控件设计代码:
后台代码实现:
BindToGirdView()函数中的if语句可以令该表中的数据为空的时候显示出表头,如果没有这个语句,当数据为空时表格则不会显示出来。
“this.Commentslist.Rows[e.NewSelectedIndex].Cells[2].Text”语句可以用来获取表格中的数据,Rows[e.NewSelectedIndex]表示当前选择的行,Cells[]用来标识获取哪一列中的数据。
<asp:GridView ID="Commentslist" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="UserID" OnPageIndexChanging="Commentslist_PageIndexChanging" OnRowDeleting="Commentslist_RowDeleting" OnSelectedIndexChanging="Commentslist_Looking" PageSize="5"> <Columns> <asp:BoundField DataField="UserID" HeaderText="用户ID"> <HeaderStyle Height="30px" Width="100px" /> <ItemStyle Height="30px" Width="100px" /> </asp:BoundField> <asp:BoundField DataField="UserName" HeaderText="用户名字"> <HeaderStyle Height="30px" Width="100px" /> <ItemStyle Height="30px" Width="100px" /> </asp:BoundField> <asp:BoundField DataField="CommentID" HeaderText="编号ID"> <HeaderStyle Height="30px" Width="60px" /> <ItemStyle Height="30px" Width="60px" /> </asp:BoundField> <asp:BoundField DataField="CommentInfor" HeaderText="内容"> <HeaderStyle Height="30px" Width="250px" /> <ItemStyle Height="30px" Width="250px" /> </asp:BoundField> <asp:CommandField HeaderText="查看" SelectText="查看" ShowSelectButton="True"> <HeaderStyle Height="30px" Width="50px" /> <ItemStyle Height="30px" Width="50px" /> </asp:CommandField> <asp:CommandField HeaderText="删除" ShowDeleteButton="True"> <HeaderStyle Height="30px" Width="50px" /> <ItemStyle Height="30px" Width="50px" /> </asp:CommandField> </Columns> </asp:GridView>
后台代码实现:
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) this.BindToGirdView(); } private void BindToGirdView() { string Userid = Session["userid"].ToString(); SqlConnection sqlConnection = new SqlConnection(); sqlConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["db"].ToString(); string sqlStr = "select * from Comment where UserID=" + Userid + "or UserID in (select FriendsID from Friends where UserID=" + Userid + ")"; SqlDataAdapter sda = new SqlDataAdapter(sqlStr, sqlConnection); DataSet ds = new DataSet(); sqlConnection.Open(); sda.Fill(ds, "Comments"); sqlConnection.Close(); if (ds.Tables["Comments"].Rows.Count == 0) { ds.Tables[0].Rows.Add(ds.Tables[0].NewRow()); Commentslist.DataSource = ds.Tables[0].DefaultView; Commentslist.DataBind(); Commentslist.Rows[0].Cells.Clear(); } else this.Commentslist.DataSource = ds.Tables["Comments"].DefaultView; this.Commentslist.DataBind(); } protected void Commentslist_PageIndexChanging(object sender, GridViewPageEventArgs e) { Commentslist.PageIndex = e.NewPageIndex; this.BindToGirdView(); } protected void Commentslist_RowDeleting(object sender, GridViewDeleteEventArgs e) { string Userid = Session["userid"].ToString(); SqlConnection sqlConnection = new SqlConnection(); sqlConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["db"].ToString(); string sqlStr = "select * from Comment where UserID=" + Userid + "or UserID in (select FriendsID from Friends where UserID=" + Userid + ")"; SqlDataAdapter sda = new SqlDataAdapter(sqlStr, sqlConnection); DataSet ds = new DataSet(); sqlConnection.Open(); sda.Fill(ds, "Comments"); sqlConnection.Close(); if (ds.Tables["Comments"].Rows.Count == 0) Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('列表为空,删除失败!');</script>"); else { int CommentID = Convert.ToInt32(this.Commentslist.Rows[e.RowIndex].Cells[2].Text); int result1 = BLL.DeleteCommentData(CommentID); //int result2 = BLL.DeleteAnswerCommentData(CommentID); //if (result1 > 0 && result2 > 0) if (result1 > 0) { Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('删除成功!');</script>"); this.BindToGirdView(); } else { Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('删除失败!');</script>"); this.BindToGirdView(); } } } protected void Commentslist_Looking(object sender, GridViewSelectEventArgs e) { Session["commentid"] = Convert.ToInt32(this.Commentslist.Rows[e.NewSelectedIndex].Cells[2].Text); Response.Redirect("AnswerCommentlist.aspx"); }
BindToGirdView()函数中的if语句可以令该表中的数据为空的时候显示出表头,如果没有这个语句,当数据为空时表格则不会显示出来。
“this.Commentslist.Rows[e.NewSelectedIndex].Cells[2].Text”语句可以用来获取表格中的数据,Rows[e.NewSelectedIndex]表示当前选择的行,Cells[]用来标识获取哪一列中的数据。
相关文章推荐
- 灵活掌握asp.net中gridview控件的多种使用方法(下)
- 灵活掌握asp.net中gridview控件的多种使用方法(上)
- asp.net中GridView控件使用HyperLinkField的方法
- Asp.net_GridView控件中添加的RadioButton的使用方法
- 使用Asp.net 2.0设计的三种控件(TreeView,DropDownList,GridView)的无限分类
- 灵活掌握asp.net中gridview控件的多种使用方法(上)
- 灵活掌握asp.net中gridview控件的多种使用方法(下)
- Asp .NET使用控件常见问题大全(设计时选中、Button、TextBox、DropDownList、GridView、UpdatePanel 等)
- asp.net的GridView控件使用方法大全
- asp.net中使用分页控件,并绑定数据到GridView或Repeater
- ASP.NET Substitution 控件的使用方法
- ASP.NET Substitution 控件的使用方法
- ASP.NET中 TextBox 文本输入框控件的使用方法
- [ASP.NET][实例]用户控件的设计与使用
- 不使用ASP.NET服务器端控件(包括form表单不加runat="server")来触发.cs里的事件(方法),(适用于有代码洁癖者)。
- ASP.NET中日历控件和JS版日历控件的使用方法(第5节)
- 黑马程序员之 ASP.NET学习笔记:GridView控件的常用属性及方法
- asp.net下的“Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用。”错误的一个可能的成因
- 在ASP.NET中使用ObjectDataSource数据控件和GridView显示数据(2)