3

我有服务器控件,例如在另一个gridview的模板字段中带有gridview的弹出窗口:

  <asp:TemplateField HeaderText="Actions">
                <ItemTemplate>
                    <asp:Button ID="viewHoursButton" runat="server" Text="View Hours" OnClick="viewHoursButton_OnClick" />
                    <ajaxToolkit:ModalPopupExtender ID="viewHoursPopup" runat="server"
                        TargetControlID="viewHoursButton"
                        PopupControlID="viewHoursPanel"
                        CancelControlID="closeInfoPanelButton2"
                        DropShadow="true">
                    </ajaxToolkit:ModalPopupExtender>
                    <asp:Panel ID="viewHoursPanel" runat="server" CssClass="infoPanel">
                        <asp:Button ID="closeInfoPanelButton2" runat="server" Text="X" CssClass="closeInfoPanelButton"  />
                        <asp:Label ID="viewHoursLabel" runat="server" Text="Label"></asp:Label>
                        <asp:GridView ID="viewHoursGridView" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                            DataSourceID="SqlDataSource6" DataKeyNames="NonScrumStoryId,PK_DailyTaskHours"
                            BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
                            CellPadding="3" CellSpacing="2" Width="94%" OnRowDeleting="viewHoursGridView_OnRowDeleting"
                            OnRowDataBound="viewHoursGridView_OnRowDataBound" CssClass="centerGridView">
                            <Columns>
                                <asp:BoundField DataField="ActivityDate" HeaderText="Activity Date" SortExpression="ActivityDate"
                                    DataFormatString="{0:MM/dd/yyyy}" />
                                <asp:BoundField DataField="Hours" HeaderText="Hours" SortExpression="Hours" />
                                <asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes" />
                                <asp:BoundField DataField="CreateDate" HeaderText="Created Date" SortExpression="CreateDate"
                                    Visible="false" />
                                <asp:CommandField ShowDeleteButton="True" />
                            </Columns>
                            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                            <HeaderStyle BackColor="#7fc041" Font-Bold="True" ForeColor="White" />
                            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                            <SortedAscendingCellStyle BackColor="#FFF1D4" />
                            <SortedAscendingHeaderStyle BackColor="#B95C30" />
                            <SortedDescendingCellStyle BackColor="#F1E5CE" />
                            <SortedDescendingHeaderStyle BackColor="#93451F" />
                        </asp:GridView>
                        <asp:SqlDataSource ID="SqlDataSource6" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
                            SelectCommand="
                           SELECT [DailyTaskHours].[PK_DailyTaskHours]
                                ,[DailyTaskHours].[NonScrumStoryId]
                                ,[DailyTaskHours].[Hours]
                                ,[DailyTaskHours].[Notes]
                                ,[DailyTaskHours].[ActivityDate]
                                ,[DailyTaskHours].[CreateDate]
                            FROM [NonScrumStory]
                                ,[DailyTaskHours]
                            WHERE [DailyTaskHours].[NonScrumStoryId] = @nonScrumStoryId
                                AND [NonScrumStory].[PK_NonScrumStory] = @nonScrumStoryId
                            ORDER BY [ActivityDate] DESC
                            "
                            DeleteCommand="
                            DELETE
                            FROM [DailyTaskHours]
                            WHERE ([PK_DailyTaskHours] = @setDailyPKDeleteParam)
                            ">
                            <SelectParameters>
                                <asp:QueryStringParameter Name="nonScrumStoryId" Type="String" />
                            </SelectParameters>
                            <DeleteParameters>
                                <asp:QueryStringParameter Name="setDailyPKDeleteParam" Type="Int32" />
                            </DeleteParameters>
                        </asp:SqlDataSource>
                    </asp:Panel>
                    <asp:Button ID="addHoursButton" runat="server" Text="Add Hours" OnClick="addHoursButton_OnClick" />
                    <asp:Button ID="editButton" runat="server" Text="Edit" OnClick="editButton_OnClick" />
                    <asp:Button ID="deleteButton" runat="server" Text="Delete" OnClick="deleteButton_OnClick" />
                </ItemTemplate>
            </asp:TemplateField>

我无法将它们移出gridview,因为调用弹出窗口必须正常工作。

但是我后面的代码无法识别 ItemTemplate 中的某些 ID:

protected void viewHoursButton_OnClick(object sender, EventArgs e)
{
    viewHoursPopup.Show();
    viewHoursGridView.DataBind();
}

例如,即使按钮位于 ItemTemplate 内,_OnClick 方法也可以工作,但无法识别两个方法调用:

在此处输入图像描述

我该如何解决这个问题?

4

2 回答 2

5
protected void viewHoursButton_OnClick(object sender, EventArgs e)
{
    var viewHoursPopup = parentGridView.FindControl("viewHoursPopup")
        as WebControl;
    var viewHoursGridView = parentGridView.FindControl("viewHoursGridView");
    if (viewHoursPopup != null && viewHoursGriView != null)
    {
         viewHoursPopup.Show();
        viewHoursGridView.DataBind();
    }
}

...或者...

protected void viewHoursButton_OnClick(object sender, EventArgs e)
{
    var viewHoursButton = (Button)sender;
    var viewHoursPopup = viewHoursButton.Parent.FindControl("viewHoursPopup")
        as WebControl;
    var viewHoursGridView = viewHoursButton.Parent.FindControl("viewHoursGridView");
    if (viewHoursPopup != null && viewHoursGriView != null)
    {
         viewHoursPopup.Show();
        viewHoursGridView.DataBind();
    }
}

为什么?

ItemTemplates、RowTemplates、EditTemplates 等都嵌套在 webforms 所称的 NamingContainers 中。您无法访问这些控件,因为它们是在运行时动态注入的,它们的控件 ID 不会由代码隐藏的部分自动生成的类自动映射。

有2个解决方案:

  1. 将模板的内容包装到 UserControl 中。该 UserControl 的代码隐藏将具有对未进一步嵌套到其他模板中的任何控件的编译时访问权限。要启用 UserControl 和包含它的父模板之间的交互,请在 UserControl 上公开事件和公共属性

  2. 使用 .FindControl(string controlID) “搜索”包含控件的当前模板以找到您正在寻找的控件。只需确保在尝试对其调用任何内容之前检查它们是否为 null,并且根据您打算在它们上调用的方法将它们转换为适当的类型。

于 2013-11-07T15:32:05.393 回答
2

使用该FindControl()方法,但将它们转换为您在ItemTemplate标记声明中使用的更具体的类型,如下所示:

protected void viewHoursButton_OnClick(object sender, EventArgs e)
{
    var viewHoursPopup = parentGridView.FindControl("viewHoursPopup") as ModalPopupExtender;
    var viewHoursGridView = parentGridView.FindControl("viewHoursGridView") as GridView;
    if (viewHoursPopup != null && viewHoursGriView != null)
    {
        viewHoursPopup.Show();
        viewHoursGridView.DataBind();
    }
}

注意:FindControl()返回 a Control,这对对象来说太笼统了,无法调用该.Show()方法。如果无法成功执行转换,as操作员将返回,因此稍后的检查将捕获该情况。nullnull

于 2013-11-07T15:42:51.613 回答