利用 window.showModalDialog 一次返回多个值

本文介绍如何使用window.showModalDialog方法实现跨页面返回多个值的技术细节。通过定义数组并利用GridView控件选择数据,最终实现多值传递。适用于需在不同页面间交换数据的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       在实际的工作中,我们会遇到,一次,需要返回值,当然这个操作,也可以用多个DropDownList控件,进行多级联动,但这样,会使页面,经常地刷新,不太利于用户的观看。等等。 在利用window.showModalDialog()进行返回一个值得基础上,进行多个值得返回操作。
       在利用window.showModalDialog,方法,主要是利用返回一个数组,之后把数组里面的值取出来,进行赋值操作。
     以下是调用页面的HTML的代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddSecondHand.aspx.cs" StylesheetTheme="Default"
    Inherits
="Machine_AddSecondHand" 
%>

<!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 runat="server">
    
<title>添加拖车信息</title>

    
<script language="javascript" type="text/javascript">
        
function getmachine()
    

        
var a=new Array(4
);
        
var a=window.showModalDialog("../Public/GetMachine.aspx",a,"center:yes;help:no;status:no;rsizable:yes"
);
        document.form1.txtProductCode.value
=a[0
];
        document.form1.txtMachineTypeCode.value
=a[1
];
        document.form1.txtMachineCode.value
=a[2
];
        document.form1.txtCustomerCode.value
=a[3
];
    }

    
</script>

</head>
<body>
    
<form id="form1" runat="server">
        
<fieldset>
            
<legend>菜单</legend>
            
<div id="tabsF">
                
<ul>
                    
<li><href="ListSecondHand.aspx" title="拖车管理"><span>拖车管理</span></a></li>
                    
<li><href="../Default.aspx" title="返回"><span>返回</span></a></li>
                
</ul>
            
</div>
        
</fieldset>
        
<div style="text-align: center">
            
<table cellpadding="4">
                
<tr>
                    
<td class="tdbg" align="left">
                        
<span style="color: #ff0000">*</span>拖车编号:</td>
                    
<td align="left">
                        
<asp:TextBox ID="txtObligeCode" runat="server"></asp:TextBox>
                        
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtObligeCode"
                            ErrorMessage
="拖车编号不能为空">*</asp:RequiredFieldValidator></td>
                    
<td class="tdbg" align="left">
                        
<span style="color: #ff0000">*</span>拖车日期:</td>
                    
<td align="left">
                        
<asp:TextBox ID="txtObligeDate" runat="server"></asp:TextBox>
                        
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtObligeDate"
                            ErrorMessage
="拖车日期不能为空">*</asp:RequiredFieldValidator></td>
                
</tr>
                
<tr style="font-size: small; font-family: 宋体, 仿宋_GB2312, Arial">
                    
<td class="tdbg" align="left">
                        
<span style="color: #ff0000">*</span>产品名称:</td>
                    
<td align="left">
                        
<asp:TextBox ID="txtProductCode" runat="server"></asp:TextBox>
                        
<img src="../Images/folder_open.gif" onclick="getmachine()" />
                        
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="产品名称不能为空"
                            ControlToValidate
="txtProductCode">*</asp:RequiredFieldValidator></td>
                    
<td align="left">
                    
</td>
                    
<td align="left">
                    
</td>
                
</tr>
                
<tr>
                    
<td class="tdbg" align="left">
                        
<span style="color: #ff0000">*</span>设备类型:</td>
                    
<td align="left">
                        
<asp:TextBox ID="txtMachineTypeCode" runat="server" ></asp:TextBox>
                        
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtMachineTypeCode"
                            ErrorMessage
="设备类型不能为空">*</asp:RequiredFieldValidator></td>
                    
<td class="tdbg" align="left">
                        
<span style="color: #ff0000">*</span>设备编号:</td>
                    
<td align="left">
                        
<asp:TextBox ID="txtMachineCode" runat="server" ></asp:TextBox>
                        
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="txtMachineCode"
                            ErrorMessage
="设备编号不能为空">*</asp:RequiredFieldValidator>
                    
</td>
                
</tr>
                
<tr>
                    
<td class="tdbg" align="left">
                        客户名称:
</td>
                    
<td align="left">
                        
<asp:TextBox ID="txtCustomerCode" runat="server"></asp:TextBox>
                        
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtCustomerCode"
                            ErrorMessage
="客户名称不能为空">*</asp:RequiredFieldValidator></td>
                    
<td align="left">
                    
</td>
                    
<td align="left">
                    
</td>
                
</tr>
                
<tr>
                    
<td class="tdbg" align="left">
                        备注:
</td>
                    
<td colspan="3" align="left">
                        
<asp:TextBox ID="txtNote" runat="server" TextMode="MultiLine" Height="180px" Width="443px"></asp:TextBox></td>
                
</tr>
                
<tr>
                    
