【问题标题】:PopUpExtender on ImageButton inside GridView problemGridView 内 ImageButton 上的 PopUpExtender 问题
【发布时间】:2011-05-02 16:20:06
【问题描述】:

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

【问题讨论】:

  • 嘿,您在将问题标记为已回答时遇到任何问题吗?

标签: asp.net ajaxcontroltoolkit modalpopupextender


【解决方案1】:

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

第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();
}   

第二条: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> 

再次注意,Modal Pop Up Extender 的 Target Control ID 与 Pop Up Control ID 相同。还要注意模板字段中按钮上的 OnClientClick 属性,确保包含“return false;”。

onClick(或onCommand)事件处理程序背后的代码中需要的所有内容来完成您需要做的事情。

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

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

【讨论】:

    【解决方案2】:

    问题与页面呈现给客户端时真实 ID 发生变化有关。

    在浏览器中打开您的页面源代码并查看从 asp.net 生成的 ID。然后在 TargetControlID 属性中使用该 ID

    这种问题存在于 ASP.NET 中的所有模板化控件中

    更简洁的方法是在页面加载时绑定 ModalPopupExtendr 的 TargetControlID 属性,您可以在其中使用动态生成的 ClientID 属性

    modalPopupExtender.TargetControlID = myTemplateControl.ClientID;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多