DataGrid 示例

原创 2004年08月10日 09:43:00
ExpandedBlockStart.gif<%@ Page Language="C#"%>
ExpandedBlockStart.gif
<%@ Import Namespace="System.Data" %>
ExpandedBlockStart.gif
<%@ Import Namespace="System.Data.SqlClient" %>
None.gif
<HTML>
None.gif 
<HEAD>
None.gif  
<TITLE>1< SPAN>TITLE>
ExpandedBlockStart.gif  
<SCRIPT language="C#" runat="server">
InBlock.gif  private 
void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gif  
{
InBlock.gif   
if(!IsPostBack)
ExpandedSubBlockStart.gif   
{
InBlock.gif    dgBind();
ExpandedSubBlockEnd.gif   }
   
ExpandedSubBlockEnd.gif  }

InBlock.gif  private 
void dgBind()
ExpandedSubBlockStart.gif  
{
InBlock.gif   SqlConnection conn 
= new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["pubs"]);
InBlock.gif   string sqlStr 
= "SELECT au_id, au_lname, au_fname, address FROM authors";
InBlock.gif   SqlDataAdapter da 
= new SqlDataAdapter(sqlStr, conn);
InBlock.gif   DataTable dt 
= new DataTable();
InBlock.gif   da.Fill(dt);
InBlock.gif   da.Dispose();
InBlock.gif   conn.Close();
InBlock.gif   dg.DataSource 
= dt;
InBlock.gif   dg.DataBind();
ExpandedSubBlockEnd.gif  }

InBlock.gif  #region Web 窗体设计器生成的代码
InBlock.gif  override protected 
void OnInit(EventArgs e)
ExpandedSubBlockStart.gif  
{
InBlock.gif   
//
InBlock.gif
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif
   //
InBlock.gif
   InitializeComponent();
InBlock.gif   base.OnInit(e);
ExpandedSubBlockEnd.gif  }

InBlock.gif  
InBlock.gif  
//
InBlock.gif
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif
  /// 此方法的内容。
InBlock.gif
  //
InBlock.gif
  private void InitializeComponent()
ExpandedSubBlockStart.gif  
{    
InBlock.gif   
this.dg.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dg_PageIndexChanged);
InBlock.gif   
this.dg.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_CancelCommand);
InBlock.gif   
this.dg.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_EditCommand);
InBlock.gif   
this.dg.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_UpdateCommand);
InBlock.gif   
this.dg.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_DeleteCommand);
InBlock.gif   
this.dg.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dg_ItemDataBound);
InBlock.gif   
this.dg.SelectedIndexChanged += new System.EventHandler(this.dg_SelectedIndexChanged);
InBlock.gif   
this.Load += new System.EventHandler(this.Page_Load);
ExpandedSubBlockEnd.gif  }

InBlock.gif  #endregion
InBlock.gif  private 
void Button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gif  
{
ExpandedSubBlockEnd.gif  }

InBlock.gif  private 
void dg_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gif  
{
InBlock.gif   dg.EditItemIndex 
= -1;
InBlock.gif   dg.SelectedIndex 
= e.Item.ItemIndex;
InBlock.gif   dgBind();
ExpandedSubBlockEnd.gif  }

InBlock.gif  private 
void dg_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gif  
{
InBlock.gif   string au_id 
= dg.DataKeys[e.Item.ItemIndex].ToString();
InBlock.gif   SqlConnection conn 
= new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["pubs"]);
InBlock.gif   string sqlStr 
= "DELETE FROM authors WHERE au_id = '" + au_id + "'";
InBlock.gif   SqlCommand cmd 
= new SqlCommand(sqlStr, conn);
InBlock.gif   conn.Open();
InBlock.gif   cmd.ExecuteNonQuery();
InBlock.gif   conn.Close();
InBlock.gif   dgBind();
ExpandedSubBlockEnd.gif  }

InBlock.gif  private 
void dg_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gif  
{
InBlock.gif   dg.EditItemIndex 
= e.Item.ItemIndex;
InBlock.gif   dg.SelectedIndex 
= e.Item.ItemIndex;
InBlock.gif   dgBind();
ExpandedSubBlockEnd.gif  }

InBlock.gif  private 
void dg_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedSubBlockStart.gif  
{
InBlock.gif   dg.Controls[
0].EnableViewState = false;
ExpandedSubBlockEnd.gif  }

InBlock.gif  private 
void dg_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gif  
{
InBlock.gif   string au_id 
= dg.DataKeys[e.Item.ItemIndex].ToString();
InBlock.gif   string au_fname 
= ((TextBox)(e.Item.Cells[2].FindControl("_ctl2"))).Text;
InBlock.gif   SqlConnection conn 
= new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["pubs"]);
InBlock.gif   string sqlStr 
= "UPDATE authors SET au_fname = '" + au_fname + "' WHERE ";
InBlock.gif   sqlStr 
+= "au_id = '" + au_id + "'";
InBlock.gif   Response.Write(sqlStr);
InBlock.gif   SqlCommand cmd 
= new SqlCommand(sqlStr, conn);
InBlock.gif   conn.Open();
InBlock.gif   cmd.ExecuteNonQuery();
InBlock.gif   conn.Close();
InBlock.gif   dg.EditItemIndex 
= -1;
InBlock.gif   dg.SelectedIndex 
= -1;
InBlock.gif   dgBind();
ExpandedSubBlockEnd.gif  }

