admin管理员组文章数量:1296505
if i want to pass in my value for confirmation box!!! so lets say i want to delete item no 1 so i checked the check box then when i pressed the delete button. the popup box e out with detail item no 1 in it.
i have implemented the popup using show modal dialog (JavaScript) but i still cant get the parent value into the child form!
furthermore if i have item no 1 and item no 2 checked i want both to be displayed in the confirmation box!!
any suggestion would be greatly appreciated thx you!!!
if i want to pass in my value for confirmation box!!! so lets say i want to delete item no 1 so i checked the check box then when i pressed the delete button. the popup box e out with detail item no 1 in it.
i have implemented the popup using show modal dialog (JavaScript) but i still cant get the parent value into the child form!
furthermore if i have item no 1 and item no 2 checked i want both to be displayed in the confirmation box!!
any suggestion would be greatly appreciated thx you!!!
Share Improve this question edited Sep 29, 2009 at 3:22 MusiGenesis 75.4k41 gold badges197 silver badges339 bronze badges asked Sep 29, 2009 at 2:28 LoxZLoxZ 3- Is your modal a new window or a DIV? – rick schott Commented Sep 29, 2009 at 2:38
- this is my modal dialog code window.showModalDialog('PopUp.aspx',null,'status:no;dialogWidth:350px; dialogHeight:200px;dialogHide:true;help:no;scroll:no'); – LoxZ Commented Sep 29, 2009 at 3:06
- if you can show me the code using DIV also can!! – LoxZ Commented Sep 29, 2009 at 3:10
3 Answers
Reset to default 1The middle parameter for showModalDialog can be an object or an array or anything you like, and this passed object can be retrieved in the child form (say, in its OnLoad event) by referencing window.dialogArguments.
Hang on a second and I'll include a code sample (it's been about 10 years since I've done this).
Update: here is a very simple code sample that shows the basics of passing data back and forth between the parent and child windows using showModalDialog. Create two HTML files in the same folder, and name them "Parent.htm" and "Child.htm". Put this code in Parent.htm:
<HTML>
<input type=button value="CustomConfirm"
onclick="ShowMyDialog()">
<script language="javascript">
function ShowMyDialog()
{
var obj = new Object();
obj.data1 = 'some data 1';
obj.data2 = 'some data 2';
showModalDialog('Child.htm', obj, '');
alert(obj.returnvalue);
}
</script>
</HTML>
and put this code in Child.htm:
<HTML>
<body onload="ReadPassedData()"
onunload="DoUnload()">
<input type=text id="textbox1">
<br>
<input type=text id="textbox2">
<br>
<br>
Return value:<br>
<input type=text id="textbox3"
value="type something">
</body>
<script language="javascript">
function ReadPassedData()
{
var obj = window.dialogArguments;
var tb1 = document.getElementById('textbox1');
tb1.value = obj.data1;
var tb2 = document.getElementById('textbox2');
tb2.value = obj.data2;
}
function DoUnload()
{
var obj = window.dialogArguments;
obj.returnvalue = textbox3.value;
}
</script>
</HTML>
Then open Parent.htm in a browser and click the "CustomConfirm" button. The child window will display the values set in the parent window ("some data 1" and "some data 2"), and when the child window is closed, whatever you've entered in the third text box will be displayed in an alert box called from the parent. This shows the basic way in which you pass data to the child and get data back from it.
There's also a way to return an object from the child window (which bees the value returned from the showModalDialog call itself), but I don't recall how to do this.
Update 2: To pass an array instead, you would do something like this:
var myarray = new Array();
myarray[0] = "Bob Smith";
myarray[1] = "Doug Jones";
myarray[2] = "Englebert Humperdinck";
var ret = showModalDialog('Child.htm', myarray, '');
alert(ret); // this will display whatever the child set for its
// window.returnValue
And then in the child window, you would get the array like before and use it to build your details display:
var myarray = window.dialogArguments;
alert(myarray[0]); // or whatever
Since you're now passing in an array instead of an object, you'll need to return true or false (instead of adding a returnvalue property to the passed object). You set the return value in the child by setting the window.returnValue
property. Since you're creating a confirmation popup, you would presumably have a "Yes" and a "Cancel" button which would set window.returnValue
to true
or false
, respectively.
If you are using a popup window you can reference the parent like this:
window.opener.document.getElementById("whatever").value = whatever;
Suppose we have the WindowTrigger.aspx which contains a button as shown below and which invokes the following script and executes and which opens the new page WindowContainer.aspx using showModalDialog.
<asp:Button ID="btnDate" Text="Display Window" OnClientClick="WindowOpen()" runat="server" />
<script language="javascript" type="text/javascript">
function WindowOpen() {
var theAdmin = new Array();
theAdmin["id"] = '';
theAdmin["name"] = '';
var winopts = "dialogWidth:290px;dialogHeight:220px;scroll:no;resizable:no;status:no;unadorned:yes";
var admStatus = window.showModalDialog("http://localhost:51836/WindowContainer.aspx", theAdmin, winopts);
alert(theAdmin["id"]);
alert(theAdmin["name"]);
}
</script>
The above script contains the array declaration 'theAdmin' which is passed to Child window using 2nd argument as shown
The following code is in WindowContainer.aspx which contains a listbox and a button,on click of the button the argument will get intialized to corresponding selected Texts and selected Values of the listbox.
<asp:ListBox ID="ddlUsers" runat="server" Rows="10" SelectionMode="Multiple">
<asp:ListItem Text="Chennai" Value="044"></asp:ListItem>
<asp:ListItem Text="Hyderabad" Value="040"></asp:ListItem>
</asp:ListBox>
<asp:Button ID="btnDate" Text="Date" OnClientClick="submitForm()" runat="server" />
<script language="javascript" type="text/javascript">
function submitForm() {
var lstAdmins = document.getElementById('ddlUsers');
var SelectedIDs = "";
var SelectedNames = "";
if (lstAdmins != null) {
for (i = 0; i < lstAdmins.options.length; i++) {
if (lstAdmins.options[i].selected) {
if (SelectedIDs == '')
SelectedIDs = lstAdmins.options[i].value;
else
SelectedIDs += ";" + lstAdmins.options[i].value;
if (SelectedNames == '')
SelectedNames = lstAdmins.options[i].text;
else
SelectedNames += ";" + lstAdmins.options[i].text;
}
}
}
var theAdmin = dialogArguments;
theAdmin["id"] = SelectedIDs;
theAdmin["name"] = SelectedNames;
window.returnValue = "OK";
self.close();
}
</script>
Now the output will be "044;040;" and "Chennai;Hyderabad;" respectively.In this way we can pass values(here theAdmin) from parent form to child form,and the result can also be used in parent form(there is no lose of scope to the parameter).
本文标签: Pass Value From Parent Form to Child Form using javascript showModalDialogStack Overflow
版权声明:本文标题:Pass Value From Parent Form to Child Form using javascript showModalDialog - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741620183a2388762.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论