Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting control value via javascript called from function

Status
Not open for further replies.

groston

IS-IT--Management
Dec 31, 2001
141
US
I am trying to do the following: On a page have a button that fires a code-behind function (to perform some server-side actions) which in turn calls a javascript function to launch a dialog window. The dialog window accepts some user input and upon the user's pushing a button, fires a code-behind function (to perform some server-side actions) then return a value to the calling page.

What follows is a stripped-down test to try to accomplish this. Everything works, except...
The value is never displayed in the TextBox (txtRetval) on MainForm (see the two line in jsPopupClass.vb marked ****). I have confirmed, using 'alert', that the text typed into DialogForm is indeed being returned.

Would you please help me fix this code. Thanks!

=== MainForm.aspx ===

<%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;MainForm.aspx.vb&quot; Inherits=&quot;testModal.MainForm&quot;%>
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content=&quot;True&quot; name=&quot;vs_snapToGrid&quot;>
<meta content=&quot;Microsoft Visual Studio .NET 7.1&quot; name=&quot;GENERATOR&quot;>
<meta content=&quot;Visual Basic .NET 7.1&quot; name=&quot;CODE_LANGUAGE&quot;>
<meta content=&quot;JavaScript&quot; name=&quot;vs_defaultClientScript&quot;>
<meta content=&quot; name=&quot;vs_targetSchema&quot;>
</HEAD>
<body MS_POSITIONING=&quot;GridLayout&quot;>
<form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;>
<asp:button id=&quot;cmdOpen&quot; style=&quot;Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 56px&quot; runat=&quot;server&quot;
Width=&quot;184px&quot; Height=&quot;32px&quot; Text=&quot;Open dialog&quot;></asp:button>
<asp:TextBox id=&quot;txtRetval&quot; style=&quot;Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 24px&quot; runat=&quot;server&quot;
Width=&quot;184px&quot;>Return test will go here</asp:TextBox></form>
</body>
</HTML>

=== MainForm.aspx.vb ====

Public Class MainForm
Inherits System.Web.UI.Page

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents cmdOpen As System.Web.UI.WebControls.Button
Protected WithEvents txtRetval As System.Web.UI.WebControls.TextBox

Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
InitializeComponent()
End Sub

Private Sub cmdOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOpen.Click
' do some server-side stuff, like database query, etc
Dim addTestPopup As New jsPopupClass
addTestPopup.doPopup(Me)
End Sub
End Class

=== DialogForm.aspx ===

<%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;DialogForm.aspx.vb&quot; Inherits=&quot;testModal.DialogForm&quot;%>
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<HTML>
<HEAD>
<title>DialogForm</title>
<meta name=&quot;vs_snapToGrid&quot; content=&quot;True&quot;>
<meta name=&quot;GENERATOR&quot; content=&quot;Microsoft Visual Studio .NET 7.1&quot;>
<meta name=&quot;CODE_LANGUAGE&quot; content=&quot;Visual Basic .NET 7.1&quot;>
<meta name=&quot;vs_defaultClientScript&quot; content=&quot;JavaScript&quot;>
<meta name=&quot;vs_targetSchema&quot; content=&quot; </HEAD>
<body MS_POSITIONING=&quot;GridLayout&quot;>
<form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;>
<asp:TextBox id=&quot;txtEntry&quot; style=&quot;Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 48px&quot; runat=&quot;server&quot;
Height=&quot;32px&quot; Width=&quot;184px&quot;></asp:TextBox>
<asp:Button id=&quot;cmdClose&quot; style=&quot;Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 88px&quot; runat=&quot;server&quot;
Height=&quot;32px&quot; Width=&quot;184px&quot; Text=&quot;Close dialog&quot;></asp:Button>
<asp:Label id=&quot;Label1&quot; style=&quot;Z-INDEX: 103; LEFT: 16px; POSITION: absolute; TOP: 24px&quot; runat=&quot;server&quot;
Height=&quot;24px&quot; Width=&quot;176px&quot;>Text to return</asp:Label>
</form>
</body>
</HTML>

=== DialogForm.aspx.vb ===

