事件处理忽略来自URL的参数

本文关键字:URL 参数 事件处理 | 更新日期: 2023-09-27 18:12:19

所以我试图使一个简单的形式,使其发送和电子邮件基于信息的形式。这是一个子窗口,您应该从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>

谢谢!

事件处理忽略来自URL的参数

将"not Postback"添加到Page-Load事件中,并通过Page-Load仅一次从querystring请求值。

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);
        }
    }

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

您正在将文本框的值设置为page_load中查询字符串的值?

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