Skip to main content

How To Pass Value From Child Control(Gridview) To Parent Page.

1.Create A New Application in VS.NET and Add Parent.aspx Page and add the following code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ContactList.aspx.cs" Inherits="ContactList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
 <title>Untitled Page</title>

 <script type="text/javascript">
function myPopUp() {
var txtItemSearch = document.getElementById('<%=txtName.ClientID%>');
var mywindow = window.open("AvailableContact.aspx?UserId=" + txtItemSearch.value , "mywindow", "location=1,status=1,scrollbars=1,width=600,height=400");
mywindow.opener = self;
mywindow.moveTo(0, 0);
mywindow.focus();
}
function processPopupValue(val) {
//Do your thing
alert(val);
document.getElementById('<%=txtName.ClientID%>').value = val1;
}
 </script>

</head>
<body>
 <form id="form1" runat="server">
     <div>
         <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
         <asp:Button ID="btnContact" runat="server" Text="Get Contact" OnClientClick="myPopUp()" />
     </div>
 </form>
</body>
</html>
2.Now Create Child.aspx Page and Populate the gridview . Child.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AvailableContact.aspx.cs"
   Inherits="AvailableContact" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
   <title>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
       <div>
           <asp:GridView ID="GridView1" runat="server" DataKeyNames="AddressId" OnRowDataBound="GridView1_RowDataBound">
               <Columns>
                   <asp:TemplateField>
                       <ItemTemplate>
                           <asp:HyperLink ID="hypReturn" NavigateUrl="javascript:void(0);" runat="server">Select</asp:HyperLink>
                       </ItemTemplate>
                   </asp:TemplateField>
               </Columns>
           </asp:GridView>
       </div>
   </form>
</body>
</html>
Child.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class AvailableContact : System.Web.UI.Page
{
    ContactBAL objCon = new ContactBAL();
    ContactBO objConBo = new ContactBO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Bind Grid View
        }

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink lnk = e.Row.FindControl("hypReturn") as HyperLink;
            string data = GridView1.DataKeys[e.Row.DataItemIndex].Value.ToString();
            DataRowView dv = (DataRowView)e.Row.DataItem;
            string data = dv[2].ToString();

            lnk.Attributes.Add("onclick", "window.opener.processPopupValue('" + data + "'); window.close();");

        }

    }
}

Comments

Popular posts from this blog