模態(tài)窗口返回值是個很好用的東西,給用戶體驗帶來了方便的同時,也可能為程序的數(shù)據(jù)格式規(guī)范帶來便利。
--------------------------------------父頁面--------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ClickTxt.aspx.cs" Inherits="ClickTxt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www./1999/xhtml" >
<head runat="server">
<title>無標題頁</title>
<script language="javascript" type="text/javascript">
function OpenWin(width,height)
{
var me;
// 把父頁面窗口對象當作參數(shù)傳遞到對話框中,以便對話框操縱父頁自動刷新。
me = window;
// 顯示對話框。
var a = window.showModalDialog("Pager.aspx",me,'dialogWidth='+width +'px;dialogHeight='+height+'px;help:no;status:no')
if(typeof(a) != "undefined")
//返回值賦給TextBox
window.document.getElementById("TextBox1").value = a;
//alert(a);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></div>
</form>
</body>
</html>
public partial class ClickTxt : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Attributes.Add("onclick", "javascript:OpenWin(540,400)");
}
}
----------------------------------------模態(tài)窗口------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Pager.aspx.cs" Inherits="Pager" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www./1999/xhtml">
<head runat="server">
<title>無標題頁</title>
<!-- 防止重新打開頁面形式的對話框 -->
<base target="_self" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataKeyNames="CSGPBH" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" OnDataBound="GridView1_DataBound" OnPageIndexChanging="GridView1_PageIndexChanging" >
<PagerTemplate>
<table width="800px" style="border: 0px; border-style: ridge;" align="center">
<tr>
<td style="border-bottom-style: ridge; width: 100%; text-align: center">
<asp:Label ID="lblCurrrentPage" runat="server" ForeColor="#CC3300"></asp:Label>
<span>跳轉(zhuǎn)至</span>
<asp:DropDownList ID="page_DropDownList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="page_DropDownList_SelectedIndexChanged">
</asp:DropDownList>
<span>頁</span>
<asp:LinkButton ID="lnkBtnFirst" CommandArgument="First" CommandName="page" runat="server">第一頁</asp:LinkButton>
<asp:LinkButton ID="lnkBtnPrev" CommandArgument="prev" CommandName="page" runat="server">上一頁</asp:LinkButton>
<asp:LinkButton ID="lnkBtnNext" CommandArgument="Next" CommandName="page" runat="server">下一頁</asp:LinkButton>
<asp:LinkButton ID="lnkBtnLast" CommandArgument="Last" CommandName="page" runat="server">最后一頁</asp:LinkButton>
</td>
</tr>
</table>
</PagerTemplate>
<Columns>
<asp:BoundField DataField="CSGPBH" HeaderText="掛牌編號" SortExpression="CSGPBH" />
<asp:BoundField DataField="FWBH" HeaderText="房屋編號" ReadOnly="True" SortExpression="FWBH" />
<asp:BoundField DataField="CQZH" HeaderText="產(chǎn)權(quán)證號" SortExpression="CQZH" />
<asp:BoundField DataField="QLR" HeaderText="權(quán)利人" SortExpression="QLR" />
<asp:BoundField DataField="QLRZJHM" HeaderText="證件號碼" SortExpression="QLRZJHM" />
<asp:BoundField DataField="FBZL" HeaderText="房屋坐落" SortExpression="FBZL" />
<asp:BoundField DataField="MC" HeaderText="掛牌幣種" SortExpression="GPBZ" />
<asp:BoundField DataField="GPJG" HeaderText="掛牌價格" SortExpression="GPJG" />
<asp:BoundField DataField="GPZT" HeaderText="掛牌狀態(tài)" SortExpression="GPZT" />
<asp:BoundField DataField="DJHM" HeaderText="登記號碼" SortExpression="DJHM" />
</Columns>
</asp:GridView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:clf %>"
SelectCommand="SELECT csgp.[CSGPBH], csgp.[FWBH], csgp.[FBZL], [ZD_A063].[MC], csgp.[GPJG], [ZD_A110].[MC] as GPZT, csgp.[QLR], csgp.[QLRZJHM], csgp.[CQZH], csgp.[DJHM] FROM [YW_CLF_CSGP] csgp JOIN [ZD_A063] ON csgp.[GPBZ] = [ZD_A063].[DM] JOIN [ZD_A110] ON csgp.[GPZT] = [ZD_A110].[DM]"></asp:SqlDataSource>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Pager : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
//getData();//綁定數(shù)據(jù)方法
//change_BackColor();
}
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
//取得顯示分頁界面的那一行
GridViewRow pagerRow = GridView1.BottomPagerRow;
if (pagerRow != null)
{
//取得第一頁。上一頁。下一頁。最后一頁的超級鏈接
LinkButton lnkBtnFirst = (LinkButton)pagerRow.Cells[0].FindControl("lnkBtnFirst");
LinkButton lnkBtnPrev = (LinkButton)pagerRow.Cells[0].FindControl("lnkBtnPrev");
LinkButton lnkBtnNext = (LinkButton)pagerRow.Cells[0].FindControl("lnkBtnNext");
LinkButton lnkBtnLast = (LinkButton)pagerRow.Cells[0].FindControl("lnkBtnLast");
//設(shè)置何時應(yīng)該禁用第一頁。上一頁。下一頁。最后一頁的超級鏈接
if (GridView1.PageIndex == 0)
{
lnkBtnFirst.Enabled = false;
lnkBtnPrev.Enabled = false;
}
else if (GridView1.PageIndex == GridView1.PageCount - 1)
{
lnkBtnNext.Enabled = false;
lnkBtnLast.Enabled = false;
}
else if (GridView1.PageCount <= 0)
{
lnkBtnFirst.Enabled = false;
lnkBtnPrev.Enabled = false;
lnkBtnNext.Enabled = false;
lnkBtnLast.Enabled = false;
}
//從顯示分頁的行中取得用來顯示頁次與切換分頁的DropDownList控件
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("page_DropDownList");
//根據(jù)欲顯示的數(shù)據(jù)源的總頁數(shù),創(chuàng)建DropDownList控件的下拉菜單內(nèi)容
if (pageList != null)
{
int intPage;
for (intPage = 0; intPage <= GridView1.PageCount - 1; intPage++)
{
//創(chuàng)建一個ListItem對象來存放分頁列表
int pageNumber = intPage + 1;
ListItem item = new ListItem(pageNumber.ToString());
//交替顯示背景顏色
switch (pageNumber % 2)
{
case 0: item.Attributes.Add("style", "background:#CDC9C2;");
break;
case 1: item.Attributes.Add("style", "color:red; background:white;");
break;
}
if (intPage == GridView1.PageIndex)
{
item.Selected = true;
}
pageList.Items.Add(item);
}
}
//顯示當前所在頁數(shù)與總頁數(shù)
Label pagerLabel = (Label)pagerRow.Cells[0].FindControl("lblCurrrentPage");
if (pagerLabel != null)
{
int currentPage = GridView1.PageIndex + 1;
pagerLabel.Text = "第" + currentPage.ToString() + "頁(共" + GridView1.PageCount.ToString() + " 頁)";
}
}
}
protected void page_DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
//取得顯示分頁界面的那一行
GridViewRow pagerRow = GridView1.BottomPagerRow;
//從顯示頁數(shù)的行中取得顯示頁數(shù)的DropDownList控件
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("page_DropDownList");
//將GridView移至用戶所選擇的頁數(shù)
GridView1.PageIndex = pageList.SelectedIndex;
//getData();不用數(shù)據(jù)源需要綁定
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠標經(jīng)過時,行背景色變
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
//鼠標移出時,行背景色變
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
//點擊返回當前行第6列的值
e.Row.Attributes.Add("onclick", "window.returnValue=\"" + e.Row.Cells[5].Text.Trim() + "\";window.close();");
////當有編輯列時,避免出錯,要加的RowState判斷
//if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
//{
// ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你確認要刪除:\"" + e.Row.Cells[1].Text + "\"嗎?')");
//}
//對指定單元格設(shè)置背景顏色
if (e.Row.Cells[8].Text == "待掛牌")
{
e.Row.Cells[8].BackColor = System.Drawing.Color.Yellow;
}
else if (e.Row.Cells[8].Text == "已掛牌")
{
e.Row.Cells[8].BackColor = System.Drawing.Color.YellowGreen;
}
else if (e.Row.Cells[8].Text == "已回退")
{
e.Row.Cells[8].BackColor = System.Drawing.Color.Red;
}
else if (e.Row.Cells[8].Text == "已簽約")
{
e.Row.Cells[8].BackColor = System.Drawing.Color.Violet;
}
else
{
e.Row.Cells[8].BackColor = System.Drawing.Color.White;
}
}
if (e.Row.RowIndex != -1)
{
int id = e.Row.RowIndex + 1;
e.Row.Cells[0].Text = id.ToString();
}
}
protected void change_BackColor()
{
for (int j = 0; j <= GridView1.PageCount - 1; j++)
{
for (int i = 0; i <= GridView1.PageSize - 1; i++)
{
GridViewRow row = GridView1.Rows[i];
string gpzt = row.Cells[3].Text.ToString(); //獲取掛牌狀態(tài)所在單元格
if (gpzt == "0")
{
GridView1.Rows[i].Cells[3].BackColor = System.Drawing.Color.Yellow;
}
else if (gpzt == "1")
{
GridView1.Rows[i].Cells[3].BackColor = System.Drawing.Color.GreenYellow;
}
else if (gpzt == "2")
{
GridView1.Rows[i].Cells[3].BackColor = System.Drawing.Color.Red;
}
else if (gpzt == "3")
{
GridView1.Rows[i].Cells[3].BackColor = System.Drawing.Color.Violet;
}
else
{
GridView1.Rows[i].Cells[3].BackColor = System.Drawing.Color.White;
}
}
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bind();
}
protected void bind()
{
GridView1.DataSourceID = "SqlDataSource1";
GridView1.DataBind();
}
}
-----------------------------------------------------------------------------------------------
值得一提的是window.returnValue只有一個返回值,如果要返回多個返回值就需要對這個返回值
進行處理可以適當加連接符&,-,+等等,returnValue類型不限制,對于字符串類型,最大為4096個字符。
var array = a.split( "&");
window.document.getElementById("textbox1").value = array[0];
window.document.getElementById("textbox2").value = array[1];