GvidView中添加HyperLink控件打開新頁,但HyperLink不支援JS,可以通過以下的方法讓其支持.
1.在頁面中添加如下JS語句:
- <script type="text/javascript">
- function ShowWindow(URLStr, popWinName, left, top, width, height) {
- window.open(URLStr, popWinName, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width=' + width + ',height=' + height + ',left=' + left + ', top=' + top + ',screenX=' + left + ',screenY=' + top + '');
- }
- function showMyModalDialog(url, width, height){
- showModalDialog(url, '','dialogWidth:' + width + 'px;dialogHeight:' + height + 'px;center:yes;status:no;scroll:no;help:no');
- }
- function showMyModelessDialog(url, width, height){
- showModelessDialog(url, '','dialogWidth:' + width + 'px;dialogHeight:' + height + 'px;center:yes;status:no;scroll:no;help:no');
- }
- </script>
2.添加GridView1_RowDataBound事件:
- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
- {
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- e.Row.Cells[3].Attributes["onclick"] = "javascript:ShowWindow('hyperlink.aspx?name=" + e.Row.Cells[2].Text.ToString() + "','hyperlink',100,100,400,300);";
- string JS = "hyperlink.aspx?id=" + e.Row.Cells[0].Text.ToString() + "&type=" + e.Row.Cells[1].Text.ToString();
- e.Row.Cells[4].Text = "<a href=/"javascript:/" οnclick=/"javascript:ShowWindow('" + JS + "','添加',100,100,400,300);/">添加信息</a>";
- e.Row.Cells[5].Text = "<a href=/"javascript:/" οnclick=/"javascript:ShowWindow('hyperlink.aspx?id=" + e.Row.Cells[2].Text.ToString() + "','查看',100,100,400,300);/">查看明细</a>";
- }
- }
此方法為GridView中的HyperLink打開新窗體的好處:不會去刷新當前頁,直接打開新頁.