标签:style class blog c code java
这两天项目中用到了datagrid控件,之前没怎么用过。经过一番努力,最终效果总算是实现了。我有如此感受:
1、功能强大,用法复杂。
2、细节很多,应该小心。
3、多问多查多试。
前台代码如下:
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="pinpai_lan_Add.aspx.cs" Inherits="wlgl_spring_pinpai_lan_Add" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <meta charset="utf-8"> 8 <!--#include file="../inc/header.aspx"--> 9 </head> 10 <body> 11 <form id="form1" runat="server"> 12 <div> 13 <asp:dataGrid id="dgContents" AutoGenerateColumns="False" 14 HeaderStyle-CssClass="info thead" CssClass="table table-bordered table-hover" 15 gridlines="None" runat="server" oncancelcommand="dgContents_CancelCommand" 16 ondeletecommand="dgContents_DeleteCommand" 17 oneditcommand="dgContents_EditCommand" 18 onupdatecommand="dgContents_UpdateCommand"> 19 <Columns> 20 <asp:BoundColumn DataField="id" Visible="false" ReadOnly="true"></asp:BoundColumn> 21 <asp:BoundColumn DataField="name" HeaderText="品牌栏目名称" ItemStyle-VerticalAlign="Middle"> 22 <ItemStyle VerticalAlign="Middle"></ItemStyle> 23 </asp:BoundColumn> 24 <asp:EditCommandColumn HeaderText="操作" ButtonType="LinkButton" EditText="编辑" UpdateText="更新" CancelText="取消"></asp:EditCommandColumn> 25 26 <asp:ButtonColumn CommandName="Delete" 27 Text="<div onclick="return confirm(‘确定删除该栏目吗?‘)">删除</div>"> 28 </asp:ButtonColumn> 29 30 </Columns> 31 32 <HeaderStyle CssClass="info thead"></HeaderStyle> 33 </asp:dataGrid><br /> 34 <div id="div" runat="server" visible="true" > 35 <span>添加品牌栏目:</span><asp:TextBox runat="server" id="txtLanMu"/><asp:Label Text="" ID="mess" runat="server" ForeColor="Red" /> 36 </div> 37 <asp:Button Text="添加栏目" ID="btn_Add" runat="server" CssClass="btn" 38 onclick="btn_Add_Click" /> 39 <input class="btn" type="button" onClick="location.href=‘background_pinpai.aspx‘;return false;" value="返 回" /> 40 </div> 41 </form> 42 </body> 43 </html>
后台代码:
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; public partial class wlgl_spring_pinpai_lan_Add : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { dgContents.DataSource = SqlHelper.GetPinpai_lan(); dgContents.DataBind(); } } protected void btn_Add_Click(object sender, EventArgs e) { string str=txtLanMu.Text.Trim(); if (str=="") { mess.Text = "必须输入栏目名称!"; return; } DataTable dt = SqlHelper.GetPinpai_lan(); if (dt.Rows.Count>0) { for (int i = 0; i < dt.Rows.Count; i++) { if (dt.Rows[i]["name"].ToString().Trim()==str) { mess.Text = "栏目已存在!"; return; } } } if (SqlHelper.Insert(str)) { mess.Text = "添加栏目成功!"; } else { mess.Text = "添加栏目失败!"; } dgContents.DataSource = SqlHelper.GetPinpai_lan(); dgContents.DataBind(); } protected void dgContents_EditCommand(object source, DataGridCommandEventArgs e) { dgContents.EditItemIndex = e.Item.ItemIndex; dgContents.DataSource = SqlHelper.GetPinpai_lan(); dgContents.DataBind(); } protected void dgContents_CancelCommand(object source, DataGridCommandEventArgs e) { dgContents.EditItemIndex = -1; dgContents.DataSource = SqlHelper.GetPinpai_lan(); dgContents.DataBind(); } protected void dgContents_DeleteCommand(object source, DataGridCommandEventArgs e) { string sid = e.Item.Cells[0].Text; int id; if (int.TryParse(sid, out id)) { if (SqlHelper.DeleteAll(id)) { mess.Text = "删除成功!"; } else { mess.Text = "删除失败!"; } } else { mess.Text = "删除失败!"; } dgContents.DataSource = SqlHelper.GetPinpai_lan(); dgContents.DataBind(); } protected void dgContents_UpdateCommand(object source, DataGridCommandEventArgs e) { string sid =e.Item.Cells[0].Text; int id; string name = ((TextBox)e.Item.Cells[1].Controls[0]).Text; if (int.TryParse(sid,out id)) { if (SqlHelper.Update(id,name)) { mess.Text = "更新成功!"; } else { mess.Text = "更新失败!"; } } else { mess.Text = "更新失败!"; } dgContents.EditItemIndex = -1; dgContents.DataSource = SqlHelper.GetPinpai_lan(); dgContents.DataBind(); } }
我想要的功能是,在datagrid中可以编辑删除数据。
标签:style class blog c code java
原文地址:http://www.cnblogs.com/xintian/p/3737230.html