Skip to main content

Passing value from popup window to parent form's TextBox Using JavaScript


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 + "')");
    }
}

Comments

Popular posts from this blog

Social tagging overview in Sharepoint 2010

A tag is a word or phrase that identifies an individual piece of information according to a set of attributes or criteria. Tags make it easy to find and share information about a specific subject or task. Social tagging helps users categorize information in ways that are meaningful to them. Social tagging can improve the quality of search results by filtering against specific tags, and it can also connect individuals who want to share information with other users who have like interests. This article describes the social tagging features in Microsoft SharePoint Server 2010. This article does not describe how to configure social tagging features. It also does not discuss how to implement social tagging features as part of an overall social media strategy for an enterprise. About using social tagging features Social tagging features help users to share information and to retrieve relevant, high-quality content more efficiently. Such sharing encourages collaboration and b...

How to Create Secure Store Service for BCS in Sharepoint 2010

How to use Secure Store Service for SharePoint 2010.BCS Access Account representive of the Credential Owner [Impersonated User], and how to wire it up in SharePoint Designer 2010. Prep Work a)Active Directory Users in Play       1.The Service Account I am selecting as the Impersonated User (Credential Owner)       2.The Security Group where all the people that will consume BCS Data will reside b)SQL Server Security      1.Who has Access to What Setup a)Creating & Configuring the Secure Store Object b)Creating & Configuring the External Content Type in SharePoint Designer 2010 1.Creating External Connection with Secure Store 2.Creating the External Content Type c)Reviewing the External Content Type (ECT) d)Reviewing the Security on the ECT Test & Validation a)Creating an External List derived from the ECT b)Logging on as a User from the Security Group AND Secured in the permission set...

AI and Microsoft: Revolutionizing Efficiency in Nonprofit Organizations

  How AI and Microsoft Enhance Efficiency in Nonprofit Organizations In today’s fast-paced world, nonprofit organizations face unique challenges—limited resources, increasing demands, and the constant need to do more with less. But what if technology could be the game-changer they need? In my latest research paper,  "How AI and Microsoft Enhance Efficiency in Nonprofit Organizations" , I explore how cutting-edge technologies like Artificial Intelligence (AI) and Microsoft’s innovative tools are revolutionizing the way nonprofits operate. From streamlining administrative tasks to enhancing donor engagement and optimizing resource allocation, AI and Microsoft’s solutions are empowering nonprofits to focus on what truly matters—their mission. This paper dives deep into real-world examples, practical applications, and the transformative potential of these technologies. Whether you’re a nonprofit professional, a tech enthusiast, or simply curious about the intersection of technolo...