Passing data into a ModalPopupExtender

落爺英雄遲暮 提交于 2019-12-03 20:57:58

It is not necessary to create a user control to pass data into a modal popup. I would only create a user control if I needed to reuse it in different locations. If you don't need to reuse it in multiple locations then you don't need a user control. Instead do this:

Suppose you need a popup that displays a string and has OK and Cancel buttons, but you need to set the string value before it pops up.

  1. Create a Panel.
  2. Place an UpdatePanel inside the Panel from step 1.
  3. Place a Button and ModalPopupExtender inside the UpdatePanel.
  4. Place a Label, the Ok and Cancel buttons inside the UpdatePanel, underneath the popup extender. This is shown below:

    <asp:Panel ID="Panel1" runat="server" CssClass="popup" Width="320px" Style="display:none">
       <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
           <ContentTemplate>
               <asp:Button ID="Button1" runat="server" Style="display:none" />
               <cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server"
                   TargetControlID="Button1"
                   PopupControlID="Panel1"
                   CancelControlID="CancelButton">
               </cc1:ModalPopupExtender>
               <asp:Label ID="Label1" runat="server" />
               <asp:Button ID="OkButton" runat="server" Text="Ok" CausesValidation="true" OnClick="OkButton_OnClick" />&nbsp;
               <asp:Button ID="CancelButton" runat="server" Text="Cancel" CausesValidation="false" />
           </ContentTemplate>
       </asp:UpdatePanel>
    </asp:Panel>
    
  5. Next, on your page, place a Button and a TextBox like this:

    <asp:TextBox ID="MyTextBox" runat="server" />
    <asp:Button ID="MyButton" runat="server" Text="Popup" OnClick="MyButton_OnClick" />
    
  6. Now, in the code behind for your page, place the handler for MyButton:

    protected void MyButton_OnClick(object sender, EventArgs e)
    {
         Label1.Text = MyTextBox.Text;
         UpdatePanel1.Update();
         ModalPopupExtender1.Show();
    }
    

I think you need to provide a bit more information. Can you show us or describe the markup/codebehind contained within your MakePayment user control? And when you talk about the "parent control" which element are you referring to? MakePayment, pnlMakePayment, pnlDummy?

Once I realized what I was doing wrong the solution I came up with was pretty straight forward. I must thank Arnold Matusz for it was his blog entry that showed me the light. http://blog.dreamlabsolutions.com/post/2009/01/10/ModalPopupExtender-to-show-a-MessageBox.aspx

Instead of having the ModalPopup entender call a user control that just displayed some html. I made a user control that was the ModalPopupExtender. This way I can call it and pass in some data.

My MakePayment user control's HTML looks like

<asp:Button ID="btnDummy" style="display: none;" runat="server"  
   OnClick="btnDummy_Click" />
<ajaxToolkit:ModalPopupExtender ID="mdlPopupPayment" runat="server"  
            TargetControlID="btnDummy" PopupControlID="pnlMakePayment" 
            CancelControlID="popUpCancel" DropShadow="true"
            BackgroundCssClass="modalBackground">
</ajaxToolkit:ModalPopupExtender>
<asp:Panel ID="pnlMakePayment" runat="server" Style="display: none;"  
    SkinID="PopUpPanel" class="ContentBoxColor" Width="400px" Height="170px">
<div class="contentbox">
    <div class="contentboxHeader1">
        <asp:Label ID="lblPopupTitle" Width="350px" runat="server" Text=""></asp:Label>
    </div>
    <div class="contentbox">
        <div style="text-align: left">
            <asp:Label ID="lblTotalDue" Width="100px" runat="server" Text="">  
            </asp:Label>
            <asp:Label ID="lblTotalDueValue" Width="100px" runat="server">
            </asp:Label>
         </div>
        <div style="text-align: left">
            <asp:Label ID="lblPayment" Width="100px" runat="server" Text=""></asp:Label>
            <asp:TextBox ID="txtPayment" Width="100px" runat="server"></asp:TextBox>
            <ajaxToolkit:FilteredTextBoxExtender ID="ftbePayment" runat="server" 
               TargetControlID="txtPayment" FilterType="Custom, Numbers" 
                 ValidChars="."> </ajaxToolkit:FilteredTextBoxExtender>
        </div>
        <div style="text-align: left">
            <asp:Label ID="lblPaymentRefNo" Width="100px" runat="server" Text=""> 
            </asp:Label>
            <asp:TextBox ID="txtPaymentRefNo" Width="100px" runat="server">
            </asp:TextBox>
        </div>
        <div style="text-align: left">
        <asp:Label ID="lblPaymentType" Width="100px" runat="server" Text=""></asp:Label>
            <asp:TextBox ID="txtPaymentType" Width="250px" runat="server"></asp:TextBox>
        </div>
    </div>
    <div class="contentbox" style="text-align=right">
        <asp:Button ID="btnApplyPayment" runat="server" Text="" 
             OnClick="btnApplyPayment_Click" />
        <asp:Button ID="btnCancel" runat="server" Text="" OnClick="btnCancel_Click" />
    </div>
</div>
<div style="text-align: right; width: 100%; margin-top: 5px;">
    <asp:Button ID="popUpCancel" runat="server" Text="Cancel" Width="0px" />
</div>
</asp:Panel>

And in the code behind I have a public method

public void MakePayment(string szAmountDue)
{
    lblTotalDueValue.Text = szAmountDue;
    mdlPopupPayment.Show();
}

On my main form I have a link called Make Payment

<asp:LinkButton ID="lnkMakePayment" runat="server" Visible="true" 
      OnClick="lnkMakePayment_Click" >
     <asp:Label ID="lblMakePayment" runat="server" Text=""/>
</asp:LinkButton>

When the Make Payment link is clicked the code behind calls the MakePayment control passing in any data I wish to display.

protected void lnkMakePayment_Click(object sender, EventArgs e)
{
    MakePayment.MakePayment(lblTotalValue.Text.ToString());
} 
Guaroa Mendez
<asp:Panel runat="server" ID="pPopupQuoteSearch" CssClass="modalPopupQuote" Style="display: none">
    <asp:LinkButton ID="lbClose" Text="Close" runat="server" />
    <div id="dQuoteContainer" runat="server">
    </div>
</asp:Panel>
<asp:Button ID="Button5" runat="server" Style="display: none" />
<cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="Button5"
        PopupControlID="pPopupQuoteSearch" BackgroundCssClass="modalBackground" OkControlID="lbClose">
</cc1:ModalPopupExtender>

C# CODE

protected void btnQuteSearch_Click(object sender, EventArgs e)
{
    pPopupQuoteSearch.Style.Remove("display");
    pPopupQuoteSearch.Style.Add("display", "inline");

    dQuoteContainer.InnerHtml = "some HTML code";

    ModalPopupExtender1.Show();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!