在网上找到过一些对于DataGrid控件的双击事件的方法,有些是需要刷新页面的,有些则太过于复杂,东搞西搞一头雾水.难啦!还是自己动手做啦,根据需求,需将DataGrid双击后将双击的行内的内容提取出来写入对于的TEXTBOX框内,当然前提是不能刷新页面,因为刷新时又跳出开头了.参考了一下网上大虾的公布的东东,自己修改了一下,现公布出来给大家,资源共享啦:)
首先,在DataGrid的ItemDataBound事件里对每个项添加客户端双击事件:
e.Item.Attributes.Add(
"
ondblclick
"
,
"
javascript:datagrid1_dbclick('
"
+
e.Item.Cells[
1
].Text.Trim()
+
"
','
"
+
e.Item.Cells[
2
].Text.Trim()
+
"
','
"
+
e.Item.Cells[
3
].Text.Trim()
+
"
','
"
+
e.Item.Cells[
4
].Text.Trim()
+
"
')
"
);
后面的参数为DataGrid行内的各项内容;
如下:
private
void
DataGrid1_ItemDataBound(
object
sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
e.Item.Cells[ 5 ].Attributes.Add( " onclick " , " return confirm('确定删除记录?'); " );
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item )
{
e.Item.Attributes.Add( " onmouseover " , " this.oldcolor=this.style.backgroundColor;this.style.backgroundColor='#FFFF00' " );
e.Item.Attributes.Add( " onmouseout " , " this.style.backgroundColor=this.oldcolor " );
e.Item.Attributes.Add( " ondblclick " , " javascript:datagrid1_dbclick(' " + e.Item.Cells[ 1 ].Text.Trim() + " ',' " + e.Item.Cells[ 2 ].Text.Trim() + " ',' " + e.Item.Cells[ 3 ].Text.Trim() + " ',' " + e.Item.Cells[ 4 ].Text.Trim() + " ') " );
}
}
{
e.Item.Cells[ 5 ].Attributes.Add( " onclick " , " return confirm('确定删除记录?'); " );
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item )
{
e.Item.Attributes.Add( " onmouseover " , " this.oldcolor=this.style.backgroundColor;this.style.backgroundColor='#FFFF00' " );
e.Item.Attributes.Add( " onmouseout " , " this.style.backgroundColor=this.oldcolor " );
e.Item.Attributes.Add( " ondblclick " , " javascript:datagrid1_dbclick(' " + e.Item.Cells[ 1 ].Text.Trim() + " ',' " + e.Item.Cells[ 2 ].Text.Trim() + " ',' " + e.Item.Cells[ 3 ].Text.Trim() + " ',' " + e.Item.Cells[ 4 ].Text.Trim() + " ') " );
}
}
完成以上后,则将DataGrid的双击事件定义了,然后再加上客户端脚本程序段,让双击事件的函数能够用起来:
function
datagrid1_dbclick(PoliceID,PoliceName,PoliceMobie,Memo)
{ window.document.forms[ 0 ].tblPoliceID.value = PoliceID;
window.document.forms[ 0 ].Police1.value = PoliceID;
window.document.forms[ 0 ].tblPoliceName.value = PoliceName;
window.document.forms[ 0 ].tblPoliceMobie.value = PoliceMobie;
if (Memo != " " )
window.document.forms[ 0 ].tblMemo.value = Memo;
else window.document.forms[ 0 ].tblMemo.value = "" ;
}
{ window.document.forms[ 0 ].tblPoliceID.value = PoliceID;
window.document.forms[ 0 ].Police1.value = PoliceID;
window.document.forms[ 0 ].tblPoliceName.value = PoliceName;
window.document.forms[ 0 ].tblPoliceMobie.value = PoliceMobie;
if (Memo != " " )
window.document.forms[ 0 ].tblMemo.value = Memo;
else window.document.forms[ 0 ].tblMemo.value = "" ;
}
以上是客户端脚本程序,将DataGrid传过来的参数,分别赋值给界面上的四个TXETBOX对像,这样就不用运行
服务端程序,也就没有刷新页面的烦恼了,呵呵,是不是很简单?希望能给你带来帮助.