日期:2014-05-18  浏览次数:20465 次

showModalDialog传值问题(asp.net C#),十分火急啊!!
问题:一个父窗口Parent.aspx其中有一个隐藏域为HdnCode,用来接收showModalDialog窗体的返回值,showModalDialog子窗口为child.aspx,我要实现的内容是:点击父窗口中的一个确定按钮btn(web控件Button),判断HdnCode是否已赋值,如果HdnCode.Value不等于空,继续执行按钮btn单击事件后面的代码。HdnCode.Value为空,则弹出showModalDialog子窗口child.aspx,处理child.aspx相关的代码,关闭child.aspx,把返回值赋给Parent.aspx的隐藏域HdnCode.Value;再执行按钮btn单击事件后面的代码。整个流程完毕。

出现未能解决的问题是:1.我在C#按钮btn事件中无法得到HdnCode的值,因为当我关闭showModalDialog子窗口child.aspx时,自动触发了父窗口Parent.aspx的Page_Load事件,我HdnCode的值刷没了,得不到想要的结果。2.当我点击C#按钮btn后,判断HdnCode.Value不为时弹出showModalDialog子窗口child.aspx,然后处理child.aspx的代码,最后继续执行按钮btn单击事件后面的代码。但这出现问题,它先执行按钮btn单击事件后面的代码,再弹出showModalDialog子窗口child.aspx。而不是等我关闭showModalDialog子窗口child后才
执行按钮btn单击事件后面的代码,我不知道这是什么原因?愿请高人竭力相助。不知道我把问题描述清楚了没有?不清楚提醒声哈!!!在此谢谢!!明天就要上缴了!!

------解决方案--------------------
首先不要在Page_load中对隐藏域赋值,或者只在第一次Load的时候才赋值
------解决方案--------------------
第二点你的showModalDialog代码写在哪里,如果你写在脚本里应该是可以的,
应该会先执行脚本然后再执行后台click事件
------解决方案--------------------
哎真惨 周末都在扣程序 同情
------解决方案--------------------
关注一下
------解决方案--------------------
1.我在C#按钮btn事件中无法得到HdnCode的值,因为当我关闭showModalDialog子窗口child.aspx时,自动触发了父窗口Parent.aspx的Page_Load事件,我HdnCode的值刷没了,得不到想要的结果。
A:将vhdnCode runat=server

2.当我点击C#按钮btn后,判断HdnCode.Value不为时弹出showModalDialog子窗口child.aspx,然后处理child.aspx的代码,最后继续执行按钮btn单击事件后面的代码。但这出现问题,它先执行按钮btn单击事件后面的代码,再弹出showModalDialog子窗口child.aspx。而不是等我关闭showModalDialog子窗口child后才执行按钮btn单击事件后面的代码,我不知道这是什么原因?

A: 事件应该写如下
if (hdnCode.Value = " ")
{
//弹出提示框
}
else
{
//你需要执行的代码
}

在返回的showModalDialog中在执行 btn.click() 就可以了。 ×btn.click() javascript代码
------解决方案--------------------
还有个办法 脚本生声一个对象 或者是个字段 引用传进对话框
需要返回值就修改内部一个数值
------解决方案--------------------
因为你不熟悉原理,所以有些细节上的问题你没有控制好,

我直接给出 DEMO, 希望合适

// p.aspx
<%@ Page Language= "C# " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<script runat= "server ">

protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(Hidden1.Value);
}
</script>

<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> Untitled Page </title>
<script type= "text/javascript ">
function getDataFromChild() {
var retVal = window.showModalDialog( "child.aspx ");
if(retVal != null) {
document.getElementById( "Hidden1 ").value= retVal;
// just test
document.getElementById( "TextBox1 ").value= retVal;
}
}

function shouldSubmit() {
return (document.getElementById( "Hidden1 ").value.length > 0);
}
</script>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:TextBox ID= "TextBox1 " runat= "server "> </asp:TextBox> <input id= "Hidden1 " type= "hidden " runat= "server " />
<input id= "Button2 " type= "button " value= "Open Child Win " onclick= "getDataFromChild() " />