Click here to Skip to main content
15,881,248 members
Articles / Web Development / HTML
Article

Populating Parent Grid from Popup Window Using Ajax

Rate me:
Please Sign up or sign in to vote.
2.40/5 (2 votes)
20 Nov 2007CPOL2 min read 51.6K   637   30   4
This article discusses a way of communicating to a web page from its popup or child window, both on a server and client level. One way a grid is populated from a popup: when editing rows in a grid, you show the edited records in the parent simutaneously without postback from a child popup window.

Introduction

I saw a lot of requests for getting a control over the parent web page from a pop page, so I decided to write this article. This article explains how to call a parent server level function from a popup window using AJAX.

For this I am using the population of a grid as an example. Here I have a datagrid which contains buttons for editing each row. When I click the button, another popup (popup.aspx) pops up containing the fields of the selected row.

After editing the row the grid will refresh from the popup window without posting back to the server. I used the Ajax functionality to do this.

Using the Code

Here am using two ASPX pages.

  1. Default.aspx
  2. popup.aspx

I am maintaining a hidden button (btnReload) just to fill the grid. On loading the function of the default.aspx I am populating the grid from Northwind database.

VB
Private Function FillGrid() As Boolean 

    Dim Sqlstring As String = " select top 10 customerID,companyname," +
        "contactname from customers" 
    Dim Myreader As SqlDataReader = Nothing 
    Connect_SQLDataReader(Myreader, Sqlstring, errmsg) 
    grid.DataSource = Myreader 
    grid.DataBind() 

End Function

And on the Onload event of the default page I am registering a JavaScript function ReloadPage() which will refer to the reload button click event. This does the trick.

VB
Protected Sub Page_Load(ByVal sender As Object,
    ByVal e As System.EventArgs) Handles Me.Load 

ScriptManager.RegisterStartupScript(Me.btnReload, Me.GetType, "xxxxx", 
    "function ReloadPage(){" & Page.ClientScript.GetPostBackEventReference(
    Me.btnReload, Nothing) & "}", True) 
If Page.IsPostBack = False Then 
FillGrid() 
End If 

End Sub

Please note you have to do this outside the Page.IsPostBack condition because we have to register the script for every post for stabilityEnsure, that you had put the grid in the Ajax Update Panel.

While clicking on the link button of the grid, I am popping up the new ASPX page popup.aspx, where I am taking the query string of the rowID and populating what is textboxed for editing purposes.

VB
Protected Sub Page_Load(ByVal sender As Object,
    ByVal e As System.EventArgs) Handles Me.Load 

If Page.IsPostBack = False Then 
Dim Sqlstring = "select companyname,
    contactname from customers where customerID='" & 
    Request.QueryString("id") & "'" 
Dim Myreader As SqlDataReader = Nothing 
Connect_SQLDataReader(Myreader, Sqlstring, errMsg) Myreader.Read() 

txtCompanyName.Text = Myreader(0).ToString() 
txtContactName.Text = Myreader(1).ToString() 
Myreader.Close() 

End If 

End Sub

Now, upon clicking the save button in the popup window I am editing the data to the backend and calling a JavaScript function, ReloadParent().

VB
Protected Sub Button1_Click(ByVal sender As Object,
    ByVal e As System.EventArgs) Handles Button1.Click 

Dim Sqlstring = "update customers set companyname='" & 
    txtCompanyName.Text & "', contactname='" & txtContactName.Text & 
    "' where customerID='" & Request.QueryString("id") & "'" 
Dim result As Integer 

Try

 Connect_SQLNonQuery(result, Sqlstring, errMsg) 

Catch ex As Exception 

End Try 

ScriptManager.RegisterStartupScript(Me.Page, GetType(String), "Success",
    "ReloadParent('Records Updated')", True) 

End Sub

Let's see what the ReloadParent() does.

VB
function ReloadParent(msg) 
{
    var opener;
    if (window.opener){opener = window.opener;} else{opener = 
       window.dialogArguments;
} 
opener.ReloadPage();
    alert('Records Updated'); //self.close(); 
}

Here I am calling the parent JavaScript fucnction opener.ReloadPage(); which will call the btnReload click event of the parent page. In the btnReload we just call the Fillgrid() function like this:

VB
Protected Sub btnReload_Click(ByVal sender As Object,
    ByVal e As System.EventArgs)
FillGrid()

End Sub

License

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


Written By
Software Developer (Senior) Emirates Group
United Arab Emirates United Arab Emirates
I am working as a Senior .NET Developer in Emirates Group(Mercator) ,Dubai
Expertise in C,C++,C#,VB,.NET,Ajax,XML,Web Service,Syncfusion..etc

Comments and Discussions

 
QuestionNot working with script manager Pin
kkbkrishna4-Apr-08 2:17
kkbkrishna4-Apr-08 2:17 
GeneralCheers Pin
TeaTime29-Nov-07 4:02
TeaTime29-Nov-07 4:02 
QuestionException Handling Pin
Pmdls26-Nov-07 22:37
Pmdls26-Nov-07 22:37 
Hi There!
I did enjoy your article but I have a question: in method Button1_Click, shouldn't we use the Catch clause, from the Try Catch, in the end of the method, after the ScriptManager.RegisterStartupScript call ? The way you wrote this method, if the sql returns an error, you still have Success in the Parent, right?

Shouldn't it be like this:

Protected Sub Button1_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Button1.Click

Dim Sqlstring = "update customers set companyname='" &
txtCompanyName.Text & "', contactname='" & txtContactName.Text &
"' where customerID='" & Request.QueryString("id") & "'"
Dim result As Integer

Try

Connect_SQLNonQuery(result, Sqlstring, errMsg)

ScriptManager.RegisterStartupScript(Me.Page, GetType(String), "Success",
"ReloadParent('Records Updated')", True)

Catch ex As Exception

End Try

End Sub

Thanks and keep the good development! Suspicious | :suss:
AnswerRe: Exception Handling Pin
janeshh27-Nov-07 0:14
janeshh27-Nov-07 0:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.