Add the javascript code in the head as follows:
<script lang=javascript>
function sel(i) // Execute after moving the mouse up
{
eval(i+".style.background='#CCCC66'"); // Change the color of the row
eval(i+".style.cursor='hand'"); // When the mouse is moved up, it changes to a hand shape
}
function unsel(i) // Executed after the mouse leaves
{
eval(i+".style.background=''");
}
function clicktr(i)
{
eval(i+".style.background=''");
window.open("Edit.aspx?param="+i,"Modify","height=490,width=710,resizable=no,scrollbars=no,status=no,toolbar=no,
menubar=no,location=no,left=50,top=50");
}
</script>
In the DataGrid's ItemDataBound (occurs when data is bound) event:
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType != ListItemType.Header)
{
string ID = e.Item.Cells[0].Text; // The first column here is the ID value in data binding (for the convenience of passing parameters in the modified page, if there are multiple parameters, you can also add them as needed!)
e.Item.Attributes.Add("id",ID);
e.Item.Attributes.Add("onmouseover","sel(" + ID+ ")");
e.Item.Attributes.Add("onmouseout", "unsel(" + ID+ ")");
e.Item.Attributes.Add("onclick", "clicktr(" + ID+")");
}
}
//**************************** Finish******************* ***************************//
However, there are inconveniences in the above approach. If you add a template column to the DataGrid, it can be used to provide users with Provide selection operations (such as deleting selections),
At this time, using the above method will cause a new window to pop up every time you select the CheckBox (the onclick event is triggered).
A poor solution:
change the original row-based Attributes to column-based. Except for the template column, all Add attributes to all columns.
For example, if the template is listed in column 6, you can modify the cs file like this
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType != ListItemType.Header)
{
string bm = e.Item.Cells[0].Text;
for(int i=0;i<5;i++)
{
e.Item.Cells[i].Attributes.Add("id","a"+i.ToString()+bm);
e.Item.Cells[i].Attributes.Add("onmouseover","sel(" +i.ToString()+","+ bm + ")");
e.Item.Cells[i].Attributes.Add("onmouseout", "unsel(" +i.ToString()+","+ bm + ")");
e.Item.Cells[i].Attributes.Add("onclick", "clicktr(" + bm +")");
}
}
}
In javascript code:
function sel(i,ID)
{
for(var j=0;j<5;j++)
{ eval("a"+j.toString()+ID+".style.background='#CCCC66'"); eval("a"+j.toString()+ID+".style.cursor='hand'" );
}
}
function unsel(i,ID)
{
for(var j=0;j<5;j++)
{ eval("a"+j.toString()+ID+".style.background=''");
}
}
function clicktr(i)
{
for(var j=0;j<5;j++)
{
eval("a"+j.toString()+i+".style.background=''");
window.open("Edit.aspx?param="+i,"Modify","height=490,width=710,resizable=no,scrollbars=no,status=no,toolbar=no,
menubar=no,location=no,left=50,top=50");
}
}