【问题标题】:"Send" button on Modal Popup is not working first time c#模态弹出窗口上的“发送”按钮第一次不起作用c#
【发布时间】:2014-04-05 22:48:57
【问题描述】:

我尝试了每个论坛来找到我的问题的答案,但没有任何效果.... 我有一个带有RequiredFieldValidator 的模态弹出窗口。当我单击“发送”时,它应该将信息发送到电子邮件。第一次“发送”什么都不做,但有回发。该信息仅在第二次单击时发送,并且弹出窗口不会隐藏。我以为问题出在服务器验证上,所以我只使用了客户端验证,仍然遇到同样的问题。 请指导我。 欣赏。

母版页

 <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

{ protected void Page_Load(object sender, EventArgs 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();
}

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    尝试覆盖更新面板内的整个弹出窗口...

    <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> 
    

    【讨论】:

      【解决方案2】:

      带有模态的页面是如何打开的?主要是,您是使用Server.Transfer 还是Response.Redirect 从另一个或链接转到页面? 我注意到,当您使用 Server.Transfer 时,Postback 的状态会混淆,导致按钮仅在第二次点击时才会做出反应。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多