心静自然凉~

一杯清茶、二盏淡酒、邀三五知己,诉七八句心语,道九分珍重,怀十分真诚,或伤感、或快乐,无现实之隔阂 、无世俗之势利,如此人生,不亦乐乎!

导航

GridView 通过javacript 控制选择

Posted on 2008-06-30 16:18  Leo.Zhu  阅读(520)  评论(0编辑  收藏  举报

GridView通过javascript控制选择,不往返于Client和Server之间。

code如下:

javascript:


var lastRowSelected;
var Colorback;
function GridView_selectRow(row, ID)
{
document.getElementById("HidID").value=ID;
if (lastRowSelected != row)
{
if (lastRowSelected != null)
{
lastRowSelected.style.backgroundColor = Colorback;
lastRowSelected.style.color = 'Black'
lastRowSelected.style.fontWeight = 'normal';
}
Colorback = row.style.backgroundColor
row.style.backgroundColor = '#DDEEFF'
row.style.color = 'Black'
row.style.fontWeight = 'normal'
lastRowSelected = row;
}
}
function GridView_mouseHover(row)
{
row.style.cursor = 'hand';
}
c#:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.ID = e.Row.Cells[0].Text;

HiddenField hf = (HiddenField)e.Row.FindControl("hidTel");
if (hf.Value != "")
{
e.Row.Attributes.Add("onclick", "GridView_selectRow(this,'" + hf.Value + "')");
e.Row.Attributes.Add("onmouseover", "GridView_mouseHover(this)");
}
//e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#DDEEFF';this.style.cursor= 'hand'");
//e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='white';this.style.cursor= 'defualt'");

//e.Row.Attributes.Add("onclick", "javascript:__doPostBack('" + GridView1.ID + "' , 'Select$" + e.Row.RowIndex + "');");
}


注释:

document.getElementById("HidID").value=ID;

在页面中添加一个hiddenField的栏位,用来标记当前选择的ID。这样后台可以通过获取这个控件的value来得知当前的GridView是选择的哪一样。