Public Class DialogForm
Inherits System.Web.UI.Page

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents cmdClose As System.Web.UI.WebControls.Button
Protected WithEvents txtEntry As System.Web.UI.WebControls.TextBox
Protected WithEvents Label1 As System.Web.UI.WebControls.Label

Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
InitializeComponent()
End Sub

Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
' do some server-side stuff, like database insert, etc
Dim Script As String = vbCrLf + &quot;<script language=JavaScript id='CloseDialog'>&quot; + vbCrLf
Script += &quot;window.returnValue = '&quot; + txtEntry.Text + &quot;'; &quot; + vbCrLf
Script += &quot;window.close();&quot; + vbCrLf
Script += &quot;</script>&quot; + vbCrLf

If (Not Page.IsStartupScriptRegistered(&quot;CloseDialog&quot;)) Then
Page.RegisterStartupScript(&quot;CloseDialog&quot;, Script)
End If
End Sub
End Class

=== jsPopupClass.vb ===

Public Class jsPopupClass
Public Sub doPopup(ByVal myPage As System.Web.UI.Page)
Dim Script As String = vbCrLf + &quot;<script language=JavaScript id='PopupPage'>&quot; + vbCrLf
Script += &quot;var retText = null;&quot; + vbCrLf
Script += &quot;var WinSettings = 'center:yes;resizable:no;dialogHeight:200px';&quot; + vbCrLf
Script += &quot;retText = window.showModalDialog('DialogForm.aspx', null, WinSettings);&quot; + vbCrLf
Script += &quot;var objForm = document.forms['Form1']; &quot; + vbCrLf ****
Script += &quot;var objFormField = objForm.elements['txtRetval']; &quot; + vbCrLf ****
Script += &quot;objFormField.Text = retText;&quot; + vbCrLf
Script += &quot;</script>&quot; + vbCrLf

If (Not myPage.IsStartupScriptRegistered(&quot;PopupPage&quot;)) Then
myPage.RegisterStartupScript(&quot;PopupPage&quot;, Script)
End If
End Sub
End Class



----

Gerry Roston
gerry@pairofdocs.net
 
In DialogForm.aspx.vb:
Code:
instead of:
Script += &quot;window.returnValue = '&quot; + txtEntry.Text + &quot;'; &quot; + vbCrLf
try:
Script += &quot;window.opener.document.getElementById('txtRetval').value='&quot; + txtEntry.Text + &quot;';&quot; + vbCrLf
 
LV,

You must tell me where you live so I can take you out for a beer - thanks for all of your help!

Anyway, I had tried something like this earlier. It works perfectly well when you use window.open, but not when you use window.showModalDialog. When I tried this, the error:

Microsoft JScript runtime error: 'window.opener.document' is null or not an object

was displayed.

Any other suggestions?



----

Gerry Roston
gerry@pairofdocs.net
 
I know, sorry for the error in my script. ALWAYS check it before you post it - forgot about this golden rule. Let me get back to you in about 5 min.
 
1. change in jsPopupClass.vb
Code:
change line:
Script += &quot;retText = window.showModalDialog('DialogForm.aspx', null, WinSettings);&quot; + vbCrLf
to:
Script += &quot;retText = window.showModalDialog('DialogForm.aspx', self, WinSettings);&quot; + vbCrLf
Having &quot;self&quot; as the second agrument, we pass the parent window as an argumnet to the dialog window.
2. change in DialogForm.aspx.vb
Code:
instead of line:
Script += &quot;window.returnValue = '&quot; + txtEntry.Text + &quot;'; &quot; + vbCrLf
add these two lines:
Script += &quot;var pnt = window.dialogArguments;&quot; + vbCrLf
Script += &quot;pnt.document.getElementById('txtRetval').value='&quot; + txtEntry.Text + &quot;';&quot; + vbCrLf
window.dialogArguments in the dialog window willl return the parent window itself - remember we passed it from the opening form. Give it a shot to make sure it works and then we'll talk about the beer :). Just kidding.
 
Works as advertised! Thanks.


----

Gerry Roston
gerry@pairofdocs.net
 
The key to your solution was the document.getElementById. I found that I could leave DialogForm.aspx.vb alone and in jsPopupClass replace:

Script += &quot;var objForm = document.forms['Form1']; &quot; + vbCrLf
Script += &quot;var objFormField = objForm.elements['txtRetval']; &quot; + vbCrLf
Script += &quot;objFormField.Text = retText;&quot; + vbCrLf

with


Script += &quot;document.getElementById('txtRetval').value = retText;&quot; + vbCrLf

