cs
-----------------------------------------------------
public class mupgrid : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button edit;
protected System.Web.UI.WebControls.Button update;
protected System.Web.UI.WebControls.DataGrid mygrid;
protected System.Web.UI.WebControls.Button del;
private SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=w;database=northwind;");
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string jsScript = "<script language=JavaScript> \n" +
"<!--\n" +
"function confirmDelete (frm) {\n\n" +
" // loop through all elements\n" +
" for (i=0; i<frm.length; i++) {\n\n" +
" // Look for our checkboxes only\n" +
" if (frm.elements[i].name.indexOf ('chkbox') !=-1) {\n" +
" // If any are checked then confirm alert, otherwise nothing happens\n" +
" if(frm.elements[i].checked) {\n" +
" return confirm ('确认要更新记录吗?')\n" +
" }\n" +
" }\n" +
" }\n" +
"}\n\n\n" + "function select_deselectAll (chkVal, idVal) {\n" +
"var frm = document.forms[0];\n" +
"// loop through all elements\n" +
" for (i=0; i<frm.length; i++) {\n" +
" // // Look for our Header Template's Checkbox\n" +
" if (idVal.indexOf ('chekall') != -1) {\n" +
" // Check if main checkbox is checked, then select or deselect datagrid checkboxes \n" +
" if(chkVal == true) {\n" +
" frm.elements[i].checked = true;\n" +
" } else {\n" +
" frm.elements[i].checked = false;\n" +
" }\n" +
" // Work here with the Item Template's multiple checkboxes\n" +
" } else if (idVal.indexOf('chkbox') != -1) {\n" +
" // Check if any of the checkboxes are not checked, and then uncheck top select all checkbox\n" +
" if(frm.elements[i].checked == false) {\n" +
" frm.elements[1].checked = false; // Check if any of the checkboxes are not checked, and then uncheck top select all checkbox\n" +
" }\n" +
" }\n" +
" }\n" +
"}" +
"//--> \n" +
"</script>";
RegisterClientScriptBlock("clientScript",jsScript);
WebControl edit = (WebControl)Page.FindControl("update");
edit.Attributes.Add("onclick","return confirmDelete(this.form);");

if(!IsPostBack)
{
setbind();
}
}
private void setbind()
{
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select employeeid,firstname,lastname from employees",conn);
DataSet ds = new DataSet();
sda.Fill(ds);
mygrid.DataSource = ds.Tables[0].DefaultView;
mygrid.DataBind();
ds.Clear();
sda.Dispose();
conn.Close();

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

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.edit.Click += new System.EventHandler(this.edit_Click);
this.update.Click += new System.EventHandler(this.update_Click);
this.del.Click += new System.EventHandler(this.del_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void edit_Click(object sender, System.EventArgs e)
{
foreach(DataGridItem i in mygrid.Items)
{
if(((CheckBox)i.FindControl("chkbox")).Checked == true)
{
((TextBox)i.FindControl("txt_firname")).Visible = true;
((TextBox)i.FindControl("txt_lastname")).Visible = true;
((Label)i.FindControl("lab_firname")).Visible = false;
((Label)i.FindControl("lab_lastname")).Visible = false;

}
}
} private void update_Click(object sender, System.EventArgs e)
{ foreach(DataGridItem i in mygrid.Items)
{
if(((CheckBox)i.FindControl("chkbox")).Checked == true)
{
if(((TextBox)i.FindControl("txt_firname")).Text != ((Label)i.FindControl("lab_firname")).Text || ((TextBox)i.FindControl("txt_lastname")).Text != ((Label)i.FindControl("lab_lastname")).Text)
{
try
{
conn.Open();
string sql = "update employees set firstname='"+(((TextBox)i.FindControl("txt_firname")).Text).ToString()+"',lastname='"+(((TextBox)i.FindControl("txt_lastname")).Text).ToString()+"' where employeeid = " + i.Cells[1].Text.ToString();
SqlCommand cmd = new SqlCommand(sql,conn);
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
Response.Write(ex);
}
finally
{
conn.Close();

}
//Response.Write("记录号:"+ this.mygrid.DataKeys[int.Parse(((HtmlInputHidden)i.FindControl("selectid")).Value)] + "FirstName:" + ((TextBox)i.FindControl("txt_firname")).Text + "LastName:" + ((TextBox)i.FindControl("txt_lastname")).Text + "<br>");

}
((TextBox)i.FindControl("txt_firname")).Visible = false;
((TextBox)i.FindControl("txt_lastname")).Visible = false;
((Label)i.FindControl("lab_firname")).Visible = true;
((Label)i.FindControl("lab_lastname")).Visible = true;
((CheckBox)i.FindControl("chkbox")).Checked = false;
}
}
setbind();
} private void del_Click(object sender, System.EventArgs e)
{
string ids = "";
bool flag = false;
foreach(DataGridItem i in mygrid.Items)
{
if(((CheckBox)i.FindControl("chkbox")).Checked)
{
flag = true;
ids += i.Cells[1].Text.ToString() + ",";
}
}
if(flag == true)
{
string sql = "delete from employees where employeeid in(" + ids.Substring(0,ids.LastIndexOf(",")) +")";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql,conn);
cmd.ExecuteNonQuery();

}
catch(Exception ex)
{
Response.Write(ex);
}
finally
{
conn.Close();
setbind();
}

}
}
}