事件处理忽略URL中的参数

时间:2016-10-10 17:28:33

标签: c# asp.net ajax events

因此,我尝试制作一个简单的表单,并根据表单上的信息发送和发送电子邮件。这是一个子窗口,您应该从URL接收参数(messages.aspx?mail=mail@mail.com)。到目前为止,我正在从邮件中捕获值并将其显示在文本字段中。

但每当我输入另一个值并按下按钮"发送"时,无论我是否更改了文本框上的值,它只是从URL中取值,完全忽略该值在盒子上。

如果我手动打开它(messages.aspx),然后添加电子邮件,当然,因为URL中没有参数,我只是得到"请添加电子邮件"。我想这是一个事件处理问题。但我对此很新。

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>

谢谢!

2 个答案:

答案 0 :(得分:1)

在页面加载事件中添加“非回发”,并通过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);
        }
    }

答案 1 :(得分:0)

  

但每当我输入另一个值并按下按钮&#34;发送&#34;时,无论我是否更改了文本框上的值,它只是从URL中取值,完全忽略该值在盒子上。

您是否已将文本框的值设置为page_load中查询字符串的值?

 emailT.Text = Request.QueryString["mail"];