問題
使用 aspx網頁互相傳值
解決方法
showModelessDialogEX.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="showModelessDialogEX.aspx.cs" Inherits="showModelessDialogEX" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>showModelessDialogEX</title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server" onclick='CallDialog();'></asp:TextBox> </div> </form> </body> </html>
<script type="text/javascript"> function CallDialog() { window.showModalDialog("myDialog.aspx",window,"status:false;dialogWidth:300px;dialogHeight:300px;dialogLeft:50px;dialogTop:200px"); } function SetTextBox(str) { var id = '<%=this.TextBox1.ClientID %>'; document.getElementById(id).value = str; } </script>myDialog.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="myDialog.aspx.cs" Inherits="myDialog" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>myDialog</title> <base target="_self" /> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" onrowdatabound="GridView1_RowDataBound"> <Columns> <asp:TemplateField HeaderText="Select"> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server">Select</asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>
<script type="text/javascript"> function GetInfo(str) { var arg = window.dialogArguments; arg.SetTextBox(str); window.close(); } </script>myDialog.aspx.cs
public partial class myDialog : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.GridView1.DataSource = new string[] { "Dotblogs", "F6 Team", "puma" }; this.GridView1.DataBind(); } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { LinkButton lnkbtn = e.Row.Cells[0].FindControl("LinkButton1") as LinkButton; lnkbtn.OnClientClick = string.Format("GetInfo('{0}'); return false;", e.Row.Cells[1].Text); } } }若是要按下按鈕,回傳輸入的值,可改寫JavaScript如下:
<script type="text/javascript"> function GetTextBox() { var id = '<%=this.txtVerContent.ClientID %>'; GetInfo(document.getElementById(id).value); return false; } function GetInfo(str) { var arg = window.dialogArguments; arg.SetTextBox(str); window.close(); } </script>參考:http://www.dotblogs.com.tw/puma/archive/2008/06/29/4401.aspx
沒有留言:
張貼留言