<td align="center" colspan="4">
                        
<asp:Button ID="btnSave" runat="server" Text="添加" OnClick="btnSave_Click" />
                        
<asp:Button ID="btnClear" runat="server" Text="清除" OnClick="btnClear_Click" /></td>
                
</tr>
            
</table>
        
</div>
        
<fieldset style="text-align: left">
            
<legend>业务逻辑</legend>
            
<ul>
                
<li><span style="color: red">*</span>项不能为空</li>
                
<li>日期格式要正确
                    
                
</li>
                
</ul>
        
</fieldset>
        
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True"
            ShowSummary
="False" />
    
</form>
</body>
</html>

        以上代码的粗体,是重点,其中GetMachine()方法,先定义一个数组,之后将数组的每个值,进行赋值,其中,在被调用的页面进行赋值操作。
       以下是被调用页面的HTML代码:

<%@ Page Language="C#" AutoEventWireup="true"  StylesheetTheme="Default" Theme="Default" CodeFile="GetMachine.aspx.cs" Inherits="Public_GetSecondHand" %>

<!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 runat="server">
    
<title>选择二手车信息</title>
    
<base target="_self" />    
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
<fieldset><legend>车辆信息</legend>
        
<asp:GridView ID="gvSecondHand" SkinID="Default_GridView"  runat="server" OnRowDataBound="gvSecondHand_RowDataBound" OnSelectedIndexChanging="gvSecondHand_SelectedIndexChanging" OnDataBound="gvSecondHand_DataBound">
            
<Columns>
                
<asp:BoundField DataField="TypeName" HeaderText="产品名称" />
                
<asp:BoundField DataField="DictName" HeaderText="产品类型" />
                
<asp:BoundField DataField="MachineCode" HeaderText="整机编号" />
                
<asp:BoundField DataField="CusName" HeaderText="客户名称" />
                
<asp:BoundField DataField="NameCode" HeaderText="产品编号" />
                
<asp:BoundField DataField="TypeCode" HeaderText="类型编号" />
                
<asp:BoundField DataField="CusCode" HeaderText="客户编号" />
                
<asp:CommandField ShowSelectButton="True" />
            
</Columns>
        
</asp:GridView>
    
</fieldset>
    
</div>
    
</form>
</body>
</html>

  被调用的页面,主要是由一个GridView控件,设置控件的数据源,我们主要是,需要将,产品编号、类型编号、整机编号、客户编号 进行返回。在HTML需要主要的,需要将<base target="_Self"/>添加到<head></head>里面,不然,会出现单击‘选择’,重新弹出一个空页面。不能达到我们需要的功能。
       以下是,此页面对应的后台页面代码:

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 Public_GetSecondHand : System.Web.UI.Page
{
    WebUtility.MachineOut secondHand;
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!Page.IsPostBack)
        
{
            secondHand 
= new WebUtility.MachineOut();
            
this.gvSecondHand.DataSource = secondHand.DataSetSecondHand();
            
this.gvSecondHand.DataBind();
        }

    }

    
protected void gvSecondHand_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    
{
        
//返回值 分别是 产品编号,整机类型编号,整机编号,客户编号
        Response.Write("<script language=javascript> var s=Array(4);s[0]='" +
            
this.gvSecondHand.Rows[e.NewSelectedIndex].Cells[4].Text + "';" +
            
"s[1]='" + this.gvSecondHand.Rows[e.NewSelectedIndex].Cells[5].Text + "';s[2]='" +
            
this.gvSecondHand.Rows[e.NewSelectedIndex].Cells[2].Text +"';"+
            
"s[3]='" + this.gvSecondHand.Rows[e.NewSelectedIndex].Cells[6].Text + "';" +
            
"window.returnValue=s;window.close();</script>");
    }

    
protected void gvSecondHand_RowDataBound(object sender, GridViewRowEventArgs e)
    
{
        
if (e.Row.RowType == DataControlRowType.DataRow)
        
{
            e.Row.Cells[
4].Visible = false;
            e.Row.Cells[
5].Visible = false;
            e.Row.Cells[
6].Visible = false;

            e.Row.Attributes.Add(
"onmouseover""e=this.style.backgroundColor; this.style.backgroundColor='linen'");
            e.Row.Attributes.Add(
"onmouseout""this.style.backgroundColor=e");
        }

        
if (e.Row.RowType == DataControlRowType.Header)
        
{
            e.Row.Cells[
4].Visible = false;
            e.Row.Cells[
5].Visible = false;
            e.Row.Cells[
6].Visible = false;
        }


    }
   在后台编程需要主要的是,SelectedIndexChanging  事件,因为使用脚本返回我们需要的值,因此需要主要单引号,不要少了单引号,这个问题,在我调试的过程中,查了好半天,才搞定。
     到此,我们用window.showModalDialog 方法,返回多个值,就做好了,您可以根据您的实际需要,进行改动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值