GridView ItemTemplate 内的 ModalPopupExtender

发布于 2024-09-10 06:46:28 字数 683 浏览 6 评论 0原文

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

我正在尝试以下操作,但无法在事件处理程序中执行 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>

谢谢, 标记

How do I use a GridView TemplateField containing a LinkButton that is to display the modal on click? I have rows of data that I want to update the details of when clicking the 'Edit' LinkButton in that row. There is a bunch of data I need to load via codebehind just before the Modal displays.

I was trying the following, but I can't do Modal1.Show() in the event handler because it's in a 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>

Thanks,
Mark

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℉絮湮 2024-09-17 06:46:28

关键是要知道 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 (或隐藏按钮)。您可以将其移出并将其放在页面上(通过弹出 div),然后只需调用 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);
        }
    }

谢谢,祝你好运!

The key is knowing which row in the GridView was the LinkButton that was clicked. You can do this several ways but the way I implemented it is to capture it in the RowCommand event. Then you can access the ModalPopupExtender in the clicked row via FindControl(..).

Page:

<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>

Codebehind:

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

Additionally, because you are opening the modal on a postback anyways you don't actually need the ModalPopupExtender (or the hidden button) in the ItemTemplate. You can move that out and put it on the page (by your popup div) and can simply call the Show() method.

Page:

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

Codebehind:

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

Thanks, good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文