GridView ItemTemplate中的ModalPopupExtender

时间:2010-07-16 01:00:48

标签: asp.net ajax modalpopupextender

如何使用包含LinkBut​​ton的GridView TemplateField来显示点击模式?我有一些数据行,我想更新单击该行中的“编辑”LinkBut​​ton时的详细信息。在模态显示之前,我需要通过代码隐藏加载大量数据。

我正在尝试以下操作,但我无法在事件处理程序中执行Modal1.Show(),因为它位于TemplateField中:

<ItemTemplate>
  <asp:Button runat="server" ID="HiddenForModal" style="display: none" />
    <ajaxToolkit:ModalPopupExtender ID="Modal1" runat="server" TargetControlID="HiddenForModal" PopupControlID="pnlModal" />
    <asp:LinkButton ID="btnEdit" runat="server" Text="Edit" onclick="btnEdit_Click" />
    <asp:LinkButton ID="btnDelete" runat="server" Text="Delete"></asp:LinkButton>
</ItemTemplate>

谢谢, 标记

1 个答案:

答案 0 :(得分:3)

关键是知道GridView中哪一行是被点击的LinkBut​​ton。您可以通过多种方式执行此操作,但我实现它的方法是在RowCommand事件中捕获它。然后,您可以通过FindControl(..)访问所单击行中的ModalPopupExtender。

页:

<asp:TemplateField>
  <ItemTemplate>
    <asp:Button ID="Button1" runat="server" style="Display:none;" Text="Button" />
    <cc1:ModalPopupExtender ID="ModalPopupExtender1" PopupControlID="Popup1" TargetControlID="Button1" BackgroundCssClass="modalBackground" runat="server" />
    <asp:LinkButton ID="LinkButton1" CommandName="Popup" runat="server">Popup</asp:LinkButton>
  </ItemTemplate>
</asp:TemplateField>

代码隐藏:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton LinkButton1 = (LinkButton)e.Row.FindControl("LinkButton1");
            LinkButton1.CommandArgument = e.Row.RowIndex.ToString();
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Popup" && e.CommandArgument != null)
        {
            int rowIndex = Convert.ToInt32(e.CommandArgument);
            ModalPopupExtender modalPopupExtender1 = (ModalPopupExtender)GridView1.Rows[rowIndex].FindControl("ModalPopupExtender1");
            modalPopupExtender1.Show();

            //Perform any specific processing.
            Label1.Text = string.Format("Row # {0}", rowIndex);
        }
    }

另外,因为你在回发上打开模态,你实际上并不需要在ItemTemplate中使用ModalPopupExtender(或隐藏按钮)。您可以将其移出并放在页面上(通过弹出窗口),然后只需调用Show()方法即可。

页:

<asp:TemplateField>
  <ItemTemplate>
    <asp:LinkButton ID="LinkButton1" CommandName="Popup" runat="server">Popup</asp:LinkButton>
  </ItemTemplate>
</asp:TemplateField>

代码隐藏:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Popup" && e.CommandArgument != null)
        {
            int rowIndex = Convert.ToInt32(e.CommandArgument);
            ModalPopupExtender1.Show();                

            //Perform any specific processing
            Label1.Text = string.Format("<Br>Row # {0}", rowIndex);
        }
    }

谢谢,祝你好运!