Open a JavaScript Window from a Web User Control and return a valu

G

Guest

hi

I have a Web User Control (ascx) - lets call it "My_WUC" - in a Web form. In
that WUC I want have a textbox and a button. I want to click on the button
and open a popup (I use javascript for that), the popup window will have also
a text box and a button. when the User click on the button the value on the
textbox will be send back to the textbox on My_WUC. I hope I was clear off
what I want to do.

I've been searching for some ideas but I found nothing that would suit me...

I think my problem is that I'm not able to find the textbox in My_WUC
because the ID is dynamic...

This is my code:

------------------
My_WUC.aspx
------------------

<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="WUC_EnviarMensagem.ascx.vb" Inherits="SiteICS.WUC_EnviarMensagem"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<LINK href="ASPNETPortal.css" type="text/css" rel="stylesheet">

<script language="javascript" id="openContactsPageJS">
function getValue (src){
open('child.aspx?src=' + src, 'popup', 'width=400,height=300,scrollbars=1')
}
</script>

<TABLE id="Table1" height="80%" cellSpacing="0" cellPadding="0" width="100%"
border="0">
<TR>
<TD width="10%"><label class="Normal" id="lbl_To">To :</label></TD>
<TD width="85%" colSpan="3"><asp:textbox id="txtb_Destinations"
tabIndex="2" runat="server" Width="100%"
CssClass="NormalTextBox"></asp:textbox></TD>
<TD vAlign="middle" align="center" width="5%"><asp:button
id="bt_adicionarDestinos" runat="server" CssClass="button_blue_2"
Text="Add"></asp:button></TD>
</TR>
</TABLE>

---------------------
My_WUC.aspx.vb
---------------------

Public Class WUC_EnviarMensagem
Inherits System.Web.UI.UserControl

Protected WithEvents bt_adicionarDestinos As
System.Web.UI.WebControls.Button
Protected WithEvents txtb_Destinations As
System.Web.UI.WebControls.TextBox

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

bt_adicionarDestinos.Attributes.Add("onClick",
"getValue('txtb_Destinations ')")
End Sub

End Class


