GridViewUp.aspx文件代码:
<%
@PageLanguage
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
GridViewUp.aspx.cs
"
Inherits
=
"
gridview_GridViewUp
"
%>
<! DOCTYPEhtmlPUBLIC " -//W3C//DTDXHTML1.0Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< htmlxmlns = " http://www.w3.org/1999/xhtml " >
< headrunat = " server " >
< title > 无标题页 </ title >
</ head >
< body >
< formid = " form1 " runat = " server " >
< div >
< tablecellpadding = " 0 " cellspacing = " 0 " border = " 0 " width = " 80% " style = " font-size:11px " >
< tr >
< tdalign = " center " >
< asp:GridViewID = " GridView1 " runat = " server " Width = " 100% " CellPadding = " 4 " ForeColor = " #333333 "
AutoGenerateColumns = " False " AllowPaging = " True " PageSize = " 12 " OnRowCancelingEdit = " GridView1_RowCancelingEdit "
OnRowEditing = " GridView1_RowEditing " OnRowUpdating = " GridView1_RowUpdating " OnRowDeleting = " GridView1_RowDeleting "
DataKeyNames = " id,name " OnPageIndexChanging = " GridView1_PageIndexChanging " DataMember = " card,price " OnRowDataBound = " GridView1_RowDataBound " GridLines = " None " >
< Columns >
< asp:BoundFieldHeaderText = " 编号 " DataField = " id " ReadOnly = " True " />
< asp:BoundFieldDataField = " name " HeaderText = " 姓名 " ReadOnly = " True " />
< asp:TemplateFieldHeaderText = " 身份证号 " >
< ItemTemplate >
<% #Eval( " card " ) %>
</ ItemTemplate >
< EditItemTemplate >
< asp:TextBoxID = " TBCard " Text = ' <%#Eval("card")%> ' runat = " server " Width = " 140px " />
</ EditItemTemplate >
< ItemStyleWidth = " 150px " />
</ asp:TemplateField >
< asp:TemplateFieldHeaderText = " 学历 " >
< ItemTemplate >
<% #Eval( " xueliText " ) %>
</ ItemTemplate >
< EditItemTemplate >
< asp:HiddenFieldID = " HDFXueli " runat = " server " Value = ' <%#Eval("xueli")%> ' />
< asp:DropDownListID = " DDLXueli " runat = " server " Width = " 90px " />
</ EditItemTemplate >
< ItemStyleWidth = " 100px " />
</ asp:TemplateField >
< asp:TemplateFieldHeaderText = " 价格 " >
< ItemTemplate >
<% #Eval( " price " ) %>
</ ItemTemplate >
< EditItemTemplate >
< asp:TextBoxID = " TBPrice " Text = ' <%#Eval("price")%> ' runat = " server " Width = " 90px " />
</ EditItemTemplate >
< ItemStyleWidth = " 100px " />
</ asp:TemplateField >
< asp:BoundFieldHeaderText = " 建立时间 " DataField = " createdate " ReadOnly = " True " />
< asp:CommandFieldShowDeleteButton = " True " ShowEditButton = " True " HeaderText = " 操作 " />
</ Columns >
< PagerSettingsFirstPageText = "" LastPageText = "" NextPageText = "" PreviousPageText = "" />
< RowStyleHeight = " 20px " BackColor = " #F7F6F3 " ForeColor = " #333333 " />
< FooterStyleBackColor = " #5D7B9D " Font - Bold = " True " ForeColor = " White " />
< EditRowStyleBackColor = " #999999 " />
< SelectedRowStyleBackColor = " #E2DED6 " Font - Bold = " True " ForeColor = " #333333 " />
< PagerStyleBackColor = " #284775 " ForeColor = " White " HorizontalAlign = " Center " />
< HeaderStyleBackColor = " #5D7B9D " Font - Bold = " True " ForeColor = " White " />
< AlternatingRowStyleBackColor = " White " ForeColor = " #284775 " />
</ asp:GridView >
</ td >
</ tr >
</ table >
</ div >
</ form >
</ body >
</ html >
<! DOCTYPEhtmlPUBLIC " -//W3C//DTDXHTML1.0Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< htmlxmlns = " http://www.w3.org/1999/xhtml " >
< headrunat = " server " >
< title > 无标题页 </ title >
</ head >
< body >
< formid = " form1 " runat = " server " >
< div >
< tablecellpadding = " 0 " cellspacing = " 0 " border = " 0 " width = " 80% " style = " font-size:11px " >
< tr >
< tdalign = " center " >
< asp:GridViewID = " GridView1 " runat = " server " Width = " 100% " CellPadding = " 4 " ForeColor = " #333333 "
AutoGenerateColumns = " False " AllowPaging = " True " PageSize = " 12 " OnRowCancelingEdit = " GridView1_RowCancelingEdit "
OnRowEditing = " GridView1_RowEditing " OnRowUpdating = " GridView1_RowUpdating " OnRowDeleting = " GridView1_RowDeleting "
DataKeyNames = " id,name " OnPageIndexChanging = " GridView1_PageIndexChanging " DataMember = " card,price " OnRowDataBound = " GridView1_RowDataBound " GridLines = " None " >
< Columns >
< asp:BoundFieldHeaderText = " 编号 " DataField = " id " ReadOnly = " True " />
< asp:BoundFieldDataField = " name " HeaderText = " 姓名 " ReadOnly = " True " />
< asp:TemplateFieldHeaderText = " 身份证号 " >
< ItemTemplate >
<% #Eval( " card " ) %>
</ ItemTemplate >
< EditItemTemplate >
< asp:TextBoxID = " TBCard " Text = ' <%#Eval("card")%> ' runat = " server " Width = " 140px " />
</ EditItemTemplate >
< ItemStyleWidth = " 150px " />
</ asp:TemplateField >
< asp:TemplateFieldHeaderText = " 学历 " >
< ItemTemplate >
<% #Eval( " xueliText " ) %>
</ ItemTemplate >
< EditItemTemplate >
< asp:HiddenFieldID = " HDFXueli " runat = " server " Value = ' <%#Eval("xueli")%> ' />
< asp:DropDownListID = " DDLXueli " runat = " server " Width = " 90px " />
</ EditItemTemplate >
< ItemStyleWidth = " 100px " />
</ asp:TemplateField >
< asp:TemplateFieldHeaderText = " 价格 " >
< ItemTemplate >
<% #Eval( " price " ) %>
</ ItemTemplate >
< EditItemTemplate >
< asp:TextBoxID = " TBPrice " Text = ' <%#Eval("price")%> ' runat = " server " Width = " 90px " />
</ EditItemTemplate >
< ItemStyleWidth = " 100px " />
</ asp:TemplateField >
< asp:BoundFieldHeaderText = " 建立时间 " DataField = " createdate " ReadOnly = " True " />
< asp:CommandFieldShowDeleteButton = " True " ShowEditButton = " True " HeaderText = " 操作 " />
</ Columns >
< PagerSettingsFirstPageText = "" LastPageText = "" NextPageText = "" PreviousPageText = "" />
< RowStyleHeight = " 20px " BackColor = " #F7F6F3 " ForeColor = " #333333 " />
< FooterStyleBackColor = " #5D7B9D " Font - Bold = " True " ForeColor = " White " />
< EditRowStyleBackColor = " #999999 " />
< SelectedRowStyleBackColor = " #E2DED6 " Font - Bold = " True " ForeColor = " #333333 " />
< PagerStyleBackColor = " #284775 " ForeColor = " White " HorizontalAlign = " Center " />
< HeaderStyleBackColor = " #5D7B9D " Font - Bold = " True " ForeColor = " White " />
< AlternatingRowStyleBackColor = " White " ForeColor = " #284775 " />
</ asp:GridView >
</ td >
</ tr >
</ table >
</ div >
</ form >
</ body >
</ html >
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 gridview_GridViewUp:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgse)
{
if ( ! IsPostBack)
{
GridViewBind();
}
}
protected void GridView1_PageIndexChanging( object sender,GridViewPageEventArgse)
{
GridView1.PageIndex = e.NewPageIndex;
GridViewBind();
}
private void GridViewBind()
{
string connStr = ConfigurationManager.ConnectionStrings[ " ConnString " ].ConnectionString;
string SqlStr = " SELECT*,casexueliwhen'1'then'博士'when'2'then'硕士'when'3'then'学士'else''endasxueliTextFROMtest01whereid<1000andid>200 " ;
DataSetds = new DataSet();
try
{
SqlConnectionconn = new SqlConnection(connStr);
if (conn.State.ToString() == " Closed " )conn.Open();
SqlDataAdapterda = new SqlDataAdapter(SqlStr,conn);
da.Fill(ds, " test01 " );
if (conn.State.ToString() == " Open " )conn.Close();
GridView1.DataSource = ds.Tables[ 0 ].DefaultView;
GridView1.DataBind();
}
catch (Exceptionex)
{
Response.Write( " 数据库错误,错误原因: " + ex.Message);
Response.End();
}
}
protected void GridView1_RowDataBound( object sender,GridViewRowEventArgse)
{
if (((DropDownList)e.Row.FindControl( " DDLXueli " )) != null )
{
DropDownListddlxueli = (DropDownList)e.Row.FindControl( " DDLXueli " );
// 生成DropDownList的值,也可以取得数据库中的数据绑定
ddlxueli.Items.Clear();
ddlxueli.Items.Add( new ListItem( " 博士 " , " 1 " ));
ddlxueli.Items.Add( new ListItem( " 硕士 " , " 2 " ));
ddlxueli.Items.Add( new ListItem( " 学士 " , " 3 " ));
//
// 选中DropDownList
ddlxueli.SelectedValue = ((HiddenField)e.Row.FindControl( " HDFXueli " )).Value;
//
}
}
protected void GridView1_RowEditing( object sender,GridViewEditEventArgse)
{
GridView1.EditIndex = e.NewEditIndex;
GridViewBind();
}
protected void GridView1_RowCancelingEdit( object sender,GridViewCancelEditEventArgse)
{
GridView1.EditIndex = - 1 ;
GridViewBind();
}
protected void GridView1_RowUpdating( object sender,GridViewUpdateEventArgse)
{
string id = GridView1.DataKeys[e.RowIndex].Values[ 0 ].ToString();
string card = ((TextBox)GridView1.Rows[e.RowIndex].FindControl( " TBCard " )).Text;
string xueli = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl( " DDLXueli " )).SelectedValue;
string price = ((TextBox)GridView1.Rows[e.RowIndex].FindControl( " TBPrice " )).Text;
string connStr = ConfigurationManager.ConnectionStrings[ " ConnString " ].ConnectionString;
string SqlStr = " updatetest01setcard=' " + card + " ',xueli=' " + xueli + " ',price=' " + price + " 'whereid= " + id;
try
http://images.csdn.net/syntaxhighlighting/OutliningIndic
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 gridview_GridViewUp:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgse)
{
if ( ! IsPostBack)
{
GridViewBind();
}
}
protected void GridView1_PageIndexChanging( object sender,GridViewPageEventArgse)
{
GridView1.PageIndex = e.NewPageIndex;
GridViewBind();
}
private void GridViewBind()
{
string connStr = ConfigurationManager.ConnectionStrings[ " ConnString " ].ConnectionString;
string SqlStr = " SELECT*,casexueliwhen'1'then'博士'when'2'then'硕士'when'3'then'学士'else''endasxueliTextFROMtest01whereid<1000andid>200 " ;
DataSetds = new DataSet();
try
{
SqlConnectionconn = new SqlConnection(connStr);
if (conn.State.ToString() == " Closed " )conn.Open();
SqlDataAdapterda = new SqlDataAdapter(SqlStr,conn);
da.Fill(ds, " test01 " );
if (conn.State.ToString() == " Open " )conn.Close();
GridView1.DataSource = ds.Tables[ 0 ].DefaultView;
GridView1.DataBind();
}
catch (Exceptionex)
{
Response.Write( " 数据库错误,错误原因: " + ex.Message);
Response.End();
}
}
protected void GridView1_RowDataBound( object sender,GridViewRowEventArgse)
{
if (((DropDownList)e.Row.FindControl( " DDLXueli " )) != null )
{
DropDownListddlxueli = (DropDownList)e.Row.FindControl( " DDLXueli " );
// 生成DropDownList的值,也可以取得数据库中的数据绑定
ddlxueli.Items.Clear();
ddlxueli.Items.Add( new ListItem( " 博士 " , " 1 " ));
ddlxueli.Items.Add( new ListItem( " 硕士 " , " 2 " ));
ddlxueli.Items.Add( new ListItem( " 学士 " , " 3 " ));
//
// 选中DropDownList
ddlxueli.SelectedValue = ((HiddenField)e.Row.FindControl( " HDFXueli " )).Value;
//
}
}
protected void GridView1_RowEditing( object sender,GridViewEditEventArgse)
{
GridView1.EditIndex = e.NewEditIndex;
GridViewBind();
}
protected void GridView1_RowCancelingEdit( object sender,GridViewCancelEditEventArgse)
{
GridView1.EditIndex = - 1 ;
GridViewBind();
}
protected void GridView1_RowUpdating( object sender,GridViewUpdateEventArgse)
{
string id = GridView1.DataKeys[e.RowIndex].Values[ 0 ].ToString();
string card = ((TextBox)GridView1.Rows[e.RowIndex].FindControl( " TBCard " )).Text;
string xueli = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl( " DDLXueli " )).SelectedValue;
string price = ((TextBox)GridView1.Rows[e.RowIndex].FindControl( " TBPrice " )).Text;
string connStr = ConfigurationManager.ConnectionStrings[ " ConnString " ].ConnectionString;
string SqlStr = " updatetest01setcard=' " + card + " ',xueli=' " + xueli + " ',price=' " + price + " 'whereid= " + id;
try
http://images.csdn.net/syntaxhighlighting/OutliningIndic
发表评论
评论