InBlock.gif  private 
void dg_SelectedIndexChanged(object sender, System.EventArgs e)
ExpandedSubBlockStart.gif  
{
InBlock.gif   Label1.Text 
= dg.DataKeys[dg.SelectedIndex].ToString();
InBlock.gif   dg.EditItemIndex 
= dg.SelectedIndex;
InBlock.gif   dgBind();
ExpandedSubBlockEnd.gif  }

InBlock.gif  private 
void dg_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
ExpandedSubBlockStart.gif  
{
InBlock.gif   dg.EditItemIndex 
= -1;
InBlock.gif   dg.SelectedIndex 
= -1;
InBlock.gif   dg.CurrentPageIndex 
= e.NewPageIndex;
InBlock.gif   dgBind();
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif  
< SPAN>SCRIPT>
None.gif  
<META content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
None.gif  
<META content="C#" name="CODE_LANGUAGE">
None.gif  
<META content="JavaScript" name="vs_defaultClientScript">
None.gif  
<META content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
None.gif  
<LINK href="style.css" type="text/css" rel="stylesheet">
None.gif 
< SPAN>HEAD>
None.gif 
<BODY>
None.gif  
<FORM id="Form1" method="post" runat="server">
None.gif   
<ASP:LABEL id="Label1" runat="server">Label< SPAN>ASP:LABEL><BR>
None.gif   
<BR>
None.gif   
<ASP:DATAGRID id="dg" runat="server" bordercolor="#CC9966" borderstyle="None" borderwidth="1px"
None.gif    backcolor
="White" cellpadding="4" allowsorting="True" allowpaging="True" pagesize="7" width="600px"
None.gif    autogeneratecolumns
="False" datakeyfield="au_id">
None.gif    
<SELECTEDITEMSTYLE font-bold="True" horizontalalign="Center" forecolor="#663399" backcolor="#FFCC66">< SPAN>SELECTEDITEMSTYLE>
None.gif    
<ITEMSTYLE horizontalalign="Center" forecolor="#330099" backcolor="White">< SPAN>ITEMSTYLE>
None.gif    
<HEADERSTYLE font-bold="True" horizontalalign="Center" forecolor="#FFFFCC" backcolor="#990000">< SPAN>HEADERSTYLE>
None.gif    
<FOOTERSTYLE forecolor="#330099" backcolor="#FFFFCC">< SPAN>FOOTERSTYLE>
None.gif    
<COLUMNS>
None.gif     
<ASP:BUTTONCOLUMN text="选择" datatextfield="au_id" headertext="au_id" commandname="Select">
None.gif      
<HEADERSTYLE width="100px">< SPAN>HEADERSTYLE>
None.gif     
< SPAN>ASP:BUTTONCOLUMN>
None.gif     
<ASP:HYPERLINKCOLUMN datanavigateurlfield="au_lname" datanavigateurlformatstring="userlist.aspx?lname={0}"  datatextfield="au_lname" headertext="au_lname">
None.gif      
<HEADERSTYLE width="80px">< SPAN>HEADERSTYLE>
None.gif     
< SPAN>ASP:HYPERLINKCOLUMN>
None.gif     
<ASP:BOUNDCOLUMN datafield="au_fname" headertext="au_fname">
None.gif      
<HEADERSTYLE width="80px">< SPAN>HEADERSTYLE>
None.gif     
< SPAN>ASP:BOUNDCOLUMN>
None.gif     
<ASP:TEMPLATECOLUMN headertext="address">
None.gif      
<ITEMSTYLE horizontalalign="Left">< SPAN>ITEMSTYLE>
None.gif      
<ITEMTEMPLATE>
ExpandedBlockStart.gif       
<%# DataBinder.Eval(Container.DataItem, "address") %>
None.gif      
< SPAN>ITEMTEMPLATE>
None.gif     
< SPAN>ASP:TEMPLATECOLUMN>
None.gif     
<ASP:EDITCOMMANDCOLUMN buttontype="LinkButton" updatetext="更新" canceltext="取消" edittext="编辑">
None.gif      
<HEADERSTYLE width="70px">< SPAN>HEADERSTYLE>
None.gif     
< SPAN>ASP:EDITCOMMANDCOLUMN>
None.gif     
<ASP:BUTTONCOLUMN text="删除" commandname="Delete">
None.gif      
<HEADERSTYLE width="50px">< SPAN>HEADERSTYLE>
None.gif     
< SPAN>ASP:BUTTONCOLUMN>
None.gif    
< SPAN>COLUMNS>
None.gif    
<PAGERSTYLE nextpagetext=">>" prevpagetext="<<" horizontalalign="Center" forecolor="#330099" backcolor="#FFFFCC">< SPAN>PAGERSTYLE>
None.gif   
< SPAN>ASP:DATAGRID>
None.gif  
< SPAN>FORM>
None.gif 
< SPAN>BODY>
None.gif
< SPAN>HTML>
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:DataGrid 示例
举报原因:
原因补充:

(最多只允许输入30个字)