---------------------------------------------------------------------------------
As You can see I have a javascript function that opens "child.aspx" with a
query ?src= src, and I use
"bt_adicionarDestinos.Attributes.Add("onClick", "getValue('txtb_Destinations
')")" to set the "onClick" event of the button to it passing the textbox.

this it the child.aspx code

------------
child.aspx
------------

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="child.aspx.vb"
Inherits="SiteICS.child" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>child</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="formChild" method="post" runat="server">
<asp:button id="buttonChild" style="Z-INDEX: 101; LEFT: 216px; POSITION:
absolute; TOP: 136px"
runat="server" Text="Submit"></asp:button><asp:textbox id="textChild"
style="Z-INDEX: 102; LEFT: 48px; POSITION: absolute; TOP: 136px"
runat="server"></asp:textbox>
</form>
</body>
</HTML>



---------------------
child.aspx.vb
---------------------


Public Class child
Inherits System.Web.UI.Page

Protected WithEvents control As New
System.Web.UI.HtmlControls.HtmlInputHidden

#Region " Web Form Designer Generated Code "

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

End Sub
Protected WithEvents buttonChild As System.Web.UI.WebControls.Button
Protected WithEvents textChild 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
'CODEGEN: This method call is required by the Web Form Designer()
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

control.Value = Request.QueryString("src").ToString()

End Sub

Private Sub buttonChild_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles buttonChild.Click

Dim strScript As String = "<script>window.opener.form[0]." &
control.Value '.Value= '"
strScript += Me.textChild.Text
strScript += "';self.close()"
strScript += "</" + "script>"

RegisterClientScriptBlock("CloseWindow", strScript)
End Sub
End Class


-------------------------------------------------------------------------------

Now this is where, I think its the problem...I'm not able to pass the value
to My_WUC textbox...probably is because I can get the ID of that textbox.

Can some onde help?I really need this to solve this problem...and I'm sure
that this is just a stupid mistake...I cant think that it is impossible to
have this kind off "Input Box" with javascript.

Thank you very much in advance.

Jorge Ponte
 
K

Karl Seguin

First off, since you are using so much javascript, I'm not sure why your
popup needs to postback, simply use javascript controls. I'd also move the
population javascript code to the user control, which will help the dynamic
id problem you are having.

POPUP (no codebehind)
<input type="text" id="child" /> <input type="button" value="Submit"
onClick="SetValue()

<script language="javascript">
function SetValue(){
var txt = document.getElementById('child');
window.opener.SetValue(txt.value);
self.close();
}
</script>


User Control:
<script language="javascript">
function SetValue(value){
var txt = document.getElementById('<%=txtb_Destinations.ClientId%>');
txt.value = value;

}
</script>


Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Jorge Ponte said:
hi

I have a Web User Control (ascx) - lets call it "My_WUC" - in a Web form. In
that WUC I want have a textbox and a button. I want to click on the button
and open a popup (I use javascript for that), the popup window will have also
a text box and a button. when the User click on the button the value on the
textbox will be send back to the textbox on My_WUC. I hope I was clear off
what I want to do.

I've been searching for some ideas but I found nothing that would suit me...

I think my problem is that I'm not able to find the textbox in My_WUC
because the ID is dynamic...

This is my code:

------------------
My_WUC.aspx
------------------

<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="WUC_EnviarMensagem.ascx.vb" Inherits="SiteICS.WUC_EnviarMensagem"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<LINK href="ASPNETPortal.css" type="text/css" rel="stylesheet">

<script language="javascript" id="openContactsPageJS">
function getValue (src){
open('child.aspx?src=' + src, 'popup', 'width=400,height=300,scrollbars=1')
}
</script>

<TABLE id="Table1" height="80%" cellSpacing="0" cellPadding="0" width="100%"
border="0">
<TR>
<TD width="10%"><label class="Normal" id="lbl_To">To :</label></TD>
<TD width="85%" colSpan="3"><asp:textbox id="txtb_Destinations"
tabIndex="2" runat="server" Width="100%"
CssClass="NormalTextBox"></asp:textbox></TD>
<TD vAlign="middle" align="center" width="5%"><asp:button
id="bt_adicionarDestinos" runat="server" CssClass="button_blue_2"
Text="Add"></asp:button></TD>
</TR>
</TABLE>

---------------------
My_WUC.aspx.vb
---------------------

Public Class WUC_EnviarMensagem
Inherits System.Web.UI.UserControl

Protected WithEvents bt_adicionarDestinos As
System.Web.UI.WebControls.Button
Protected WithEvents txtb_Destinations As
System.Web.UI.WebControls.TextBox

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

bt_adicionarDestinos.Attributes.Add("onClick",
"getValue('txtb_Destinations ')")
End Sub

End Class


-------------------------------------------------------------------------- -------
As You can see I have a javascript function that opens "child.aspx" with a
query ?src= src, and I use
"bt_adicionarDestinos.Attributes.Add("onClick", "getValue('txtb_Destinations
')")" to set the "onClick" event of the button to it passing the textbox.

this it the child.aspx code

------------
child.aspx
------------

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="child.aspx.vb"
Inherits="SiteICS.child" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>child</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="formChild" method="post" runat="server">
<asp:button id="buttonChild" style="Z-INDEX: 101; LEFT: 216px; POSITION:
absolute; TOP: 136px"
runat="server" Text="Submit"></asp:button><asp:textbox id="textChild"
style="Z-INDEX: 102; LEFT: 48px; POSITION: absolute; TOP: 136px"
runat="server"></asp:textbox>
</form>
</body>
</HTML>



---------------------
child.aspx.vb
---------------------


Public Class child
Inherits System.Web.UI.Page

Protected WithEvents control As New
System.Web.UI.HtmlControls.HtmlInputHidden

#Region " Web Form Designer Generated Code "

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

End Sub
Protected WithEvents buttonChild As System.Web.UI.WebControls.Button
Protected WithEvents textChild 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
'CODEGEN: This method call is required by the Web Form Designer()
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

control.Value = Request.QueryString("src").ToString()

End Sub

Private Sub buttonChild_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles buttonChild.Click

Dim strScript As String = "<script>window.opener.form[0]." &
control.Value '.Value= '"
strScript += Me.textChild.Text
strScript += "';self.close()"
strScript += "</" + "script>"

RegisterClientScriptBlock("CloseWindow", strScript)
End Sub
End Class


-------------------------------------------------------------------------- -----

Now this is where, I think its the problem...I'm not able to pass the value
to My_WUC textbox...probably is because I can get the ID of that textbox.

Can some onde help?I really need this to solve this problem...and I'm sure
that this is just a stupid mistake...I cant think that it is impossible to
have this kind off "Input Box" with javascript.

Thank you very much in advance.

Jorge Ponte
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top