Thanks again!



----

Gerry Roston
gerry@pairofdocs.net
 
LV,

If you're still looking in, maybe you can offer some guidance.

The reason I needed a solution to this issue is that on the main form, I have a DropDownList. The first item in the list is &quot;Add new item&quot;. When selected, the child form is opened (in dialog mode), the user enters the, and then hits 'Done' button. This causes the data to be stored into the database and the record key for the new record is returned to the main form.

What I had been trying is to set ddl.SelectedValue to the returned value. This doesn't work (duh) because the ddl does not yet have this new record as it was added to the database table after the ddl was populated.

So, what I need to do, I guess, is to rerun the query that filled the ddl upon return from the dialog window. However, that is all server side code and I do not know if it can be run from within the javascript function.

Can you offer any suggestions?


----

Gerry Roston
gerry@pairofdocs.net
 
Let me run a little samle and I'll get back to you.
 
OK, still haven't had a chance to run a sample, but here is what comes to mind. Since you're probably populating the dropdown on the parent while page is loading, all you need to do is refresh the page after the new item was added int the pop up. So it should be an addition to the jsPopupClass as a last line of code:
Code:
Script += &quot;window.location='MainForm.aspx';&quot; + vbCrLf
This should redirect the page to itself and refresh it so the new entry will appear in the ddl. Let's see if this works.
 
LV,

I think that you are correct. However, since this reload would occur after the line in which the value for the DDL is set, I cannot imagine how this would work.

However, if I were to store the return value in a hidden control, I could, in load_page, requery the db if there is a value in the control and then set SelectedIndex to that value.

I'll give it a whirl.


----

Gerry Roston
gerry@pairofdocs.net
 
OK, so the line
Code:
Script += &quot;document.getElementById('txtRetval').value = retText;&quot; + vbCrLf
was actualy for testing? In real life you're adding a new option to a ddl?
 
Right. That was the surrogate for the actual line of code:

Script += &quot;{document.getElementById('ddlSponsor').value = newSpondorID;}&quot; + vbCrLf

Now the sad news, the suggested line fo code, when executed, prodcued the following error:

Microsoft JScript runtime error: 'contentWindow.document' is null or not an object

----

Gerry Roston
gerry@pairofdocs.net
 
The suggested line fo code is the one for refreshing the window? Also, before you set the value by
Script += &quot;{document.getElementById('ddlSponsor').value = newSpondorID;}&quot; + vbCrLf
you have to add the new item to the ddl, right?
 
Yes, sorry, was not specific enough. The suggested line of code:

Script += &quot;window.location='MainForm.aspx';&quot; + vbCrLf

when executed, produced the following error:

Microsoft JScript runtime error: 'contentWindow.document' is null or not an object.

Yes, the new item needs to be added to the ddl. That was the point of my first comment of today. If, instead of setting the value directly, I instead store it in a hidden field and use a line of code that causes the parent window to refresh, I can update the ddl and set the appropriate value in page_load.

p.s. The actual item is added to the underlying table as a part of the click event for the 'Done' button on the popup window.

----

Gerry Roston
gerry@pairofdocs.net
 
Then probably in the click event for the 'Done' button, after you add a new item to the dll sitting on the parent, you can do a loop through all ddl's items and since you know the new item value, you can find it and set this item as selected.
Code:
for(i=0;i<ddl.length;i++){
 if(ddl.options[i].value == newSpondorID){
   ddl.options[i].selected = true;
 }
 else{
   // do not break out of the loop to uselect all other options
   // in order not to have two options selcted at the same time
   ddl.options[i].selected = false;
 }
}
 
I see what you are suggesting. I guess that it is possible for the 'Done' button on dialog form to access cotnrols on the main form by passing in a reference to the main form as a window.dialogArguments.

I am working on another project right now - I'll try this out tomorrow. Great idea, thanks!


----

Gerry Roston
gerry@pairofdocs.net
 
LV,

I haven't forgotten about you - I just got pulled into 17 other things. I also realized a fundamental error with my approach - I was updating the underlying DB without giving the user the chnace to cancel. So, I am fixing this, having all changes be applied to a dataset object until the update confirmation is given. however, that does not change the basic idea, and I still believe that your apporach is the correct one.

Thanks!


----

Gerry Roston
gerry@pairofdocs.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top