Thursday, December 31, 2009

Calling an External Page into ModelPopup from GridView

In this post the ModalPopup extender is used to pop open an external ASPX page inside GridView,
Here are the steps:

1: Create new ASPX page and add The following controls // call this page MyPage.aspx
a:- GridView to handle data // call is GridView1
b:- Panel to open the external page inside it // call it pnlEdit
c:- LinkButton in GridView to Open The ModelPoup // call it lnkEdit
d:- ModalPopupExtender to popup the Panel // call it MPEEditPopup
e:- Iframe to open the external page and make it as server control // call it ifrEditPopup
f:- HIDDEN LinkButton to virtualy open the popup // call it lnkHidden
g:- DONT FORGET to add ScriptManger

Look to the following code which contains all controls we need

        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                <asp:BoundField DataField="ID" HeaderText="ID" />
                <asp:BoundField DataField="Description" HeaderText="Description" />
                <asp:TemplateField HeaderText="Edit">
                        <asp:LinkButton ID="lnkEdit" CommandName="OpenEditPopup" runat="server">Edit</asp:LinkButton>
        <asp:Panel Style="display: none" CssClass="modalPopup" ID="pnlDetail" runat="server">
            <table cellpadding="0" cellspacing="0">
                    <td align="right">
                        <asp:Button Text="Close Popup" ID="btnClose" runat="server"  />
                        <iframe id="ifrEditPopup" runat="server" scrolling="no" width="250px" height="140px"
        <ajaxToolkit:ModalPopupExtender ID="MPEEditPopup" runat="server" TargetControlID="lnkHidden"
            PopupControlID="pnlDetail" CancelControlID="btnClose" BackgroundCssClass="modalBackground"
            DropShadow="false" />
        <asp:LinkButton ID="lnkHidden" Style="visibility: hidden" runat="server">Hidden</asp:LinkButton>

The page design will be like this:

2:- In code-behind add the following code to fill grid with data

protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
GridView1.DataSource = LoadDataFromDB(); // This function will retrive data from DB to fill grid;
// to reset the src attributes for IFrame control
ifrEditPopup.Attributes["src"] = "";


3:-Create New Page and call it EditPage.aspx and write anything in this page e.g: This is EDIT Page

4:-In code-behind for The first page add OnRowCommand event to write the code to open the popup, look to the following code


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
if (e.CommandName == "OpenEditPopup")
// for IFrame control add new src as new attribute and give it the path for EditPage.aspx
// what is t ??? I've added Ticks as Query String just to avoid cookie problem when trying to open the popup again
ifrEditPopup.Attributes.Add("src", "EditPage.aspx?t=" + DateTime.Now.Ticks);
// call Show method to open the popup

5:- now run the project, the result will be like this:

   we left the src attribute for IFrame control empty, to avoid loading EditPage.aspx more than one time when loading MyPage.aspx, e.g: if the GridView contains 10 rows then the system will create 10 Iframe controls and for each control the system will call the EditPage, therefore the best solution is to leave the src attribute balnk and fill it with the URL in OnRowCommand event when the user Click on Edit link

Mohammad Al-Shafei 

No comments:

Post a Comment