Passing value from popup window to parent form's TextBox

 Rate It (22)

Once again seen lot of questions on the forum related to passing values from popup window to the parent form textbox. Specially when they have some GridView type control in the popup. In the following example I will be using two forms, parent form will be parent.aspx and popup will be popup.aspx. Also note that my parent.aspx form is derived from some MasterPage. Code is provided both in VB.Net and C#.Net.

--- .aspx of parent form ---

<script type="text/javascript">
        function OpenPopup()
        {
            window.open("popup.aspx","List","scrollbars=no,resizable=no,width=400,height=280");
            return false;
        }
</script>
.
.
.
<asp:TextBox ID="txtPopupValue" runat="server" Width="327px"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Show List" />

--- .vb of parent.aspx if vb.net is the language ---

If Not IsPostBack Then
 Me.Button1.Attributes.Add("onclick", "javascript:return OpenPopup()")
End If

--- .cs of parent.aspx if C#.net is the language ---

if !(IsPostBack)
{
 this.Button1.Attributes.Add("onclick", "javascript:return OpenPopup()");
}

 

--- .aspx of popup form ---

<script language="javascript">
        function GetRowValue(val)
        {
            window.opener.document.getElementById("ctl00_ContentPlaceHolder1_TextBox2").value = val; //hardcoded value used to minimize the code. ControlID can be passed as query string to the popup window
            window.close();
        }
</script>
.
.
.
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" Width="400px" AllowPaging="True">
            <Columns>
                <asp:TemplateField>
                    <AlternatingItemTemplate>
                        <asp:Button ID="btnSelect" runat="server" Text="Select" />
                    </AlternatingItemTemplate>
                    <ItemTemplate>
                        <asp:Button ID="btnSelect" runat="server" Text="Select" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

--- .vb file if vb.net is the language ---

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            DirectCast(e.Row.FindControl("btnSelect"), Button).Attributes.Add("onclick", "javascript:GetRowValue('" & e.Row.Cells(1).Text & "')") 'assuming that the required value column is the second column in gridview
        End If
    End Sub

--- .cs file if C#.net is the language ---

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if ((e.Row.RowType == DataControlRowType.DataRow)) {
        ((Button)e.Row.FindControl("btnSelect")).Attributes.Add("onclick", "javascript:GetRowValue('" + e.Row.Cells(1).Text + "')");
        //assuming that the required value column is the second column in gridview
    }
}

 

I hope the code above is straight forward and easy to understand.

Happy Coding!!!

Revision number 1, Saturday, February 16, 2008 5:50:12 AM by
This is not the most up to date version of this article. The most recent version can be found here.

Comments

This sounds fine if you only have one field. Would it not be better to pass a couple of parameters to the to the new window so that you could reuse the same popup window for different lookup values??

This was a helpful article. Thanks!

Looks great, just doesn't compile if you use the C# code. Error: Error 1 Non-invocable member 'System.Web.UI.WebControls.TableRow.Cells' cannot be used like a method. C:\Code\WebSite1\popup.aspx.cs 28 115 C:\Code\WebSite1\

c# sample has been corrected to use square brackets when indexing into the cells collection.

Well, got past that error (need to use [ ] instead of ( ) for csharp code!) and not the popup doesnt respond when i select the "Select" button. the javascript code in the popup does not do anything. sorry to rain on the parade.

Ahh, got past another bug: The asp gridview definition in the html code needs this added to the first line: "OnRowDataBound="GridView1_RowDataBound" In order for that event to get 'wired up'.

I have it working, using this: var a1 = window.opener.document.getElementById("txtPopupValue"); I have one Question: Does getElementByID("txtPopupValue") work every time ? Or do I need to change that to something like: ("txtPopupValue.ClientID") because the 'rendered' control id changes ? Thx !! Bud.

what about special characters

Simple N Clear, Thanks

Rocks!

why is there an SQLdatasource in the pop up window. What if i'm not accessing a database at all or a gridview in the pop up

OK, so how would you trigger the TextChanged event for the textbox when the Select button in the popup is hit?

Great article

Well, the aricle is pretty helpful.but I want to ask that this "window.opener.document.getElementById("ctl00_ContentPlaceHolder1_TextBox2").value" will be going to work for every browser?

Thanks for sharing. Great article and very helpful.

Exactly this was what I needed for my project. Thanks a lot!!

Hi, This can be more powerful when combined with Ajax and class objects. Like you can post from the pop window to the server and save the values in a list object and refer the same in the gri that called them.

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. mbanavige (5)
  2. SGWellens (4)
  3. maartenba (2)
  4. rami_nassar (2)
  5. stiansol (2)
  6. MisterFantastic (2)
  7. satish1.v (1)
  8. raklos (1)
  9. mosessaur (1)
  10. Jos Branders (1)

Advertise Here

Microsoft Communities
Page view counter