C# 在页面中找到控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9830198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
find control in page
提问by user1263390
HTML
HTML
<body>
<form id="form1" runat="server">
<asp:Button runat="server" ID="a" OnClick="a_Click" Text="apd"/>
</form>
</body>
Code
代码
protected void a_Click(object sender,EventArgs e)
{
Response.Write(((Button)FindControl("a")).Text);
}
This code works fine.
这段代码工作正常。
However, this code:
但是,这段代码:
HTML
HTML
<%@ Page Title="" Language="C#" MasterPageFile="~/Student/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Student_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Button runat="server" ID="a" OnClick="a_Click" Text="andj"/>
</asp:Content>
Code
代码
protected void a_Click(object sender, EventArgs e)
{
Response.Write(((Button)FindControl("a")).Text);
}
This code does not work and FindControlreturns Null- why is this?
此代码不起作用并FindControl返回Null- 为什么会这样?
The FindControlmethod works in a simple page fine, but in a master page, does it not work?
该FindControl方法在一个简单的页面中工作正常,但在母版页中,它不起作用吗?
The ID of the ais changed to ctl00_ContentPlaceHolder1_a- how can find control?
的IDa更改为ctl00_ContentPlaceHolder1_a-如何找到控件?
采纳答案by Hans
To find the button on your content page you have to search for the ContentPlaceHolder1control first.
Then use the FindControlfunction on the ContentPlaceHolder1control to search for your button:
要在内容页面上找到按钮,您必须先搜索ContentPlaceHolder1控件。然后使用控件FindControl上的功能ContentPlaceHolder1搜索您的按钮:
ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
Response.Write(((Button)cph.FindControl("a")).Text);
回答by McGarnagle
This is probably due to how ASP.NET names the client IDs for nested controls. Look at the page source and see exactly what ASP.NET is naming your control.
这可能是由于 ASP.NET 如何命名嵌套控件的客户端 ID。查看页面源代码并确切了解 ASP.NET 为您的控件命名的内容。
For example, looking at my page I can see that the button within the content placeholder renders like this:
例如,查看我的页面,我可以看到内容占位符中的按钮呈现如下:
<input type="submit" name="ctl00$ContentPlaceHolder1$btn1" value="hello" id="MainContent_btn1" />
In this case FindControl("ctl00$ContentPlaceHolder1$btn1") returns a reference to the Button.
在这种情况下 FindControl("ctl00$ContentPlaceHolder1$btn1") 返回对 Button 的引用。
回答by Adam
See if the ID of the control is in fact being rendered as 'a'. Use firebug or developer tools while page is loading. You can change client id mode to static and get the same ID each time.
查看控件的 ID 是否实际上呈现为“a”。在页面加载时使用萤火虫或开发人员工具。您可以将客户端 ID 模式更改为静态并每次获取相同的 ID。
回答by Chen G
You may try this..
你可以试试这个..
this.Master.FindControl("Content2").FindControl("a");
You may refer this article...
你可以参考这篇文章...
http://www.west-wind.com/weblog/posts/2006/Apr/09/ASPNET-20-MasterPages-and-FindControl
http://www.west-wind.com/weblog/posts/2006/Apr/09/ASPNET-20-MasterPages-and-FindControl
回答by e wagness
controls are nested. you have your page, inside the page there is more controls, some of these controls contain controls themselves. the FindControl method only searches the current naming container, or if you do Page.FindControls if will only look for the controls in Page, not in the Controls inside those controls so you must search recursively.
控件是嵌套的。你有你的页面,页面里面有更多的控件,其中一些控件包含控件本身。FindControl 方法仅搜索当前命名容器,或者如果您执行 Page.FindControls 则只会在 Page 中查找控件,而不是在这些控件内的 Controls 中,因此您必须递归搜索。
if you know the button is inside the content place holder and you know its id you can do:
如果您知道按钮位于内容占位符内并且您知道它的 ID,您可以执行以下操作:
ContentPlaceHolder cph = Page.FindControl("ContentPlaceHolder1");
Response.Write(((Button)cph.FindControl("a")).Text);
alternatively, if your controls is deeply nested, you can create a recursive function to search for it:
或者,如果您的控件嵌套很深,您可以创建一个递归函数来搜索它:
private void DisplayButtonText(ControlCollection page)
{
foreach (Control c in page)
{
if(((Button)c).ID == "a")
{
Response.Write(((Button)c).Text);
return null;
}
if(c.HasControls())
{
DisplayButtonText(c.Controls);
}
}
initially you would pass this Page.Controls
最初你会通过这个 Page.Controls
回答by john carlo
if the page to look for has no master page
如果要查找的页面没有母版页
this.Page.Master.FindControl("ContentPlaceHolder1");
else
别的
this.Page.Master.FindControl("ContentPlaceHolder1").FindControl("controlAFromPage");
回答by Joydip Roy
ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.Master.FindControl("ContentPlaceHolder1");
Button img = (Button)cph.FindControl("btncreate_email");
回答by eugeniy
This should find any Control on page
这应该在页面上找到任何控件
private Control FindALL(ControlCollection page, string id)
{
foreach (Control c in page)
{
if (c.ID == id)
{
return c;
}
if (c.HasControls())
{
var res = FindALL(c.Controls, id);
if (res != null)
{
return res;
}
}
}
return null;
}
Call like:
像这样调用:
Button btn = (Button)FindALL(this.Page.Controls, "a");
btn.Text = "whatever";
回答by Joydip Roy
To find the master page control on the other pages we can use this:
要在其他页面上找到母版页控件,我们可以使用:
Button btnphotograph = (Button)this.Master.FindControl("btnphotograph");
btnphotograph.Text="Hello!!";

