Search code examples
c#asp.netajaxajaxcontroltoolkitmodalpopupextender

How to show a error when a modal popup extender is active


I have a ModalPopupExtender to create new users. When the user enters the new user details and tries to enter the same email Id which is already registered then I want to show a error.

Create user:

  if (emailcount != 0)
            {
                
                Page.ClientScript.RegisterStartupScript(GetType(), "UserDialogScript", 

"alert(\"User already exists!\");", true);
            }

I check whether the email is already used, if it is used I want to pop up the error but the page stays still with the ModalPopupExtender on the top. I am not able to do anything after that. How can I display the error?


Solution

  • I would use a different approach for this. In your modal popup extender, try something like this:

    <asp:UpdatePanel ID="pnlUserDetails" runat="server">
        <ContentTemplate>
            <asp:TextBox ID="txtEmail" runat="server" OnTextChanged="txtEmail_OnTextChanged" AutoPostBack="true"></asp:TextBox>
            <asp:Label ID="lblEmailMessage" runat="server" Text="Already exists!" Visible="false" /> 
        </ContentTemplate>
    </asp:UpdatePanel>
    

    In the code-behind:

    protected void txtEmail_TextChanged(object sender, EventArgs e)
    {
        //check for matching email address and show label if match is found
        lblEmailMessage.Visible = FindMatchingEmailAddress(txtEmail.Text.Trim());    
    
        //clear the email input if a match is found??
        txtEmail.Text = String.Empty;
    }