在GridView中使用超連結開啟明細資料
<asp:TemplateField HeaderText="客戶ID">
<ItemTemplate>
<asp:HyperLink id="detailLink" runat="server" Text='<%#Eval("ID") %>'> </asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
在RowDataBound事件中去組合超連結要傳遞的資料 和設定超連結的樣式
protected void gvCustomerData_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hl = new HyperLink();
hl = (HyperLink)e.Row.FindControl("detailLink");
string strID = hl.Text;
string strCUSTNAME = e.Row.Cells[2].Text;
string strCONTRID = e.Row.Cells[3].Text;
hl.Style.Add("text-decoration", "none");
hl.Style.Add("cursor", "hand");
string url = "SCR010_1.aspx?ID=" + strID + "&CUSTNAME=" + HttpUtility.UrlEncode(strCUSTNAME) ;
url += "&DATA_YM="+this.ddlYM.SelectedValue;
hl.Attributes.Add("onclick", "javascript:popupwindow('" + url + "','detailWin',700,480)");
e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#FFFFCC'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
}
}
呼叫popupwindow 主要當明細被開啟後 使用者可能忘記關閉 縮至下方 但當又開啟明細時 明細即使開啟後也是顯示在工作列 讓使用者以為沒開啟畫面 而使用popupwindow 會將縮至下方的明細視窗自動被叫起至螢幕最上方 並且顯示的是新點選的資料 而不是上一次縮至工作列前點選的明細資料
var winMap = new Object();
window.onunload = function() {
for (var propName in winMap) {
try {
winMap[propName].close();
} catch (e) {
}
}
}
function popupwindow(url, title, w, h) {
var winName = "detailWin";
var win;
win = winMap[winName];
try {
var left = (screen.width / 2) - (w / 2);
var top = (screen.height / 2) - (h / 2);
win = window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
win.focus();
} catch (e) {
winMap[winName] = win;
if (!win) {
//fail to open
}
else {
win.focus();
}
}
}