Login PopUp Windows

6 posts, 0 answers
  1. Jet
    Jet avatar
    6 posts
    Member since:
    Dec 2010

    Posted 13 Dec 2010 Link to this post

    Hello!
    I'd been searching for a couple of days to find a solution to what is think is a "simple" problem, that due to my lack of knowledge is toooo hard to solve it without help.
    What i need to accomplish is:
    Open a RadWindow with my login page, 
    Get the login and password
    Authenticate
    -if succeed: redirect
    -if fails: display a message ( i have label for that purpose) and keep the RadWindow open.

    My problem is that i can't find a way to process the authentication and automatically close the window and redirect.
    Because client-side functions run before server-side, i can't perform the authentication in my db and then execute the JavaScript functions with the same button. I know I can look for a javascript snippet to perform the authentication, but i don't want to take that path 'cause at this point i'm blind to javascript. so i want to reduce it at the very minimum possible. I'd tried the techniques to execute JS functions from code-behind with no luck. Even run sever-side functions from JS but i just can't make it work.
    What i have so far:
    Parent Page
    <script language="javascript" type="text/javascript">
            function OpenLogin()
             {
               var wnd = $find("<%=dlgLogin.ClientID%>");
                    wnd.setUrl("Login.aspx");
                    wnd.show();
                }
                function IsOk(wnd, Arg) {
                   var args = Arg.get_argument();
                    if (args) {
                        window.location = "CustomerStatus.aspx";
                    }
                }     
        </script>
    <script language="javascript" type="text/javascript">
            function OpenLogin()
             {
               var wnd = $find("<%=dlgLogin.ClientID%>");
                    wnd.setUrl("Login.aspx");
                    wnd.show();
                }
                function IsOk(wnd, Arg) {
                   var args = Arg.get_argument();
                    if (args) {
                        window.location = "CustomerStatus.aspx";
                    }
                }    
        </script>

    Child Page aspx
    <script language="javascript" type="text/javascript">
        
        function GetRadWindow() {
            var oWindow = null;
            if (window.radWindow) oWindow = window.radWindow;
            else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
            return oWindow;
        }
        function CancelWindow() {
           var wnd = GetRadWindow();
            wnd.close();
        }
        function CloseWndw() {
            var arg = document.getElementById("txtIsOk").value;
            if (arg) {
                var wnd = GetRadWindow();
                wnd.close(arg);
            }
        }
        function Waiting(s, e)
            {
                Callback.PerformCallback();
                LPWaitConn.Show();
     
            }
                           
    </script>

    The  hidden-Field and RadButtons
     <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
           
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
     
    <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
            
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
    <asp:HiddenField ID="txtIsOk" runat="server" />
     <telerik:RadButton ID="btnLogin" runat="server" Text="Submit" Style="z-index: 1;
                    left: 243px; top: 134px; position: absolute; height: 19px" BackColor="Transparent"
                    Font-Names="Verdana" Font-Size="10pt" ForeColor="#84DA2E" SkinID="Black" OnClientClicking="Waiting"
                    OnClick="btnLogin_Click" OnClientClicked="CloseWndw">
                </telerik:RadButton>
                 
                <telerik:RadButton ID="btnCancel" runat="server" Text="Cancel" Style="z-index: 1;
                    left: 180px; top: 134px; position: absolute; height: 19px" BackColor="Transparent"
                    Font-Names="Verdana" Font-Size="10pt" ForeColor="#84DA2E" Font-Bold="True" Skin="Black"
                    EnableViewState="False" OnClientClicked="CancelWindow">
                </telerik:RadButton>
    </ContentTemplate>
        </asp:UpdatePanel>

    <script language="javascript" type="text/javascript">
            function OpenLogin()
             {
               var wnd = $find("<%=dlgLogin.ClientID%>");
                    wnd.setUrl("Login.aspx");
                    wnd.show();
                }
                function IsOk(wnd, Arg) {
                   var args = Arg.get_argument();
                    if (args) {
                        window.location = "CustomerStatus.aspx";
                    }
                }     
        </script>
    Code-Behind
    Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
           Dim strCon As String = String.Empty
           Dim cnn As SqlConnection
     
           If Me.txtIsOk.Value.Length > 0 Then Exit Sub 'Because of the forced postback this is avoid more than one execution
     
           If Me.txtUser.Text.Length > 0 AndAlso Me.txtPass.Text.Length > 0 Then
               strCon = "Data Source=MyServer;Initial Catalog=MyDb;User Id=" & Me.txtUser.Text & ";Password=" & Me.txtPass.Text & ";"
               cnn = New SqlConnection(strCon)
               Try
                   cnn.Open()
                   Me.txtIsOk.Value = "Yes"
               Catch ex As SqlException
                   Me.lblWarning.Text = "Wrong Login or Password"
                   Me.lblWarning.Visible = True
               Catch ex1 As Exception
                   Me.lblWarning.Text = "Wrong Login or Password"
                   Me.lblWarning.Visible = True
               Finally
                   cnn.Close()
                   cnn.Dispose()
               End Try
           End If
       End Sub
    The problem with my code is that i have to click twice in the "btnLogin".
    In the first click everything will run, but the client-side will execute first and nothing will happen in that side. At the same time if credentials are ok the hidden field will be populated.
    In the second click: Again everything will run this time as expected.

    For any help
    Thanks in advance




  2. Cori
    Cori avatar
    562 posts
    Member since:
    Jul 2010

    Posted 13 Dec 2010 Link to this post

    Hello Jet,

    First, you shouldn't hookup the OnClientClicked event because it gets called regardless if the login failed or succeeded. Second, since your using the RadButton inside of an UpdatePanel, set UseSubmitBehaviour="false", because of some ajax issues it has.

    Now with that out of the way, to close the window when the login succeeded, try this:

    'close window
    Page.ClientScript.RegisterStartupScript(Page.GetType(), "CloseWndw", "CloseWndw();", true)

    Add that line to where you set the hidden fields value and it will register a script that runs once the postback is finished to close the window.

    I hope that helps.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jet
    Jet avatar
    6 posts
    Member since:
    Dec 2010

    Posted 13 Dec 2010 Link to this post

    G'd Afternoon Cori!
    First of all thanks a lot for your kind and quick reply. Unfortunately it didn't work. I'm know your solution would work if  i just can find the way to make the Page.ClientScript.RegisterStartupScript execute.
    I don't know what's wrong with my page/project, but no matter what i do i can't register any JS code.
    i even place a js function to show an alert window, just to check if it registers, but no luck.
    I did exactly as you suggested, but it didn't work.
    Here is the test code:
    Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
            Dim strCon As String = String.Empty
            Dim cnn As SqlConnection
     
            If Me.txtIsOk.Value.Length > 0 Then Exit Sub
     
            If Me.txtUser.Text.Length > 0 AndAlso Me.txtPass.Text.Length > 0 Then
                strCon = "Data Source=MyServer;Initial Catalog=MyDb;User Id=" & Me.txtUser.Text & ";Password=" & Me.txtPass.Text & ";"
                cnn = New SqlConnection(strCon)
                Try
                    cnn.Open()
                    Session("CustomerId") = Me.txtUser.Text
                    Me.txtIsOk.Value = "Yes"
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "CloseWndw", "CloseWndw();", True)
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "ShowIt", "ShowIt();", False)
                Catch ex As SqlException
                    Me.lblWarning.Text = "Wrong Login or Password"
                    Me.lblWarning.Visible = True
                Catch ex1 As Exception
                    Me.lblWarning.Text = "Wrong Login or Password"
                    Me.lblWarning.Visible = True
                Finally
                    cnn.Close()
                    cnn.Dispose()
                End Try
            End If
        End Sub
    aspx
    function Showit() {
            alert("Oops");
        }
         
     <telerik:RadButton ID="btnLogin" runat="server" Text="Submit" Style="z-index: 1;
                    left: 243px; top: 134px; position: absolute; height: 19px" BackColor="Transparent"
                    Font-Names="Verdana" Font-Size="10pt" ForeColor="#84DA2E" SkinID="Black" OnClientClicking="Waiting"
                    OnClick="btnLogin_Click" UseSubmitBehavior="false" >
                </telerik:RadButton>

  5. Jet
    Jet avatar
    6 posts
    Member since:
    Dec 2010

    Posted 13 Dec 2010 Link to this post

    Hello Again!
    I finally solved my problem, thanks to Cory to put me in the right track.
    After removing the "OnClientClicked" in the login button (the submit behavior in my case is optional) the only thing i had to change
    was
     ScriptManager.RegisterStartupScript(Me.Page, Page.GetType(), "CloseWndw", "CloseWndw();", True)
    Instead of
    Page.ClientScript.RegisterStartupScript(Page.GetType(), "CloseWndw""CloseWndw();"True)
    To my surprise when using Ajax the ClientScript doesn't work. The only way it works (for me) is with the ScriptManager.
    I hope it helps


  6. Jonx
    Jonx avatar
    258 posts
    Member since:
    Jul 2012

    Posted 02 Jan 2011 Link to this post

    Hello Jet,
    Thanks for pointing this out.

    I was having a hard time using a FormView inside a RadWindow Popup.
    I was using a RadButton to Update or Save my data throught an EntityDatasource.

    I used the samples you can find here using a DetailsView.
    Once I used a FormView the sample was not working anymore.

    Even when using something else than a RadButton like normal button or hyperlink the Popup Windoow refused to close.
    The script closing the popup registered through RegisterStartupScript was never called.

    The solution is like you said, change Page.ClientScript.RegisterStartupScript to ScriptManager.RegisterStartupScript:
    protected void FormView1_ItemCommand1(object sender, FormViewCommandEventArgs e)
    {
                if (e.CommandName == "Update")
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "mykey", "CloseAndRebind();", true);
                    //ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndRebind();", true);
                }
    }

    I'm adding my post to your thread for other people to find it easier.

    Thanks again, you saved my day!

    John.
  7. Jet
    Jet avatar
    6 posts
    Member since:
    Dec 2010

    Posted 03 Jan 2011 Link to this post

    Happy New Year John,
    I'm so glad you could solve your problem, and thanks for adding your post. I'm sure our solution will help others with the same issue.

    Happy new year for every1!!.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017