【问题标题】:Open an ajax modalpopup with details from gridview cells打开一个 ajax modalpopup,其中包含来自 gridview 单元格的详细信息
【发布时间】:2012-11-05 22:54:25
【问题描述】:

我有一个包含约会信息的网格视图。我想做的是在每个单元格中都有一个链接按钮(将在运行时创建)并打开一个显示约会详细信息的模式弹出窗口。任何帮助将不胜感激。

到目前为止,我已经得到了,但它不会触发链接按钮

<asp:GridView ID="Grd" runat="server" AutoGenerateColumns="true" onrowdatabound="Grd_RowDataBound"></asp:GridView><asp:Button ID="btnShowPopup" style="display:none" runat="server"  />
<ajaxToolkit:ModalPopupExtender
ID="ModalPopupExtender1" runat="server" TargetControlID="btnShowPopup" PopupControlID="pnlpopup" CancelControlID="ImgCancel" ></ajaxToolkit:ModalPopupExtender>
<asp:Panel ID="pnlpopup" runat="server" Width="400px" ><!--Show Details--!>
 <asp:ImageButton ID="imgCancel"  AlternateText="cancel"  Height="25" Width="25" runat="server" ImageAlign="Right" />
</asp:Panel>

后面的代码

protected void Grd_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Split out the visit details & format
            for (int i = 0; i <= e.Row.Cells.Count - 1; i++)
            {

                LinkButton lnk = new LinkButton();
                lnk.Text = "Details for:" + "<br />" + e.Row.Cells[i].Text; 

                lnk.CommandName = "ShowDetails";
                lnk.Command += LinkButton_Command;
                e.Row.Cells[i].Controls.Add(lnk);


            }



        }
    }
    protected void LinkButton_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "ShowDetails")
        {

            LinkButton btndetails = sender as LinkButton;

            GridViewRow gvrow = (GridViewRow)btndetails.NamingContainer;

            this.ModalPopupExtender1.Show();


        }
    }

【问题讨论】:

  • 您目前在哪里\何时在您的 gridview 上调用BindData()
  • 如果不是回发,则在页面加载上。

标签: c# .net ajax gridview modalpopupextender


【解决方案1】:

最重要的是,您需要确保在回发时重新创建动态LinkButton 控件,以便正确连接事件。由于您在Grd_RowDataBound 处理程序中创建了LinkButtons,它仅在您调用BindData() 时被调用(并且您没有在回发时调用它),所以您的事件不会被连接。所以如果你的代码看起来像这样:

if (!IsPostBack)
{
     Grd.BindData();
}

尝试删除 if(!IsPostBack) 检查,并始终在 Page_Load 上调用 BindData()。

【讨论】:

  • 您好,谢谢,但是,因为它每次点击都会重新加载页面并绑定网格,所以它真的很慢。
【解决方案2】:

在您的模态弹出窗口中,唯一的内容应该是一个空的 iframe。然后编写一个javascript函数如下:

function showPopup(target, link, width, height)
{
    var frame = document.getElementById('popupIframe');
    frame.src = link;
    frame.width = width;
    frame.height = height;
    var location = Sys.UI.DomElement.getLocation(target);
    var popup = $find('Popup');
    popup.set_X(location.x);
    popup.set_Y(location.y);
    popup.show();
}

然后在你的链接按钮中添加:

OnClientClick="showPopup(this, your_details_page_with_querystring_params, width, height)

【讨论】:

    猜你喜欢
    • 2013-07-01
    • 2020-04-19
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多