gridview 基本使用
来源:互联网 发布:数据库课程设计web源码 编辑:程序博客网 时间:2024/06/10 07:35
前台代码:
- <asp:GridView runat="server" ID="grdText" AutoGenerateColumns="False" OnRowCancelingEdit="grdText_RowCancelingEdit"
- OnRowDeleting="grdText_RowDeleting" OnRowEditing="grdText_RowEditing" OnRowUpdating="grdText_RowUpdating"
- OnRowDataBound="grdText_RowDataBound">
- <Columns>
- <asp:BoundField HeaderText="ID" DataField="Id" ReadOnly="true" />
- <asp:BoundField HeaderText="姓名" DataField="Name" ReadOnly="true"/>
- <asp:BoundField HeaderText="年龄" DataField="Age" ReadOnly="true"/>
- <asp:TemplateField HeaderText="学校">
- <EditItemTemplate>
- <asp:DropDownList runat="server" ID="dropSchool">
- </asp:DropDownList>
- </EditItemTemplate>
- <ItemTemplate>
- <%# Eval( "SchoolName" ) %>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField>
- <ItemTemplate>
- <asp:HiddenField runat="server" ID="hiddenSchoolId" Value=<%# Eval( "SchoolId" ) %> />
- </ItemTemplate>
- </asp:TemplateField>
- <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
- <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
- </Columns>
- </asp:GridView>
后台代码:
- DatebaseClass objDB = new DatebaseClass();
- protected void Page_Load(object sender, EventArgs e)
- {
- Response.Expires = 0;
- if( !IsPostBack )
- {
- this._BeginRun();
- }
- }
- private void _BeginRun()
- {
- DataTable dt = objDB.getStudent();
- if( dt != null )
- {
- this.grdText.DataSource = dt;
- this.grdText.DataKeyNames = new string []{ "Id" };
- this.grdText.DataBind();
- }
- }
- protected void grdText_RowDeleting( object sender, GridViewDeleteEventArgs e )
- {
- objDB.delete( Convert.ToInt32( grdText.DataKeys[ e.RowIndex ].Value ) );
- _BeginRun();
- }
- /// <summary>
- /// 行编辑
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void grdText_RowEditing( object sender, GridViewEditEventArgs e )
- {
- grdText.EditIndex = e.NewEditIndex;
- _BeginRun();
- }
- /// <summary>
- /// 取消行编辑
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void grdText_RowCancelingEdit( object sender, GridViewCancelEditEventArgs e )
- {
- grdText.EditIndex = -1;
- _BeginRun();
- }
- /// <summary>
- /// 行修改
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void grdText_RowUpdating( object sender, GridViewUpdateEventArgs e )
- {
- int schoolId = Convert.ToInt32( ( grdText.Rows[ e.RowIndex ].Cells[4].Controls[0] as TextBox ).Text.Trim() );
- objDB.update( Convert.ToInt32( grdText.DataKeys[ e.RowIndex ].Value ), schoolId );
- grdText.EditIndex = -1;
- _BeginRun();
- }
- /// <summary>
- /// 邦定学校
- /// </summary>
- /// <param name="drop"></param>
- protected void binderDropSchool( DropDownList drop )
- {
- drop.DataSource = objDB.getScholl();
- drop.DataTextField = "SchoolName";
- drop.DataValueField = "Id";
- drop.DataBind();
- }
- /// <summary>
- /// rowDataBound
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void grdText_RowDataBound( object sender, GridViewRowEventArgs e )
- {
- if( e.Row.RowType == DataControlRowType.DataRow )
- {
- int i = e.Row.Cells.Count -1 ;
- DropDownList drop = e.Row.FindControl( "dropSchool" ) as DropDownList;
- if( drop != null )
- {
- HiddenField hid = e.Row.FindControl( "hiddenSchoolId" ) as HiddenField;
- binderDropSchool( drop );
- if( hid != null )
- {
- drop.SelectedValue = hid.Value;
- }
- }
- e.Row.Attributes.Add( "onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E6F5FA'" );
- e.Row.Attributes.Add( "onmouseout", "this.style.backgroundColor=c" );
- if ( e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate )
- {
- e.Row.Cells[i].Attributes.Add( "onclick", "javascript:return confirm('你确认要删除!')");
- }
- }
- }
记录备用!(Data Source=127.0.0.1;Initial Catalog=llc2007;User ID=sa;Password=were)
- gridview 基本使用
- GridView的基本使用
- 2.7 GridView的基本使用
- Android RecyclerView 的基本使用之GridView
- GridView(网络视图)的基本使用
- GridView的基本操作
- gridview基本数据操作
- Gridview的基本用法
- GridView的基本使用方法
- GridView的基本应用
- GridView基本操作事件
- GridView 基本用法
- GridView的基本用法
- GRIDVIEW 使用
- Gridview使用
- GridView使用
- gridview 使用
- GridView 使用
- 每个IT人都应当拥有的30条技能
- 毛坯房验房的标准与内容
- O/R Mapping
- 第一章 创建一个GEF Editor插件
- strstr()函数
- gridview 基本使用
- 使用图形编辑框架创建基于 Eclipse 的应用程序
- C++类模板的三种特化
- Build glibc-2.6
- vs2005 用正则 替换模板列
- lighttpd 1.4.x不支持Expect: 100-continue
- char,varchar,nvarchar的区别?
- C语言编译过程
- 设置JAVA_HOME&Path&CLASSPATH