asp.net 彈出對話方塊返回多個值

來源:互聯網
上載者:User

說了這麼多,可能不知道我在說什麼。,你就知道了。呵呵。


圖中,姓名有英文和中文之分。當使用者單擊對話方塊中的選擇按鈕時,就可以返回給父對話方塊了。

下面說代碼了:
這裡共包含3個頁面
結構如:

其中Default.aspx的代碼如下:複製代碼 代碼如下:<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>彈出選擇視窗</title>
<script language="javascript" type="text/javascript"><!--

function ShowDialog(ch,en,src)
{
var array=new Array();
array[0]=document.getElementById(ch);
array[1]=document.getElementById(en);

showModalDialog(src,array,"resizable:yes;");//src 為彈出頁面,array 傳過去的參數。
}

// --></script>
</head>
<body>
<form id="form1" runat="server">
<table border="1px">
<tr>
<td> </td>
<td style="text-align:center;" style="text-align:center;">中文</td>
<td style="text-align:center;" style="text-align:center;">英文</td>
<td> </td>
</tr>
<tr>
<td>姓名:</td>
<td><asp:TextBox ID="chTxtName" runat="server"></asp:TextBox></td>
<td><asp:TextBox ID="enTxtName" runat="server"></asp:TextBox></td>
<td><input type="button" id="btnChoiceName" value="選擇" onclick="ShowDialog('chTxtName','enTxtName','Frame.aspx');" /></td>
</tr>
</table>
</form>
</body>
</html>

其中javascript 彈出modaldialog,並且傳過去是一個數組,數組中包含對象。這樣就實現了,同時傳多個值了。
然後我使用了架構,使用了架構才能解決彈出的頁面GridView.aspx無法傳值和緩衝的問題了。
下面看Frame.aspx的代碼,也很簡單,無後台代碼,只是一個iframe 複製代碼 代碼如下:<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>架構</title>
</head>
<body>
<form id="form1" runat="server">
<iframe id="gridview" src="GridView.aspx" src="GridView.aspx" style="position:relative;width:100%;" scrolling="no" frameborder="0" onload="document.getElementById('gridview').style.height=(gridview.document.body.scrollHeight+20)+'px'"></iframe>
</form>
</body>
</html>

這個iframe是自適應大小的。通過onload事件實現的。

好了,看GridView.aspx頁面吧。

其代碼如下:

複製代碼 代碼如下:<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView</title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<asp:TextBox ID="txtChName" runat="server"></asp:TextBox></td>
<td>
<asp:TextBox ID="txtEnName" runat="server"></asp:TextBox></td>
<td style="width: 164px">
<asp:Button ID="btnNew" runat="server" Text="建立" OnClick="btnNew_Click" /></td>
</tr>
<tr>
<td colspan="3">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" Width="359px" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" OnSelectedIndexChanging="GridView1_SelectedIndexChanging">
<Columns>
<asp:BoundField DataField="chName" HeaderText="中文" />
<asp:BoundField DataField="enName" HeaderText="英文" />
<asp:CommandField ShowEditButton="True" >
<ControlStyle Width="100px" />
</asp:CommandField>
<asp:CommandField ShowSelectButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<EmptyDataTemplate>
無資料
</EmptyDataTemplate>
<FooterStyle BackColor="Tan" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>
</td>
</tr>
</table>
</form>
</body>
</html>

在這個頁面裡可以建立、插入、刪除和更新。單擊選擇時就可以返回了,當單擊選擇時觸發下面的事件:

複製代碼 代碼如下:protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
string chName = GridView1.Rows[e.NewSelectedIndex].Cells[0].Text;
string enName = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
Response.Write("<:script language=\"javascript\">
window.dialogArguments[0].value='" + chName + "';window.dialogArguments[1].value='" + enName + "';window.close();<script>");
}

上面的代碼就是返回的重點;window.dialogArguments實際上就是我們剛剛傳過來的array數組。所以它有2個對象,這2個對象就是我們要賦值的對象。通過這一句就可以達到我們的目的了。

提供原代碼下載:其中包括資料庫。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.