views:

314

answers:

3

I have this website that has a MasterPage and some content pages. The MasterPage contains a div that I pop up when the user clicks a 'Log In' link that is also located on the MasterPage. This login form has two textboxes, one for user name and one for password, and when the user submits the form they should be authenticated and there user info inserted in the session. ALL of the code to handle is is located in the MasterPage code behind.

Here is the issue: When the MasterPage click event grabs the text from the textboxes to use in the authentication method, the code sees the text as empty!!!

What am I missing?

Here is the button click code and form that is located on the master page:

ButtonClick:

bool IsUserAuthenticated = false;
        string userName = txtUserName.Text;
        string password = txtPassword.Text;

        IsUserAuthenticated = masterDataManager.AuthenticateUser(userName, password);

        if (IsUserAuthenticated)
        {
            Session.Add("UserName", userName);
            lblCurrentUserName.Text = "You are logged in as: " + userName;

            string script = "<script type=\"text/javascri\">$('#dialogBox').dialog('close');</script>";

            if (Page.ClientScript.IsStartupScriptRegistered("closeDialog"))
            {
                Page.RegisterStartupScript("closeDialog", script);
            }
        }
        else
        {
            lblLoginError.Text = "Username/Password combination does not exist in the database. Please try again.";
        }

And here is the Div:

<div id="dialogBox" title="Log In">
            <table align="center" style="margin-top: 5px; width: 100%" cellpadding="5" cellspacing="5">
                <tr>
                    <td colspan="2">
                        <asp:Label ID="lblLoginError" runat="server" Text="" CssClass="errorText"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td class="dialogText">
                        <asp:Label ID="lblUserName" runat="server" Text="User Name: " ForeColor="#c3c003"></asp:Label>
                    </td>
                    <td class="dialogInput">
                        <asp:TextBox ID="txtUserName" runat="server" BackColor="Black" ForeColor="#c3c003" Width="210px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="dialogText">
                        <asp:Label ID="lblPassword" runat="server" Text="Password: " ForeColor="#c3c003"></asp:Label>
                    </td>
                    <td class="dialogInput">
                        <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" BackColor="Black" ForeColor="#c3c003" Width="210px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="dialogInput" colspan="2">
            <asp:UpdatePanel runat="server" ID="testupdatepanel">
            <ContentTemplate>
                        <asp:ImageButton ID="btnLogin" runat="server" ImageUrl="~/images/loginout.png" ImageAlign="Right" style="padding-top: 15px;" onclick="btnLogin_Click" />
                        <asp:ImageButton ID="btnCancel" runat="server" ImageUrl="~/images/cancelout.png" ImageAlign="Right" style="padding-top: 15px;" />
            </ContentTemplate>
            </asp:UpdatePanel>
                    </td>
                </tr>
            </table>
        </div>

THe txtUserName and txtPassword Text properties are empty though there is text in the box.

Let me know if you need anything else.

A: 

Do the textboxes have the runat="server" attribute?

EDIT: Does it work if you put the login button outside of the updatepanel?

ZippyV
Yes it does. As a matter of fact, it will not compile without it.
The Sheek Geek
A: 

It's hard to guess without code but here are a couple anyway:

  • Check at what point in the page lifecycle you're attempting to access the textbox contents.
  • Check you're not resetting the textboxes in any way.
TreeUK
To add to TreeUK's response, you may be resetting the textbox contents in the page load event, not checking for postback, but again, hard to say without seeing the code.
Steve
+2  A: 

Try moving the update panel so that it encompasses the textboxes you're trying to post back.

When you're using an update panel, you're generally defining the elements within it that you want to post back.

TreeUK