Parsing error from ASP.Net page when trying to execute javascript Alert

Greetings all

I am have a user control that sends an email. On successful send, I want to displaya message. I am using "Response.Write("<script>alert('Your email has been sent.')</script>");" but this gives me an error - "PageRequestManagerParserErrorException" (plse see attached jpg).

USerControl source and code-behind are attached.

What am I mising?

In advance, thanks!!

    allanmark



USER CONTROL SOURCE:
 
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MimeEmailer.ascx.cs" Inherits="UserControls_MimeEmailer" %>
 
<script type ="text/javascript" language="javascript">
 
 function ClearTheItXYZ()
{
   document.getElementById('<%=txtSubject.ClientID %>').value = '';
   document.getElementById('<%=txtMessage.ClientID %>').value = '';   
   var control = document.getElementById('<%=txtSubject.ClientID %>');
   if( control != null ){ control.focus(); }
   return false;
}
 
</script>      
 
<ajaxext:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
</ajaxext:ScriptManagerProxy>
 
<ajaxext:UpdatePanel ID="UpdatePanelA" runat="server">
<ContentTemplate>
    <asp:Panel ID="pnlMail" runat="server" CssClass="pnlEMail" Width="600" BackColor="Lime">
    <br />
    
    <table id="email" style="position: relative" >
 
       <!-- To -->
       <tr>
            <td style="width: 75px">
                <asp:Label ID="lblTo" runat="server" CssClass="smallerFontSize">To:</asp:Label>
                </td>
                <td colspan="2">
                <asp:DropDownList ID="ddlAddresses" runat="server" Width="325px"></asp:DropDownList>                
            </td>
       </tr>      
       
        <tr>                
        <!-- Row: Blank  -->
            <td colspan="2" height="12"></td>
        </tr>
 
 
       <!-- Row: Subject -->
       <tr>                
            <td style="width: 75px">
		        <asp:Label ID="lblSubject" runat="server" CssClass="smallerFontSize" AssociatedControlID="txtSubject" Width="175px" >Subject:</asp:Label>
            </td>
            <td colspan="2">
                <asp:TextBox ID="txtSubject" runat="server" Width="350px"></asp:TextBox>
	    	    <asp:RequiredFieldValidator ID="rfvSubject" runat="server" CssClass="smallFontSize" ControlToValidate="txtSubject"
                    ErrorMessage="Required Field" SetFocusOnError="True" Style="position: relative" ValidationGroup="SendEmail"></asp:RequiredFieldValidator></td>            
    	<!-- End: Row: Subject -->
        </tr>
 
        <tr>                
        <!-- Row: Blank  -->
            <td colspan="2" height="12"></td>
        </tr>
            
        <tr>                
        <!-- Row: Message -->
            <td style="width: 75px">
    		    <asp:Label ID="lblMessage" runat="server" CssClass="smallerFontSize" AssociatedControlID="txtMessage">Message:</asp:Label>
            </td>
            <td colspan="2">
		        <asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine" Width="475px" Height="200px"></asp:TextBox>
		        <asp:RequiredFieldValidator ID="rfvMessage" runat="server" CssClass="smallFontSize" ControlToValidate="txtMessage"
                    ErrorMessage="Required Field" SetFocusOnError="True" Style="position: relative" ValidationGroup="SendEmail"></asp:RequiredFieldValidator>        
            </td>            
	    <!-- End: Row: Message -->
        </tr>
 
        <tr>                
	    <!-- Row: Blank  -->
            <td colspan="2" height="1"></td>
        </tr>
              
    <!-- End: table id="email". -->
    </table>     
    
    <center>
    <asp:Button ID="btnSendTheEMail" runat="server"  Text="Send" ValidationGroup="SendEmail" CssClass="buttons" OnClick="btnSendTheEMail_Click" />  
    &nbsp;&nbsp;&nbsp;
    <asp:Button ID="btnClear" runat="server" Text="Clear" OnClientClick="return ClearTheItXYZ()" CausesValidation="False" />        
    </center><br />
 
 
    </asp:Panel>
    <!-- <asp:Panel ID=pnlMail"> -->
 
</ContentTemplate>        
</ajaxext:UpdatePanel>
 
 
 
USER CONTROL CODE BEHIND:
 
