通过函数刷新时关闭弹出窗口

Closing popup when refreshed via function

我已经设置好所有功能,功能已经正常运行,一切似乎都很好。只有一个疑问,我不知道这是否可能。我有一个打开 modalpopupextender 的按钮,在那里,我有一个表单,它有另一个激活下拉列表的按钮(这是我获取所有信息的那个),点击按钮后,它应该填写所有表格。 问题是,它关闭了 modalpopupextender,虽然功能正在完成,但我不想关闭 window,我的意思是刷新弹出窗口而不关闭它

这里是...

Class.aspx

<asp:Panel ID="panelEmail" Height="680px" Width="800px" runat="server" CssClass="modalPopUp">
    <h2>Contact by Email:</h2>
    <hr />
    <blockquote>
        <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" />   
        <legend>Email Recipient: </legend>
        <asp:TextBox ID ="emailT" runat="server" Width="356px" Height="24px" Visible="true" Text="prueba@prueba.com"></asp:TextBox>
        <legend>Email Subject: </legend>
        <asp:TextBox ID ="title" runat="server" Width="356px" Height="24px" Visible="true" ></asp:TextBox> 
        <asp:TextBox ID ="txtDetails" runat="server" Width="672px" Height="267px" Visible="true" ></asp:TextBox>
        <ajaxToolkit:HtmlEditorExtender ID="TextBox1_HtmlEditorExtender" runat="server" TargetControlID="txtDetails" 
            EnableSanitization="false" DisplaySourceTab="true" >
        </ajaxToolkit:HtmlEditorExtender><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 />
        <div align="center">
        <asp:Button ID="Button2" runat="server" Text="Send Mail" OnClick="SendMail"/> 
        <asp:Button ID="Button3" runat="server" Text="Save Template" OnClick="saveTemplate"/>
        <asp:Button ID="btnCancelEmail" runat="server" Text="Cancel"  CausesValidation="false" />
        </div>
    </blockquote>           
</asp:Panel>

<!-- Código añadido por Enrique Bravo -->
<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender4" runat="server"
    PopupControlID="panelEmail" 
    TargetControlID="lnkEmail"
    CancelControlID="btnCancelEmail"
    BackgroundCssClass="modalBackGround" 
    DropShadow="true" ></ajaxToolkit:ModalPopupExtender>

                        <tr id="trEmail">
                            <td>
                                <asp:Image ID="Image1" runat="server" ImageUrl ="Images/share.png" width="22px" height="22px" />
                            </td>
                            <td align="left" valign="middle">
                                  <asp:LinkButton ID="lnkEmail" runat="server" Text="Email Contact" ></asp:LinkButton> 
                            </td>
                        </tr>

Class.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack){
loadlist();
         }
    }

    public void loadList()
    {
        if (!this.IsPostBack)
        {
            try
            {
                BO.Messages template = new BO.Messages();
                ddlTemplate.DataSource = template.GetAll();
                ddlTemplate.DataTextField = "Title";
                ddlTemplate.DataValueField = "Id";
                ddlTemplate.DataBind();
                ddlTemplate.SelectedIndexChanged += Template_Changed;
            }
            catch (Exception e)
            {
                e.ToString();
            }
        }
    }

    /// <summary>
    /// Select the correct Message if there has been one template selected
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Template_Changed(object sender, EventArgs e)
    {

        int countryId = int.Parse(ddlTemplate.SelectedItem.Value);
        if (countryId > 0)
        {
            BO.Messages texto = new BO.Messages();
            txtDetails.Text = texto.GetByID(countryId).Body;
            title.Text = texto.GetByID(countryId).Subject;

        }
    }

正如提供的信息 here 所说:

A Postback is an action taken by an interactive webpage, when the entire page and its contents are sent to the server for processing some information and then, the server posts the same page back to the browser.

因此,无论何时单击该按钮,页面都会被发送回服务器,进行更改并发送回客户端,从而导致刷新。

要避免刷新,您可以执行以下两项操作之一:

  1. 使用 UpdatePanel 控件
  2. 将方法 Template_Changed 设为 [WebMethod] 并通过 ajax.

    调用它

    $.ajax({ type: 'POST', url: '<%= ResolveUrl("~/Class.aspx/Template_Changed") %>', data: '{ templateId:' + JSON.stringify(value) + ' }', contentType: 'application/json; charset=utf-8', dataType: 'json', success: function (result) { /****FILL FORM HERE ***/}; });