Click here to Skip to main content
15,881,882 members
Articles / General Programming / File
Article

Passing value from popup window to parent form's TextBox

Rate me:
Please Sign up or sign in to vote.
4.86/5 (5 votes)
11 Oct 2013CPOL2 min read 44.2K   8  
Passing values from a popup window back to the parent page is an often asked question. Especially when there is a GridView type control in the popup.

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Passing values from a popup window back to the parent page is an often asked question. Especially when there is a GridView type control in the popup. In the following example we will be using two forms. The parent form will be parent.aspx and the popup will be popup.aspx. Also note that the 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)

{

    // hardcoded value used to minimize the code.

    // ControlID can instead be passed as query string to the popup window

    window.opener.document.getElementById("ctl00_ContentPlaceHolder1_TextBox2").value = val;

    window.close();

}

</script>

 

 

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">

    <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

        'assuming that the required value column is the second column in gridview

        DirectCast(e.Row.FindControl("btnSelect"), Button).Attributes.Add("onclick", "javascript:GetRowValue('" & e.Row.Cells(1).Text & "')")

    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)) {

        //assuming that the required value column is the second column in gridview

        ((Button)e.Row.FindControl("btnSelect")).Attributes.Add("onclick", "javascript:GetRowValue('" + e.Row.Cells[1].Text + "')");

    }

}

 

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

Happy Coding!!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --