Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Opening Child window from Parent window after selecting data from child window closing the child window and displaying that data in parentwindow using asp.net

Aug 15, 2010

Here i will explain how to opening Child window from Parent window after selecting data from child window closing the child window and displaying that data in parent window using asp.net

Here based on my requirement i have taken one textbox and one image button i want to search for the email ids exists in database whenever the user clicks on the image button i need to open one child window like this



Now open the new website in default.aspx page write the following code in form tag


<table align="center">
<tr>
<td align="right">
<asp:Label ID="lblemail" Text="Email" runat="server"></asp:Label>
</td>
<td align="left">
<asp:TextBox ID="txtemailids" runat ="server" CssClass="textbox"></asp:TextBox>
</td>
<td>
<asp:ImageButton ID="imgadd" runat ="server" ImageUrl ="~/Images/add_btn.jpeg" Width="28px" Height="28px"
onclick="imgadd_Click" AlternateText="Add"/>
</td>
</tr>
</table>

In image button click write the follwing code

protected void imgadd_Click(object sender, ImageClickEventArgs e)
{  
Response.Write("<script>window.open('Closewindow.aspx','mywindow','width=400,height=400,left=300,top=150')</script>");
}
 Here see Closewindow.aspx is the child window page i have given height, width for the child window page.

Now right click on solution select Add New item select New website give name for that website Closewindow.aspx click ok.

Design the Closewindow.aspx page just like this
















Design Closewindow.aspx page with following code

<div>
<table align="center">
<tr>
<td colspan ="2">
<table>
<tr>
<td>
<asp:TextBox ID="txtsearch" runat="server" CssClass="textbox"></asp:TextBox>
</td>
<td>
<asp:ImageButton ID="imgbt1" runat="server" ImageUrl="~/Images/gosearch.gif" onclick="imgbt1_Click" AlternateText="Search"/>
</td>
</tr>
</table>  
</td>
</tr>
<tr>
<td colspan ="2">
<asp:GridView ID="gvrecords" runat="server" CellPadding="4" CssClass="grid" GridLines="None" Width="100%" AutoGenerateColumns ="false" CellSpacing ="2">
<RowStyle CssClass="itemstyle"/>
<PagerStyle CssClass="gridheader" HorizontalAlign="Center" />
<SelectedRowStyle CssClass="selecteditemstyle" />
<HeaderStyle CssClass="gridheader"/>
<AlternatingRowStyle CssClass="alternativeitemstyle"/>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="rdbmail" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Email" HeaderText ="Email" HeaderStyle-HorizontalAlign ="Left" />
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td colspan ="2">
<table>
<tr>
<td>
<asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" CssClass="button" />
</td>
<td>
<asp:Button ID="btnCancel" runat="server" Text="Cancel" onclick="btnCancel_Click" CssClass="button"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>

After that the Closewindow.aspx code behind contains the following code


protected void Page_Load(object sender, EventArgs e)
{
btnSave.Visible = false;
btnCancel.Visible = false;
}
protected void btnsearch_Click(object sender, EventArgs e)
{
}
protected void btnSave_Click(object sender, EventArgs e)
{
//ArrayList array = new ArrayList(100);
string[] str = new String[100];
int i = 0;
int count = 0;
foreach (GridViewRow rows in gvrecords.Rows)
{
RadioButton rdb = (RadioButton)rows.Cells[0].FindControl("rdbmail");
if (rdb.Checked)
{
str[i] = rows.Cells[1].Text;
i++;
count = 1;
}
if (count == 0)
{
Page.RegisterStartupScript("Usermsg", "<script language='javascript'>alert('Please select mailid')</script>");
}
}
for (int j = 0; j < i; j++)
{
txtsearch.Text = txtsearch.Text + str[j] + ";";
}
string s = txtsearch.Text;
Response.Write("<script language=javascript>window.opener.document.getElementById('txtemailids').value = '" + s + "';window.close();</script>");
}
protected void btnCancel_Click(object sender, EventArgs e)
{
Response.Write("<script>window.close()</script>");
}
protected void imgbt1_Click(object sender, ImageClickEventArgs e)
{
if (txtsearch.Text.Trim() != "")
{
string Search_text = txtsearch.Text;
SqlConnection con = new SqlConnection("user id=aaa;password=aaa;data source=111.32.12.32");
con.Open();
SqlCommand cmd = new SqlCommand("sp_searchrecordsbymail", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Search_text", Search_text);
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
gvrecords.DataSource = ds;
gvrecords.DataBind();
btnSave.Visible = true;
btnCancel.Visible = true;
txtsearch.Text = "";
}
else
{
Page.RegisterStartupScript("Usermsg", "<script language='javascript'>alert('No Records found')</script>");
}
}
else
{
Page.RegisterStartupScript("Usermsg", "<script language='javascript'>alert('Please Enter text')</script>");
}
}
 Here I am binding gridview based on search text given by user here u people need to change the SqlConnection give ur local path  and write the stored procedure "sp_searchrecordsbymail" otherwise bind the gridview directly with data from your database

I hope this helps you

Download sample code attached

 

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

2 comments :

shekar said...

Thank you Suresh for this type of articles.

Unknown said...

not working for me

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.