前台代码:
- <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)