简体   繁体   English

模态弹出窗口上的按钮单击事件,未触发网格视图

[英]Button click event on modal popup, inside a grid-view not firing

I'm having problems with the following. 我在以下方面遇到问题。

The scenario: I have a asp grid with a few columns that bind to data. 场景:我有一个ASP网格,其中有一些绑定到数据的列。 The last column has been converted to a template-field. 最后一列已转换为模板字段。 In this template-field is a button with a modal popup extender attached to it. 在此模板字段中,是一个按钮,上面已附加了模式弹出扩展器。 Hidden inside this field is a modal popup. 隐藏在此字段中的是模式弹出窗口。 This modal popup is used to add a new account. 此模式弹出窗口用于添加新帐户。 it contains 2 text boxes, drop down lists and buttons("Add" and "Cancel"). 它包含2个文本框,下拉列表和按钮(“添加”和“取消”)。 When "add" is clicked the modal should close after the inserting of the new account in the code behind. 单击“添加”后,模态应在后面的代码中插入新帐户后关闭。

The Problem: I get the popup to display and load the Drop down lists from the cache, without problem. 问题:我可以弹出窗口来显示并从缓存中加载下拉列表,而不会出现问题。 How do I get the button click event to fire in the code behind. 如何获取按钮单击事件以在后面的代码中触发。 I've tried using a JavaScript function that performs a _doPostBack('btnAddAcc','') but it keeps returning the error "JavaScript error: Object expected". 我尝试使用执行_doPostBack('btnAddAcc','')的JavaScript函数,但该函数始终返回错误“ JavaScript错误:预期对象”。 I gathered after about 1 hour on Google that it is because "btnAddAcc' is not found because it is actually within the grid-view cell and can't be directly accessed. Using page methods and ajax calls is a last resort as the company has a strict policy against this and only allowes this after a bunch of paperwork. 大约1个小时后,我在Google上搜集了这是因为未找到“ btnAddAcc”,因为它实际上位于网格视图单元中并且无法直接访问。使用页面方法和ajax调用是该公司的最后手段对此采取严格的政策,并且只有在大量文书工作之后才允许这样做。

The Code: 编码:

<asp:GridView ID="gvNEA" runat="server" CssClass="gridA_Orange" 
AutoGenerateColumns="False" AllowPaging="True" 
EmptyDataText="No transactions with 'Non Existent Account(s)'" 
ShowHeaderWhenEmpty="True" Width="945px">
<Columns>
    <asp:BoundField HeaderText="Transaction Date" DataField="Transaction_Date" />
    <asp:BoundField HeaderText="Account Number" DataField="Account_Number" />
    <asp:BoundField HeaderText="Description" DataField="Description"/>
    <asp:BoundField HeaderText="Amount" DataField="Amount"/>
    <asp:BoundField HeaderText="Offset Account" DataField="OffsetAccount" />
    <asp:BoundField HeaderText="File Name" DataField="FileName" />
    <asp:BoundField HeaderText="Transaction Type" DataField="TransType" />
    <asp:TemplateField ShowHeader="False">
        <ItemTemplate>
            <asp:Button ID="btnAdd" runat="server" CausesValidation="false" 
                OnClientClick="showPopUp('pupAddAcc');" Text="Add Account" CssClass="ButtonStyle_Gray" />
            <asp:ModalPopupExtender ID="mpeAddAcc" runat="server" 
                BackgroundCssClass="modalBackground" TargetControlID="btnAdd"
                PopupControlID="pnlAddAcc"
                CancelControlID="btnCancelAddAcc" DropShadow="True" 
                Enabled="True">
            </asp:ModalPopupExtender>
            <div id="pupAddAcc">
                <asp:Panel ID="pnlAddAcc" runat="server" BackColor="White" 
                    BorderColor="White" BorderStyle="Solid" BorderWidth="1px" Width="430">                
                    <table align="center" id="tblAddAcc" class="BasicHTMLTable">
                    <asp:HiddenField ID="hfTransType" runat="server" Value='<%# Bind("TransType") %>' />
                        <tr align="center" >
                            <td colspan="2">
                                <asp:Label ID="lblAccountHeader" runat="server" width="390px" Text="Add Account" 
                                CssClass="ButtonStyle_Orange" Height="25px" Font-Bold="true" Font-Size="Large" />
                            </td>
                        </tr>
                        <tr>
                            <td align="right" class="PopupCol">Account Number</td>
                            <td align="left" ><asp:Label ID="lblAccNumber" runat="server" Width="165px" 
                                    Text='<%# Bind("Account_Number") %>' /></td>
                        </tr>
                        <tr>
                            <td align="right" class="PopupCol" >Name</td>
                            <td align="left" ><asp:TextBox ID="txtName" runat="server" Width="165px" />
                                <asp:RequiredFieldValidator ID="rfvName" runat="server" 
                                    ControlToValidate="txtName" Text="*" ValidationGroup="acc" />
                            </td>
                        </tr>
                        <tr>
                            <td align="right" class="PopupCol" >Search Name</td>
                            <td align="left" ><asp:TextBox ID="txtSearchName" runat="server" Width="165px" />
                                <asp:RequiredFieldValidator ID="rfvSName" runat="server" 
                                    ControlToValidate="txtSearchName" 
                                    Text="*" ValidationGroup="acc" />
                            </td>
                        </tr>
                        <tr>
                            <td align="right" class="PopupCol" >Group</td>
                            <td align="left" >
                                <asp:DropDownList ID="ddlGroup" runat="server" Width="170">                                
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td align="right" class="PopupCol" >Currency</td>                                    
                            <td align="left" >
                                <asp:DropDownList ID="ddlCurrency" runat="server" Width="170px">
                                </asp:DropDownList>
                            </td>
                        </tr> 
                        <tr>
                            <td align="center" colspan="2">
                                <asp:Button ID="btnAddAcc" runat="server" Text="Add Account" 
                                    CssClass="ButtonStyle_Orange" OnClientClick="javascript:addAccount()" 
                                    ValidationGroup="acc" />  
                                &nbsp&nbsp                      
                                <asp:Button ID="btnCancelAddAcc" runat="server" Text="Cancel" CssClass="ButtonStyle_Orange" />
                            </td>
                        </tr>
                    </table>                
                </asp:Panel>
            </div>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField HeaderText="RecId" DataField="RecId" Visible="False"/>
</Columns>

I open the popup with this JavaScript function: 我使用此JavaScript函数打开弹出窗口:

function showPopUp(p) {
        var Popup = document.getElementById(p);
        Popup.style.visibility = "visible";
        Popup.style.display = "";
    }

The function when btnAddAcc is clicked: 单击btnAddAcc时的函数:

function addAccount() {
        _doPostBack('btnAddAcc', '');
    };

The code behind function 功能背后的代码

Protected Sub btnAddAcc_Click(sender As Object, e As System.EventArgs)
    'Code for inserting new account goes here
End Sub

Thank you in advance for any help and proposals. 预先感谢您的任何帮助和建议。 Regards 问候

What happens if you set the OnClick to your server-side event? 如果将OnClick设置为服务器端事件会怎样?

<td align="center" colspan="2"> 
  <asp:Button ID="btnAddAcc" runat="server" Text="Add Account"  
     CssClass="ButtonStyle_Orange" OnClick="btnAddAcc_Click"  
     ValidationGroup="acc" />   
  &nbsp&nbsp                       
  <asp:Button ID="btnCancelAddAcc" runat="server" Text="Cancel" CssClass="ButtonStyle_Orange" /> 
</td> 

But... the correct way to get an event reference (ie the '__doPostback' thing) is to get it server-side with ClientScript.GetPostBackEventReference 但是...获取事件引用(即“ __doPostback”的东西)的正确方法是使用ClientScript.GetPostBackEventReference在服务器端获取它。

I found the answer eventually. 我终于找到了答案。 I had to use the gridview.RowCommand event. 我不得不使用gridview.RowCommand事件。 It handles all button related events inside a cell of a gridview. 它处理gridview单元中所有与按钮相关的事件。

Have a look at the msdn documentation. 看一下msdn文档。 It should explain most of this command. 它应该解释大多数此命令。

GridView.RowCommand GridView.RowCommand

Regards George 问候乔治

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM