Solved

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

Posted on 2009-04-07
7
451 Views
Last Modified: 2013-12-17
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
0
Comment
Question by:allanmark
  • 3
  • 3
7 Comments
 
LVL 2

Accepted Solution

by:
AlvinLim84 earned 175 total points
ID: 24084706
0
 

Author Comment

by:allanmark
ID: 24084809
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
 
LVL 2

Assisted Solution

by:AlvinLim84
AlvinLim84 earned 175 total points
ID: 24084896
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:allanmark
ID: 24085174
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
 
LVL 2

Expert Comment

by:AlvinLim84
ID: 24085590
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
 
LVL 14

Assisted Solution

by:GiftsonDJohn
GiftsonDJohn earned 75 total points
ID: 24085601
Use as follows.

ScriptManager.RegisterClientScriptBlock(btnSendTheEMail, btnSendTheEMail.GetType(), "mailsent", "alert('Your email has been sent.')", true);
0
 

Author Closing Comment

by:allanmark
ID: 31567386
Many thanks!!!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now