ASP.Net: How To Close Modalpopup And Refresh Parent Page


I've a parent page with a long form. I am opening ajax modal popup extender on the click of a link button.

Default.aspx

[Code]....

In login.aspx, I've another  form. When the submit button is clicked, I am doing some processing, after that need to close the modal popup window and refresh my parent page, without disturbing the session  already created in parent page.

login.aspx


[Code]....

Parent.Page.Response.Redirect("default.aspx")

Solution: Earlier i was using iframe to load the login.aspx inside modal popup. Now i have switched to another concept of converting webform to control. As follows: 

To convert a code-behind ASP.NET Web page into a user control

  1. Rename the .aspx file so the file name extension is .ascx.
  2. Rename the code-behind file to have the file name extension .ascx.vb or .ascx.cs, depending on what programming language the code-behind file is in.
  3. Open the code-behind file and change the class from which it inherits from Page to UserControl.
  4. In the .aspx file, do the following:
    1. Remove the htmlbody, and form elements from the page.
    2. Change the @ Page directive to an @ Control directive.
    3. Remove all attributes of the @ Control directive except LanguageAutoEventWireup (if present), CodeFile, and Inherits.
    4. In the @ Control directive, change the CodeFile attribute to point to the renamed code-behind file.
  5. Include a className attribute in the @ Control directive. This allows the user control to be strongly typed when it is added to a page.
    Example:

    My earlier login.aspx and login.vb file were like this:


                  <%@ Page Language="VB" AutoEventWireup="false" CodeFile="login.aspx.vb" Inherits="login" %>


    <!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 runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:Label ID="Label1" runat="server" Text="Username:"></asp:Label>
     <asp:TextBox ID="txtName" runat="server" MaxLength="50"></asp:TextBox>
            <br />
            <asp:Label ID="Label2" runat="server" Text="Password:"></asp:Label>
     <asp:TextBox ID="txtPwd" runat="server" MaxLength="50" TextMode="Password"></asp:TextBox>
            <br />
            <br />
     <asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="108px" />
            <br />
            <asp:Label ID="lblInfo" runat="server" Text="Label"></asp:Label>
        
        </div>
        </form>
    </body>
    </html>


    And code behind was:



    Imports dbOperation
    Partial Class login
        Inherits System.Web.UI.Page


        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
              
                If Request.Params("login") = 1 Then
                    Exit Sub
                End If
                If Request.Params("logout") = 1 Then


                    Session.Clear()
                    Session.Abandon()


                End If
               
        End Sub


        Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
            Dim myquery = "SELECT distinct username,first_name,last_name FROM users u,groups g,group_users gu WHERE u.userid = gu.userid and g.gid = gu.gid and username='" & txtName.Text.Trim & "' and password=md5('" & txtPwd.Text.Trim & "') and g.name like ('%')"
            Dim result() As String = authenticateUser(myquery)
            If result(0) = "ok" Then
                lblInfo.Text = "Logged in"
                Session("eid") = txtName.Text.Trim
                Session("ename") = result(2) & " " & result(3)
           Else
                lblInfo.Text = "Wrong Password. Please Try Again. Account shall be locked after 6 wront attempts." & result(0).ToString


            End If


        End Sub
    End Class


    Now i converted it to a control named logme.ascx

<%@ Control Language="VB"  CodeFile="logme.ascx.vb" classname="logme" Inherits="_logme" %>


    <div>
    <table border=0><tr><td>
        <asp:Label ID="Label1" runat="server" Text="Username:"></asp:Label></td><td>
 <asp:TextBox ID="txtName" runat="server" MaxLength="50"></asp:TextBox>
        <br /> </td></tr>
      <tr><td>  <asp:Label ID="Label2" runat="server" Text="Password:"></asp:Label> </td><td>
 <asp:TextBox ID="txtPwd" runat="server" MaxLength="50" TextMode="Password"></asp:TextBox>
        </td></tr>
       <tr> <td></td><td>
 <asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="108px" />
        </td>
        </tr></table>
        <br />
        <asp:Label ID="lblInfo" runat="server" Text="Label"></asp:Label>
    
    </div>

and Code behind logme.ascx.vb

Imports dbOperation
Partial Class _logme
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
         
            If Request.Params("login") = 1 Then
                Exit Sub
            End If
            If Request.Params("logout") = 1 Then

                Session.Clear()
                Session.Abandon()

            End If
          End If
    End Sub

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim myquery = "SELECT distinct username,first_name,last_name FROM users u,groups g,group_users gu WHERE u.userid = gu.userid and g.gid = gu.gid and username='" & txtName.Text.Trim & "' and password=md5('" & txtPwd.Text.Trim & "') and g.name like ('%')"
        Dim result() As String = authenticateUser(myquery)
        If result(0) = "ok" Then
            lblInfo.Text = "Logged in"
            Session("eid") = txtName.Text.Trim
            Session("ename") = result(2) & " " & result(3)
           Parent.Page.Response.Redirect("default.aspx")
        Else
            lblInfo.Text = "Wrong Password. Please Try Again. Account shall be locked after 6 wrong attempts." & result(0).ToString

        End If

    End Sub
End Class

Thats It. Now use this control in default.aspx like this:
Before html tag declare the control

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<%@ Register TagPrefix="uc" TagName="log" Src="logme.ascx" %>

Use Inside the modalpopup

 <!--  popup -->
                  <div id="popup">
                  <asp:UpdatePanel ID="popupPanel" runat="server" >
                     <ContentTemplate>
              <div class="insidemodalpopup">   
                <asp:ImageButton ID="ibtnCancel" runat="server" ImageAlign="Right" 
                      ImageUrl="images/close.jpg" /><br />  <b>Help</b> <br /><br />
                  <asp:Label ID="lblPop" runat="server" Text="Label"></asp:Label> <br /><br />
                 <br />
                  
                  <uc:log ID="log1" runat=server Visible=false />
                  
             </div>  
   
                    </ContentTemplate>
                    </asp:UpdatePanel>
                    <asp:HyperLink ID="test" runat="server"></asp:HyperLink>
                    <asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" 
                          TargetControlID="test" PopupControlID="popupPanel" 
                          BackgroundCssClass="modalBackground" dropshadow="true" 
                          CancelControlID="ibtnCancel"></asp:ModalPopupExtender>
  
                  
                  </div>  <!-- end popup -->

the code behind in default.vb for link button who popups would be like this:

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
        Timer1.Enabled = False
           log1.Visible = True
        ModalPopupExtender1.Show()
    End Sub

- Vinod Kotiya
www.vinodkotiya.com

Comments