原理:
在我们点击GridView默认生成的选择,编辑,删除按钮时,会产生postback参数.可以查看源代码:
<td> <a href="javascript:__doPostBack('ctl00$cp$GvDeparments','Edit$0')">编辑</a> <a href="javascript:__doPostBack('ctl00$cp$GvDeparments','Delete$0')">删除</a> <a href="javascript:__doPostBack('ctl00$cp$GvDeparments','Select$0')">选择</a> </td> <td> <a href="javascript:__doPostBack('ctl00$cp$GvDeparments','Edit$1')">编辑</a> <a href="javascript:__doPostBack('ctl00$cp$GvDeparments','Delete$1')">删除</a> <a href="javascript:__doPostBack('ctl00$cp$GvDeparments','Select$1')">选择</a> </td>
说明:
function __doPostBack(eventTarget, eventArgument) { if (!theForm.onsubmit || (theForm.onsubmit() != false)) { theForm.__EVENTTARGET.value = eventTarget; theForm.__EVENTARGUMENT.value = eventArgument; theForm.submit(); } }
此为asp.net内置函数.
我们可以使用Edit$,Delete$,Select$参数来做提交.具体如下:以下代码都是在GridView的RowDataBound事件中写的.
代码 if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes["onclick"] = "javascript:__doPostBack('" + GvDeparments.ClientID.Replace('_', '$') + "','Select$" + e.Row.RowIndex + "')"; }源文件 <tr onclick="javascript:__doPostBack('ctl00$cp$GvDeparments','Select$3')"> ... </tr>
代码 if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GvDeparments, "Select$" + e.Row.RowIndex); }源文件 <tr onclick="javascript:__doPostBack('ctl00$cp$GvDeparments','Select$3')"> ... </tr>
代码 if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes["onclick"] = ClientScript.GetPostBackEventReference(this.GvDeparments, "Select$" + e.Row.RowIndex); }源文件 <tr onclick="__doPostBack('ctl00$cp$GvDeparments','Select$3')"> ... </tr>
注意查看源代码的onclick事件.