gpt4 book ai didi

c# - ItemTemplate 中的控件无法在代码隐藏中调用

转载 作者:行者123 更新时间:2023-11-30 14:58:03 24 4
gpt4 key购买 nike

我有服务器控件,例如在另一个 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 方法也可以工作,但无法识别这两个方法调用:

enter image description here

我该如何解决这个问题?

最佳答案

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 的内容中。您无法访问这些控件,因为它们是在运行时动态注入(inject)的,它们的控件 ID 不会由代码隐藏的部分自动生成的类自动映射。

有两种解决方案:

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

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

关于c# - ItemTemplate 中的控件无法在代码隐藏中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19839761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com