当愚蠢的方式奏效的时候,它就不是愚蠢的。——《美国士兵手册》
 
作者前言:
在动态网页(我是指服务器脚本编程)纯手工年代,工作量是大,但控制起来非常精确,我称之为手术刀式编程。而今,在利润支配技术的年代,一夜之间“软件复用”“框架”“平台”“面向XX”雨后春笋……我并不反对城市扩张占用农田,也并不支持为了保护野生动物而放弃现代文明,我只是不想发生这样一幕:当一名程序员苦于实现一个功能的时候,怪罪于微软没有提供相应的控件,或者归咎于网上没有人贴出源代码。要知道,世界上最贵的奢侈品大多是纯手工制作。
 
这里是一个页面的codebehind代码,献丑了:
 
在线编辑.aspx.cs
 
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;
public partial class GridView_在线编辑 : System.Web.UI.Page
{
    ///
    /// 构建数据源
    /// 请根据自己的情况修改
    ///

    ///
    private DataTable InitTable()
    {
        DataTable dt = new DataTable("testtable");
        DataColumn dc = new DataColumn("ID", typeof(String));
        dt.Columns.Add(dc);
        dc = new DataColumn("Sex", typeof(String));
        dt.Columns.Add(dc);
        DataRow dr = dt.NewRow();
        dr["ID"] = "001";
        dr["Sex"] = "0";
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["ID"] = "002";
        dr["Sex"] = "0";
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["ID"] = "003";
        dr["Sex"] = "0";
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["ID"] = "004";
        dr["Sex"] = "0";
        dt.Rows.Add(dr);
        return dt;
    }
    ///
    /// 数据绑定
    ///

    private void BindDataa()
    {
        GridView1.DataSource = InitTable();
        GridView1.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            GridView1.DataSource = InitTable();
            GridView1.DataBind();
        }
    }
    ///
    /// 编辑按钮按下时
    ///

    ///
    ///
    protected void btEdit_Click(object sender, EventArgs e)
    {
        Button rowbutton = sender as Button;
        GridView1.EditIndex = Convert.ToInt32(rowbutton.CommandArgument);
        BindDataa();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowIndex != GridView1.EditIndex)
            {
               
                Button edit = e.Row.FindControl("btEdit") as Button;
                edit.CommandArgument = e.Row.RowIndex.ToString();
            }
            else
            {
                DataRowView drv = e.Row.DataItem as DataRowView;
                RadioButtonList sex = e.Row.FindControl("RadioButtonList1") as RadioButtonList;
                sex.SelectedValue = drv["Sex"].ToString();
            }
        }
    }
    ///
    /// 取消按钮按下时
    ///

    ///
    ///
    protected void btCancel_Click(object sender, EventArgs e)
    {
        GridView1.EditIndex = -1;
        BindDataa();
    }
    ///
    /// 确定按钮按下时
    ///

    ///
    ///
    protected void btOK_Click(object sender, EventArgs e)
    {
        //主键获取,GridView普通用法,在此不赘述
        string key = GridView1.DataKeys[GridView1.EditIndex].Value.ToString();
        //获取用户输入
        GridViewRow gvr = GridView1.Rows[GridView1.EditIndex];
        TextBox userID = gvr.FindControl("tbID") as TextBox;
        string id = userID.Text;
        RadioButtonList userSex = gvr.FindControl("RadioButtonList1") as RadioButtonList;
        string sex = userSex.SelectedValue;
        //====在这里做你想要做的事情,比如更新数据库什么的======
        //恢复到浏览状态
        GridView1.EditIndex = -1;
        BindDataa();
    }
}
 
在线编辑.aspx
 
   

   

       
           
               
                   
                       
                       
                   

                   
                       
                   

               

               
                   
                       
                   

                   
                       
                   

               

               
                   
                         
                       
                            Boy
                            Girl
                       

                   

                   
                       
                   

               

           

       

   
   

       
   
 
(完)