gridview and repeater
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Button btndelete = (Button)e.Row.FindControl("btndelete");
//if (btndelete != null)
//{
// //btndelete.Attributes.Add("onclick", "btndoubleclick_Click()");
// //btndelete.Style["display"] = "none";
//}
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
int k = e.Row.RowIndex % 2;
//e.Row.Attributes.Add("onclick", "window.open('Default.aspx');");
e.Row.Cells[1].Attributes.Add("onclick", "window.open('Default.aspx');");
e.Row.Cells[2].Attributes.Add("onclick", "window.open('Default.aspx');");
e.Row.Attributes.Add("onmouseover", "this.style.textDecoration='underline';this.style.cursor='pointer';this.style.backgroundColor='#00A9FF';");
e.Row.Attributes.Add("onmouseout", "this.style.textDecoration='none';if(" + k + "==0){this.style.backgroundColor='#528B8B';}else{this.style.backgroundColor='#FFFFFF';}");
switch (drv.Row.ItemArray[1].ToString())
{
case "zhuo":
e.Row.Cells[1].Text = "陈卓勇";
break;
case "xsgl":
e.Row.Cells[1].Text = "学生管理";
break;
}
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "shanchu")
{
//int id = Convert.ToInt32(GridView1.Rows[drv.RowIndex].Cells["Id"].Text);
int id = Convert.ToInt32(e.CommandArgument.ToString());
DBHelper.ExecuteSql("delete from tbWorker where id="+id+"");
BindGridView();
}
}
protected void btnshanchu_Click(object sender, EventArgs e)
{
bool Choose = false; //用于标记GridView中任意CheckBox控件是否被选中
bool Wrong = false; //用于标记删除GridView中某一行的数据时是否失败
string msg = ""; //用于记录当删除失败时存储的错误信息
//遍历GridView所有行
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//如果某一行的CheckBox被选中,则执行删除操作
CheckBox chk = (CheckBox)(GridView1.Rows[i].Cells[0].FindControl("chkItem"));
if (chk.Checked == true)
{
Choose = true;
//获取要被删除的记录id
int id = Convert.ToInt32(GridView1.DataKeys[i].Value);
if (Rowdelete("delete from tbWorker where id="+id+"") == false) //请自行更改
{
Wrong = true;
msg = msg + "第" + (i + 1) + "行删除失败!";
continue;
}
}
}
//校验
if (Choose == false)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('没有任何选项被选中,无法删除!');</script>");
return;
}
if (Wrong == true)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('" + msg + "请联系管理员');</script>");
return;
}
BindGridView();
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView drv = e.Item.DataItem as DataRowView;
switch (drv.Row.ItemArray[1].ToString())
{
case "zhuo":
drv[1] = "陈卓勇";
break;
case "xsgl":
drv[1] = "学生管理";
break;
}
}
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "shanchu")
{
if (!Rowdelete("delete from tbWorker where id=" + e.CommandArgument.ToString() + ""))
{
Response.Write("<script>alert('删除失败')</script>");
}
BindRepeater();
}
}