使用 FindControl 在内容页面中获取 GridView [英] Using FindControl to get GridView in a Content Page
问题描述
我想在一个单独的类中找到一个 GridView
控件,但我在这样做时遇到了问题.我什至尝试将我的代码放在 aspx.cs 页面中,但无济于事.我不断收到未将对象引用设置为对象实例的情况.我确定我遗漏了一个简单的步骤,但在我的研究中我似乎找不到任何东西.
I would like to find a GridView
Control within a separate class and I am having issues doing so. I even tried placing my code in the aspx.cs page to no avail. I keep getting Object reference not set to an instance of an object. I'm sure there is a simple step I'm missing, but in my research I cannot seem to find anything.
<asp:GridView ID="GridView1" EnableViewState="true"
runat="server"
BackColor="White" BorderColor="#CC9966"
BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="933px"
onrowdatabound="GridView1_RowDataBound"
onrowdeleting="GridView1_RowDeleting"
onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
onsorting="GridView1_Sorting"
AllowSorting="true"
AutoGenerateColumns="False"
PersistedSelection="true" onrowcancelingedit="GridView1_RowCancelingEdit">
<EditRowStyle Font-Size="Small" Width="100px" />
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="EditLinkButton" CausesValidation="True"
Text="Edit" CommandName="Edit"/>
<asp:LinkButton runat="server" ID="DeleteLinkButton" CausesValidation="False"
Text="Delete" CommandName="Delete"/>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton runat="server" ID="UpdateLinkButton" CausesValidation="True"
Text="Update" CommandName="Update"/>
<asp:LinkButton runat="server" ID="CancelLinkButton" CausesValidation="False"
Text="Cancel" CommandName="Cancel"/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.cs 代码
protected void Page_Load(object sender, EventArgs e) {
SetDirectory();
System.Web.UI.Page page = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
GridView GridViewCopy = (GridView)page.FindControl("GridView1");
Log.WriteLine("SortBindGrid: GridView Row Count: " +
GridViewCopy.Rows.Count, Log.DEBUG_LEVEL.TERSE);
return;
}
我尝试了几种使用 MainContent_GridView 来获取和 Master.FindControl 以获取相同结果的变体.
I've tried a few variations of using MainContent_GridView for the find to get and Master.FindControl with all the same result.
推荐答案
在您的评论之一中,您声明 GridView
不在母版页上,因此假设它在母版页上是否安全在使用母版页的页面上?因此它必须在 ContentPlaceholder
控件中?
In one of your comments you state that the GridView
isn't on the Master Page, so is it safe to assume that it's on a page that uses a Master Page? And therefore it must be in a ContentPlaceholder
control?
关键问题是 FindControl
方法 仅看起来对于直系子女(强调):
The key issue is that FindControl
method only looks for direct children (emphasis added):
该方法只有在控件直接被指定容器包含时才会找到该控件;也就是说,该方法不会在控件内的控件层次结构中进行搜索.
This method will find a control only if the control is directly contained by the specified container; that is, the method does not search throughout a hierarchy of controls within controls.
所以你要么需要:
- 在正确的
ContentPlaceholder
控件中搜索控件,而不是从Page
中搜索. - 递归地遍历
Page.Controls
中的每个控件,直到找到所需的控件.
- Search for the control within the correct
ContentPlaceholder
control, rather than fromPage
. - Loop through each of the Controls in
Page.Controls
recursively until you find the control you're after.
示例 2:
private Control FindControlRecursive(Control rootControl, string controlID)
{
if (rootControl.ID == controlID) return rootControl;
foreach (Control controlToSearch in rootControl.Controls)
{
Control controlToReturn =
FindControlRecursive(controlToSearch, controlID);
if (controlToReturn != null) return controlToReturn;
}
return null;
}
获得控制权后,您应该使用 as
进行转换,然后检查是否为 null,以防万一它不是您所期望的:
Once you've got your control, you should cast it using as
and then check for null just in case it's not quite what you were expecting:
var gridView = FindControlRecursively(Page, "GridView1") as GridView
if (null != gridView) {
// Do Stuff
}
这篇关于使用 FindControl 在内容页面中获取 GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!