using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataTransferObjects;
using BusinessLogic;
 
 
public partial class UserControls_MimeEmailer : System.Web.UI.UserControl
{
 
    private List<EMailAddress> emailList = new List<EMailAddress>();
 
 
    protected void Page_Load(object sender, EventArgs e)
    {
        // Load the Page-specific stylesheet.
        //HtmlLink lnk = (HtmlLink)Master.FindControl("cssLink");
        HtmlLink lnk = new HtmlLink();
        lnk.Href = "~/CSS/UserControls.css";
 
        //        txtMessage.Attributes.Add("OnClientClick", "return false;");
 
        if (!Page.IsPostBack)
        {
            emailList = BusinessLogic.Business.GetEmailAddress();
            ddlAddresses.DataSource = emailList;
            ddlAddresses.DataTextField = "Name";
            ddlAddresses.DataValueField = "Email";
            ddlAddresses.DataBind();
 
            ddlAddresses.SelectedIndex = 0;
            Session["selectedEmail"] = ddlAddresses.SelectedIndex;
 
        }
        else
        {
            if (Session["selectedEmail"] != null)
            {
                ddlAddresses.SelectedIndex = (int)Session["selectedEmail"];
            }
            else
            {
                ddlAddresses.SelectedIndex = 0;
                Session["selectedEmail"] = ddlAddresses.SelectedIndex;
            }
        }
    }
 
 
    protected void btnSendTheEMail_Click(object sender, EventArgs e)
    {
        bool isEmailSent = BusinessLogic.Business.SendEmail(ddlAddresses.SelectedValue.ToString(),
                                                       System.Configuration.ConfigurationManager.AppSettings["AdminEmail"].ToString(),
                                                       txtSubject.Text,
                                                       txtMessage.Text + Environment.NewLine + Environment.NewLine);
        if (isEmailSent)
        {
 
            Response.Write("<script>alert('Your email has been sent.')</script>");
                  ClearTheFields();
        }
        else
        {
            //   Message to go here.     
        }
    }
 
    private void ClearTheFields()
    {
        ddlAddresses.SelectedIndex = 0;
        txtSubject.Text = "";
        txtMessage.Text = "";
        txtSubject.Focus();
    }
 
 
}

Open in new window

Error.jpg
allanmarkAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
allanmarkAuthor Commented:
Good articles!!

So anything with a Resonse.write in it is not going to work!  I need to inform the user in some way (was hoping to do so via a message popped up) - is there a way to do this?

0
 
AlvinLim84Connect With a Mentor Commented:
Not really must remove the response.write(). You can try to move the button name 'btnSendTheEMail' outside of the Panel such as the following part in aspx page. It should be working as expected and popped up the message to user.
<!-- End: table id="email". -->
    </table>     
 
    </asp:Panel>
    <!-- <asp:Panel ID=pnlMail"> -->
 
</ContentTemplate>        
</ajaxext:UpdatePanel>
 
<center>
    <asp:Button ID="btnSendTheEMail" runat="server"  Text="Send" ValidationGroup="SendEmail" CssClass="buttons" OnClick="btnSendTheEMail_Click" />  
    &nbsp;&nbsp;&nbsp;
    <asp:Button ID="btnClear" runat="server" Text="Clear" OnClientClick="return ClearTheItXYZ()" CausesValidation="False" />        
    </center><br />

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
allanmarkAuthor Commented:
Bummer!

The UserControl is on a panel that is the TargetControl for an AJAX ModalPopup. If the "Send" button is outside the UpdatPanel then it causes the popup to close!
0
 
AlvinLim84Commented:
Sorry. I didn't know it is Modalpopup. It is always better to use RegisterStartupScript rathen than Response.write method when you want to execute javascript command after the button postback event. Replace Response.Write with the following code. I hope this help.

ClientScript.RegisterStartupScript(typeof(String), "", "<script>alert('Your email has been sent.')</script>");
0
 
GiftsonDJohnConnect With a Mentor Commented:
Use as follows.

ScriptManager.RegisterClientScriptBlock(btnSendTheEMail, btnSendTheEMail.GetType(), "mailsent", "alert('Your email has been sent.')", true);
0
 
allanmarkAuthor Commented:
Many thanks!!!
0
All Courses

From novice to tech pro — start learning today.