How do I get around modal interfering with button

2019-08-27 00:58发布

问题:

In my ASP.Net Web Form App, I have a button, and that button is suppose to launch an ajaxToolKit: ModalPopupExtender modal.

<asp:Button ID="uxTicketHistoryButton" runat="server" Text="Show Ticket History"  style="color: blue;" OnClick="uxTicketHistoryButton_Click"/>&nbsp;
<ajaxToolkit:ModalPopupExtender ID="uxTicketHistoryModal" runat="server" PopupControlID="Panel1" TargetControlID="uxTicketHistoryButton"CancelControlID="btnClose" BackgroundCssClass="modalBackground"></ajaxToolkit:ModalPopupExtender>
<asp:Panel ID="Panel1" runat="server" CssClass="modalPopup" align="center" style = "display:none">
....
</asp:Panel ID>

I had asked a previous question on how to insert C# variable values into the modal, but it seems my problem there is that the modal seems to be interfering with my button click event function; essentially keeping it from firing. The function is suppose to get the data I need to plug into the modal once the button is clicked and the modal launched.

protected void uxTicketHistoryButton_Click(object sender, EventArgs e)
{
    DataTable ticketHist = _dtMgr.GetContactInfoByName(uxContactDropdownList.SelectedValue);
    string rName = ticketHist.Rows[0]["RequestorName"].ToString();
    string rPhone = ticketHist.Rows[0]["RequestorPhone"].ToString();
    ....
}

I have noticed several odd things while debugging: 1) I put break points on the string variables (to see what the values were being returned) while debugging. After clicking the button, it goes right over the breakpoints (I assume that means its not "firing" that code.2) I can remove all of the code from inside the _Click function and the modal still launches when I click the button 3) If I comment out the modal code and click the button, the _Click event code fires fine and I can see the values for the string variables. So, I am assuming that the way I have the modal set (I suspect its related to using TargetControlID="uxTicketHistoryButton") is the problem. How do I get around my modal interfering with button click event? What am I doing wrong here?

回答1:

Looks like you found one of those ajaxToolkit quirks. Try this:

Create a hidden button:

<asp:button id="hiddenButton" runat="server" style="display:none;" />

Change your Extender to target said button:

TargetControlID="hiddenButton"

Then in your _Click event, show the modal:

protected void uxTicketHistoryButton_Click(object sender, EventArgs e)
{
    DataTable ticketHist = _dtMgr.GetContactInfoByName(uxContactDropdownList.SelectedValue);
    string rName = ticketHist.Rows[0]["RequestorName"].ToString();
    string rPhone = ticketHist.Rows[0]["RequestorPhone"].ToString();
    ....

    uxTicketHistoryModal.Show();
}