问题描述
我想找到一个单独的类中的 GridView控件
控制和我有问题这样做。我甚至尝试把我的code在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 code
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):
此方法会发现只有如果控制直接由指定的容器中包含一个控制;也就是说,该方法不贯穿的控制层次控件中搜索
所以,你要么需要:
- 搜索正确的
的ContentPlaceHolder
控制中的控制,而不是从页
。 - 循环每一个控件的
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;
}
一旦你得到了你的控制,你应该使用它转换为
,然后就在这不是很是你所期待的情况下检查空:
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控件在内容页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!