模态弹出窗口上的“发送”按钮在 c# 第一次不起作用
本文关键字:按钮 不起作用 第一次 发送 窗口 模态 | 更新日期: 2023-09-27 17:56:47
我尝试了每个论坛来寻找我问题的答案,但没有任何效果......我有一个带有RequiredFieldValidator的modalpopup。当我单击"发送"时,它应该将信息发送到电子邮件。第一次"发送"不执行任何操作,但有回发。信息仅在第二次单击时发送,弹出窗口不会隐藏。我认为问题是服务器验证,所以我只使用了客户端验证,但仍然有同样的问题。请指导我。欣赏。
母版页
<ajaxToolkit:ModalPopupExtender runat="server" ID="ModalPopup"
TargetControlID="ImgBtnContact"
PopupControlID="PopupControl"
DropShadow="True"
PopupDragHandleControlID="PopupControlDragHandle" X="500" Y="200" CancelControlID="btnPopupCancel" Drag="True" BackgroundCssClass="grey" OnOkScript="btn_Send">
</ajaxToolkit:ModalPopupExtender>
<asp:Panel ID="PopupControl" runat="server" Style="width: 550px; height: 630px; padding: 10px;" BackColor="silver">
<asp:Panel runat="server" ID="PopupControlDragHandle" Style="cursor: move; background-color:Blue; border: solid 1px Gray; color: white; text-align: center; font-size: large; font-weight: 600; height: 35px;">
Contact Form
</asp:Panel>
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<ContentTemplate>
<div style="color: maroon; font-size:medium; font-weight: 500; vertical-align:text-top; margin-left: 25px;"> Plese fill out the form</div>
<table style="width: 80%; height: 100%; text-align: justify; margin: 1px 15px 15px 25px">
<tr style="margin-left: 10px; margin-top:30px;">
<td style="color: maroon; font-size:medium; font-weight: 600;vertical-align:text-top; ">
<label>Name:*</label>
</td>
<td style="Height:50px;vertical-align:text-top; float:right;" colspan="3">
<asp:TextBox ID="name" runat="server" ></asp:TextBox><br />
<asp:RequiredFieldValidator ID="ReqName"
runat="server" Text="Please enter your name"
Display="Dynamic" ForeColor="Red" ControlToValidate="name" ValidationGroup="1"
SetFocusOnError="True" ValidateEmptyText="True" EnableClientScript="False" ></asp:RequiredFieldValidator>
</td>
</tr>
<tr style="margin-left: 10px; margin-top:30px;">
<td style="color: maroon; font-size:medium; font-weight: 600;vertical-align:text-top; ">
<label>Email:*</label>
</td>
<td style="Height:50px;vertical-align:text-top; float:right;" colspan="3">
<asp:TextBox ID="email" runat="server" ></asp:TextBox><br />
<asp:RequiredFieldValidator ID="ReqEmail"
runat="server" Text="Please enter your email address"
Display="Dynamic" ForeColor="Red" ControlToValidate="email" ValidateEmptyText="True" ValidationGroup="1" ></asp:RequiredFieldValidator ><br />
</td>
</tr>
<tr style="margin-left: 10px; margin-top:30px;">
<td style="color: maroon; font-size:medium; font-weight: 600;vertical-align:text-top; ">
<label>Phone:*</label>
</td>
<td style="Height:50px; vertical-align:text-top; float:right;" colspan="3">
<asp:TextBox ID="phone" runat="server" ></asp:TextBox><br />
<asp:RequiredFieldValidator ID="ReqPhone"
runat="server" Text="Please enter phone number" ControlToValidate="phone" ValidateEmptyText="True"
Display="Dynamic" ForeColor="Red" ValidationGroup="1"></asp:RequiredFieldValidator><br />
</td>
</tr><br />
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<br />
<p>
<tr style="margin-left: 10px;margin-top:30px;"">
<td style="color: maroon; font-size:medium; font-weight: 600;vertical-align:text-top; ">
<label>Comments:*</label>
</td>
<td>
<asp:TextBox ID="comments" runat="server" TextMode="MultiLine" Enabled="True">
</asp:TextBox><br />
<asp:RequiredFieldValidator ID="ReqComments"
runat="server" Text="Please enter comments" ControlToValidate="comments" ValidateEmptyText="True" ForeColor="Red"
Display="Dynamic" EnableTheming="True" ValidationGroup="1"></asp:RequiredFieldValidator>
</td>
</tr>
</p>
<tr align="center">
<td style="text-align:center" colspan="2">
<asp:Button runat="server" ID="btnSendClick" CausesValidation="true" Text="Send" ValidationGroup="1" OnClick="btn_Send" Width="90px" Height="30px" EnableViewState="True" />
</td>
<td style="text-align:center" colspan="2">
<asp:Button runat="server" ID="btnPopupCancel" Text="Cancel" OnClick="PopupCancelClick" Width="90px" Height="30px" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
代码隐藏
public partial class MasterPages_MasterPage : System.Web.UI.MasterPage
{ 受保护的无效Page_Load(对象发送器,事件参数 e) {
comments.AutoPostBack = true;
comments.BackColor = System.Drawing.Color.WhiteSmoke;
comments.Columns = 28;
comments.Font.Bold = true;
comments.Font.Italic = false;
comments.Font.Size = FontUnit.Large;
comments.ForeColor = System.Drawing.Color.Navy;
comments.Rows = 7;
comments.TextMode = TextBoxMode.MultiLine;
comments.Wrap = true;
comments.Visible = true;
name.BackColor = System.Drawing.Color.WhiteSmoke;
phone.BackColor = System.Drawing.Color.WhiteSmoke;
email.BackColor = System.Drawing.Color.WhiteSmoke;
protected void btn_Send(object sender, EventArgs e)
{
if (Page.IsValid)
{
string fileName = Server.MapPath("~/App_Data/ContactForm.txt");
string mailBody = System.IO.File.ReadAllText(fileName);
mailBody = mailBody.Replace("##Name##", name.Text);
mailBody = mailBody.Replace("##Email##", email.Text);
mailBody = mailBody.Replace("##Phone##", phone.Text);
mailBody = mailBody.Replace("##Comments##", comments.Text);
MailMessage message = new MailMessage();
message.Subject = "Test Message from the " + PageDescription;
message.Body = mailBody;
message.From = new MailAddress("email.com", "ASAP");
message.To.Add(new MailAddress("email.com", "To receiver"));
SmtpClient mysmtp_client = new SmtpClient();
mysmtp_client.Send(message);
lblMessage.Visible = true;
PopupControl.Visible = false;
System.Threading.Thread.Sleep(5000);
}
else
{
return;
}
}
}
protected void ShowPopupClick(object source, EventArgs e)
{
ModalPopup.Show();
}
protected void PopupCancelClick(object source, EventArgs e)
{
ModalPopup.Hide();
}
尝试覆盖更新面板中的整个弹出窗口...
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<ContentTemplate>
<asp:Panel ID="PopupControl" runat="server" Style="width: 550px; height: 630px; padding: 10px;" BackColor="silver">
<asp:Panel runat="server" ID="PopupControlDragHandle" Style="cursor: move; background-color:Blue; border: solid 1px Gray; color: white; text-align: center; font-size: large; font-weight: 600; height: 35px;">
Contact Form
</asp:Panel>
<div style="color: maroon; font-size:medium; font-weight: 500; vertical-align:text-top; margin-left: 25px;"> Plese fill out the form</div>
<table style="width: 80%; height: 100%; text-align: justify; margin: 1px 15px 15px 25px">
<tr style="margin-left: 10px; margin-top:30px;">
<td style="color: maroon; font-size:medium; font-weight: 600;vertical-align:text-top; ">
<label>Name:*</label>
</td>
<td style="Height:50px;vertical-align:text-top; float:right;" colspan="3">
<asp:TextBox ID="name" runat="server" ></asp:TextBox><br />
<asp:RequiredFieldValidator ID="ReqName"
runat="server" Text="Please enter your name"
Display="Dynamic" ForeColor="Red" ControlToValidate="name" ValidationGroup="1"
SetFocusOnError="True" ValidateEmptyText="True" EnableClientScript="False" ></asp:RequiredFieldValidator>
</td>
</tr>
<tr style="margin-left: 10px; margin-top:30px;">
<td style="color: maroon; font-size:medium; font-weight: 600;vertical-align:text-top; ">
<label>Email:*</label>
</td>
<td style="Height:50px;vertical-align:text-top; float:right;" colspan="3">
<asp:TextBox ID="email" runat="server" ></asp:TextBox><br />
<asp:RequiredFieldValidator ID="ReqEmail"
runat="server" Text="Please enter your email address"
Display="Dynamic" ForeColor="Red" ControlToValidate="email" ValidateEmptyText="True" ValidationGroup="1" ></asp:RequiredFieldValidator ><br />
</td>
</tr>
<tr style="margin-left: 10px; margin-top:30px;">
<td style="color: maroon; font-size:medium; font-weight: 600;vertical-align:text-top; ">
<label>Phone:*</label>
</td>
<td style="Height:50px; vertical-align:text-top; float:right;" colspan="3">
<asp:TextBox ID="phone" runat="server" ></asp:TextBox><br />
<asp:RequiredFieldValidator ID="ReqPhone"
runat="server" Text="Please enter phone number" ControlToValidate="phone" ValidateEmptyText="True"
Display="Dynamic" ForeColor="Red" ValidationGroup="1"></asp:RequiredFieldValidator><br />
</td>
</tr><br />
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<br />
<p>
<tr style="margin-left: 10px;margin-top:30px;"">
<td style="color: maroon; font-size:medium; font-weight: 600;vertical-align:text-top; ">
<label>Comments:*</label>
</td>
<td>
<asp:TextBox ID="comments" runat="server" TextMode="MultiLine" Enabled="True">
</asp:TextBox><br />
<asp:RequiredFieldValidator ID="ReqComments"
runat="server" Text="Please enter comments" ControlToValidate="comments" ValidateEmptyText="True" ForeColor="Red"
Display="Dynamic" EnableTheming="True" ValidationGroup="1"></asp:RequiredFieldValidator>
</td>
</tr>
</p>
<tr align="center">
<td style="text-align:center" colspan="2">
<asp:Button runat="server" ID="btnSendClick" CausesValidation="true" Text="Send" ValidationGroup="1" OnClick="btn_Send" Width="90px" Height="30px" EnableViewState="True" />
</td>
<td style="text-align:center" colspan="2">
<asp:Button runat="server" ID="btnPopupCancel" Text="Cancel" OnClick="PopupCancelClick" Width="90px" Height="30px" />
</td>
</tr>
</table>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
如何打开带有模态的页面?主要是,您是否使用Server.Transfer
或Response.Redirect
从另一个页面或链接转到页面?我注意到,当您使用Server.Transfer时,回发的状态会混淆,导致按钮仅在第二次单击时做出反应。