gpt4 book ai didi

asp.net - GridView 问题中 ImageButton 上的 PopUpExtender

转载 作者:行者123 更新时间:2023-12-01 10:12:33 28 4
gpt4 key购买 nike

我的 asp.net 页面上有 GridView,该网格中的一列是 ImageButton(TemplateField with ID="imbReserve")。单击该按钮我想显示 PopUp,但是当我输入 TargetControlId="imbReserve"时,我收到错误消息“无法找到 ID 为‘imbReserve’的控件”。如何实现这一点,点击 Grid 中的按钮显示 PopUp?

最佳答案

看看这两篇文章,帮助我解决了这个问题

第 1 条: A More Traditional approach

以下内容转自上述文章

页面代码:

 <asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false"
Width="95%">
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:Button ID="btnViewDetails" runat="server" Text="Details" OnClick="BtnViewDetails_Click" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="customerid" HeaderText="ID" />
<asp:BoundField DataField="companyname" HeaderText="Company" />
<asp:BoundField DataField="contactname" HeaderText="Name" />
<asp:BoundField DataField="contacttitle" HeaderText="Title" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>

<ajaxToolKit:ModalPopupExtender
ID="mdlPopup" runat="server" TargetControlID="pnlPopup" PopupControlID="pnlPopup"
CancelControlID="btnClose" BackgroundCssClass="modalBackground" />
<asp:Panel ID="pnlPopup" runat="server" Width="500px" style="display:none">
<asp:UpdatePanel ID="updPnlCustomerDetail" runat="server" UpdateMode="Conditional">
<ContentTemplate>
[Your Content Here]
</ContentTemplate>
</asp:UpdatePanel>
<div align="right" style="width:95%">
<asp:Button
ID="btnSave" runat="server" Text="Save" />
<asp:Button ID="btnClose" runat="server" Text="Close" Width="50px" />
</div>
</asp:Panel>

请注意,GridView 包含在更新面板中,模态扩展器的目标控件 ID 与弹出控件 ID 相同。这是因为 ModalPopUp Extender 需要目标控件 ID,我认为此解决方案比使用隐藏按钮更好。

现在是背后的代码:

protected void BtnViewDetails_Click(object sender, EventArgs e)
{
// Do Anything you need to the contents of the update panel

// update the contents in the detail panel
this.updPnlCustomerDetail.Update();
// show the modal popup
this.mdlPopup.Show();
}

第 2 条: Uses Clientside Javascript

以下内容转自上述文章

客户端Javascript

    <script type="text/javascript">
// keeps track of the delete button for the row
// that is going to be removed
var _source;
// keep track of the popup div
var _popup;

function showConfirm(source){
this._source = source;
this._popup = $find('mdlPopup');

// find the confirm ModalPopup and show it
this._popup.show();
}

function okClick(){
// find the confirm ModalPopup and hide it
this._popup.hide();
// use the cached button as the postback source
__doPostBack(this._source.name, '');
}

function cancelClick(){
// find the confirm ModalPopup and hide it
this._popup.hide();
// clear the event source
this._source = null;
this._popup = null;
}

页面代码:

    <asp:GridView ID="gvToDoList" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="Item" HeaderText="Description" />
<asp:BoundField DataField="IsCompleted" HeaderText="Complete?" />
<asp:TemplateField ControlStyle-Width="50px" HeaderStyle-Width="60px" >
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server"
OnClientClick="showConfirm(this); return false;"
OnClick="BtnDelete_Click" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

<ajaxToolKit:ModalPopupExtender BehaviorID="mdlPopup" runat="server"
TargetControlID="div" PopupControlID="div"
OkControlID="btnOk" OnOkScript="okClick();"
CancelControlID="btnNo" OnCancelScript="cancelClick();"
BackgroundCssClass="modalBackground" />
<div id="div" runat="server" align="center" class="confirm" style="display:none">
<img align="absmiddle" src="Img/warning.jpg" />Are you sure you want to delete this item?
<asp:Button ID="btnOk" runat="server" Text="Yes" Width="50px" />
<asp:Button ID="btnNo" runat="server" Text="No" Width="50px" />
</div>

再次注意,模态弹出扩展器的目标控件 ID 与弹出控件 ID 相同。还要注意模板字段中按钮上的 OnClientClick 属性,确保包含“return false;”。

在其背后的代码中,onClick(或 onCommand)事件处理程序需要执行您需要执行的所有操作。

我已经成功地尝试了这两种方法。

希望这两个中的一个对你有用。

关于asp.net - GridView 问题中 ImageButton 上的 PopUpExtender,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4056627/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com