我遇到以下问题。
场景: 我有一个 asp 网格,其中有几列绑定到数据。最后一列已转换为模板字段。在这个模板字段中是一个按钮,上面附加了一个模态弹出扩展器。隐藏在此字段内的是一个模式弹出窗口。此模式弹出窗口用于添加新帐户。它包含 2 个文本框、下拉列表和按钮(“添加”和“取消”)。单击“添加”时,在后面的代码中插入新帐户后,模式应该关闭。
问题: 我让弹出窗口显示并从缓存中加载下拉列表,没有问题。如何在后面的代码中触发按钮单击事件。我尝试使用执行 _doPostBack('btnAddAcc','') 的 JavaScript 函数,但它一直返回错误“JavaScript 错误:预期对象”。我在 Google 上大约 1 小时后收集到,这是因为找不到“btnAddAcc”,因为它实际上位于网格视图单元格中,无法直接访问。使用页面方法和 ajax 调用是公司的最后手段对此有严格的政策,只有在一堆文书工作后才允许这样做。
编码:
<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" />
  
<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>
我用这个 JavaScript 函数打开弹出窗口:
function showPopUp(p) {
var Popup = document.getElementById(p);
Popup.style.visibility = "visible";
Popup.style.display = "";
}
点击 btnAddAcc 时的功能:
function addAccount() {
_doPostBack('btnAddAcc', '');
};
函数背后的代码
Protected Sub btnAddAcc_Click(sender As Object, e As System.EventArgs)
'Code for inserting new account goes here
End Sub
提前感谢您的任何帮助和建议。问候