HTML:
<script type="text/javascript">
var oldrow=null;
var currentcolor=null;
var oldcolor=null;
function selectx(row)
{
if(oldrow!=null)
{
oldrow.style.backgroundColor=oldcolor;
}
row.style.backgroundColor='#99ccff';
oldrow=row;
oldcolor = currentcolor;
}
</script> .cs:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{ if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "if(this!=oldrow){currentcolor=this.style.backgroundColor;this.style.backgroundColor='PeachPuff',this.style.fontWeight='';}");
e.Row.Attributes.Add("onmouseout", "if(this!=oldrow){this.style.backgroundColor=currentcolor;this.style.fontWeight='';}");
e.Row.Attributes.Add("onclick", e.Row.ClientID.ToString() + ".checked=true;selectx(this)");
}
}用了这种方法可以单击后改变该列的颜色。另外我还由一个delete按钮。此时如果我单击,提示是否是否删除。然后在后台连接数据库,删除制定的行,请问如何在后台。单击delete按钮时。获得选中行的某个字段值呢,比如id。 谢谢如何改这段代码才行呢?
<script type="text/javascript">
var oldrow=null;
var currentcolor=null;
var oldcolor=null;
function selectx(row)
{
if(oldrow!=null)
{
oldrow.style.backgroundColor=oldcolor;
}
row.style.backgroundColor='#99ccff';
oldrow=row;
oldcolor = currentcolor;
}
</script> .cs:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{ if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "if(this!=oldrow){currentcolor=this.style.backgroundColor;this.style.backgroundColor='PeachPuff',this.style.fontWeight='';}");
e.Row.Attributes.Add("onmouseout", "if(this!=oldrow){this.style.backgroundColor=currentcolor;this.style.fontWeight='';}");
e.Row.Attributes.Add("onclick", e.Row.ClientID.ToString() + ".checked=true;selectx(this)");
}
}用了这种方法可以单击后改变该列的颜色。另外我还由一个delete按钮。此时如果我单击,提示是否是否删除。然后在后台连接数据库,删除制定的行,请问如何在后台。单击delete按钮时。获得选中行的某个字段值呢,比如id。 谢谢如何改这段代码才行呢?
再用二楼的来绑定。
绑定方法要灵活,并不是说二楼的一定能找得到ID,得根据具体情况而定。
{
GridViewRow dr = e.Row;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((ImageButton)e.Row.Cells[0].Controls[3]).Attributes.Add("onclick", "check_del('"+e.Row.Cells[1].Text +"')");//删除按钮加提示
}
} } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string wlbh = GridView1.DataKeys[e.RowIndex].Value.ToString() ; //表中主键 //check for it by used
bool is_exists=mater_product.wlbh_is_used(wlbh);
if (is_exists)
{
Page.RegisterStartupScript("Duplication","<script>alert('此物料已被后面调用,你的操作已被取消')</script>");
return;
}
else
{
mater_product.Delete(wlbh); bind();
}
}
CommandName="delete" OnClientClick="return confirm('确定要删除吗?删除后不可恢复');">删除</asp:LinkButton>protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
string id = e.CommandArgument.ToString();
string sql="delete Products where pid="+id+"";
int i= con.DeleteCommand(sql);
if (i > 0)
{
Response.Write("<script>alert('删除信息成功!'); </script> ");
Bind();
}
else
{
Response.Write("<script>alert('删除信息失败'); </script> ");
Bind();
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
string value;
value = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[dataGridView1.CurrentCell.ColumnIndex].Value.ToString().Trim();
MessageBox.Show(value, "结果"); } private void Form1_Load(object sender, EventArgs e)
{
string connstr = "Persist Security Info=false;Initial Catalog=PROJECT;Server=localhost;Integrated Security=SSPI";
string comdstr = "select * from users";
SqlConnection conn = new SqlConnection(connstr );
SqlCommand comd = new SqlCommand(comdstr ,conn);
SqlDataAdapter adapt = new SqlDataAdapter();
adapt.SelectCommand = comd;
adapt.SelectCommand.Connection = conn;
DataSet ds = new DataSet();
conn.Open();
adapt.Fill(ds,"users");
dataGridView1.DataSource = ds.Tables["users"];
dataGridView1.Refresh();
conn.Close();
}
.....
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="CB_Select" runat="server" />
</ItemTemplate>
</asp:TemplateField>
....
</Columns>
在girdview 外
<asp:Button ID="Btn_Delete" runat="server" Text="删除" OnClick="Btn_Delete_Click" /><br />
删除后台代码(给你参考): int selectCount = 0; //要删除的员工总数
string employeeNos = ""; //保存要删除的员工编号
string oneEmployeeNo; //保存某行记录的员工编号
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox chk = (CheckBox)gr.Cells[0].FindControl("CB_Select");
if (chk.Checked) //如果要删除该员工
{
oneEmployeeNo = gr.Cells[1].Text;
if (0 == selectCount)
employeeNos = "'" + oneEmployeeNo + "'";
else
employeeNos = employeeNos + ",'" + oneEmployeeNo + "'";
selectCount++; }
} if (0 == selectCount) //如果用户没有选择员工
Response.Write("<script>alert('对不起,你没有选择员工!');</script>");
else
{
/*如果选择了员工就执行调用业务层进行该些员工信息的删除*/
EmployeeLogic employeeLogic = new EmployeeLogic();
if (!employeeLogic.DeleteEmployeeInfo(employeeNos))
Response.Write("<script>alert('" + employeeLogic.ErrMessage + "');location.href='EmployeeManage.aspx';</script>");
else
Response.Write("<script>alert('员工信息删除成功!');location.href='EmployeeManage.aspx';</script>");
}
//获取当前选中行的索引
int i = this.GridView1.SelectedRow.RowIndex;
string key=this.GridView1.DataKeys[i].ToString();这样应该可以