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是选择的哪一样。