Enrique Bravo Enrique Bravo - 1 month ago 13
Ajax Question

Event handling ignoring the param from URL

So Im trying to make a simple form and make it send and email based on the information on the form. This is a sub window where you are supposed to receive the parameter from the URL (messages.aspx?mail=mail@mail.com). So far I am capturing the value from the mail and displaying it on the textfield.

But whenever I type another value and I hit on the button "Send", no matter if I do change the value on the textbox, it is only taking the value from the URL, completely ignoring the value on the box.

And if I open it by hand (messages.aspx), then add the email, of course, because there is no parameter in the URL, I just get the "Please add an email". I guess it is a event handling issue. But Im fairly new to this.

Messages.aspx.cs

public partial class Messages : System.Web.UI.Page
{

public string emails = "";
protected void Page_Load(object sender, EventArgs e)
{
loadList();
emailT.Text = Request.QueryString["mail"];

}

protected void SendMail(object sender, EventArgs e)
{
BO.Messages mail = new BO.Messages();
emails = Request.QueryString["mail"];
if (emails != "")
{
emailT.Text = emails;
}
if(emailT.Text == "")
{
ClientScript.RegisterStartupScript(this.GetType(), "Redirect", "alert('Please fill in an email')", true);
}
else
{
mail.TemplateEmail(emailT.Text, title.Text, txtDetails.Text);
ClientScript.RegisterStartupScript(this.GetType(), "Redirect", "alert('Email sent!'); window.close()", true);
}


}


Messages.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Messages.aspx.cs" Inherits="ASF.HC.JobApplication.Admin.Messages" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="ajaxToolkit" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<style type="text/css">
#Button2 {
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Messages</h2>
<br />
<asp:ScriptManager ID="ScriptManager2" runat="server"></asp:ScriptManager>
<legend>Pick the template to use:</legend>
<asp:dropdownlist id ="ddlTemplate" runat ="server" Height="23px" Width="436px">
</asp:dropdownlist >
<asp:Button ID="Button1" runat="server" Text="Select" OnClick="Template_Changed" />
<br /> <legend>Email Recipient: </legend>
<asp:TextBox ID ="emailT" runat="server" Width="356px" Height="24px" Visible="true" Text="prueba@prueba.com"></asp:TextBox>
<br /><legend>Email Subject: </legend><asp:TextBox ID ="title" runat="server" Width="356px" Height="24px" Visible="true" ></asp:TextBox>
<br /> <br /> <asp:TextBox ID ="txtDetails" runat="server" Width="850px" Height="267px" Visible="true" ></asp:TextBox>
<ajaxToolkit:HtmlEditorExtender ID="TextBox1_HtmlEditorExtender" runat="server" TargetControlID="txtDetails"
EnableSanitization="false" DisplaySourceTab="true" >
</ajaxToolkit:HtmlEditorExtender><br />
<br /><div align="center">
<br />
<legend>If you want to save the template, name it: (Optional) </legend><asp:TextBox ID ="nameTemplate" runat="server" Width="356px" Height="24px" Visible="true" ></asp:TextBox>
<br /><br />
<asp:Button ID="Button2" runat="server" Text="Send" Height="44px" Width="132px" OnClick="SendMail"/> <asp:Button ID="Button3" runat="server" Text="Save Template" Height="44px" Width="132px" OnClick="saveTemplate"/>
</div>
<br />
</asp:Content>


Thanks!

Answer

Add a "not Postback" into the Page-Load Event and request the value from querystring only once, by Page-Load.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack) {
       loadList();
       emailT.Text = Request.QueryString["mail"];
    }
}

protected void SendMail(object sender, EventArgs e)
{
    BO.Messages mail = new BO.Messages();

    if(emailT.Text == "")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "Redirect", "alert('Please fill in an email')", true);
        }
        else
        {
            mail.TemplateEmail(emailT.Text, title.Text, txtDetails.Text);
            ClientScript.RegisterStartupScript(this.GetType(), "Redirect", "alert('Email sent!'); window.close()", true);
        }
    }
Comments