简体   繁体   English

在页面中查找控件

[英]find control in page

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 FindControl returns Null - why is this? 此代码不起作用, FindControl返回Null - 为什么这样?

The FindControl method works in a simple page fine, but in a master page, does it not work? FindControl方法适用于一个简单的页面,但在母版页中,它不起作用吗?

The ID of the a is changed to ctl00_ContentPlaceHolder1_a - how can find control? 在的ID a改为ctl00_ContentPlaceHolder1_a -如何能找到控制?

To find the button on your content page you have to search for the ContentPlaceHolder1 control first. 要在内容页面上找到该按钮,您必须首先搜索ContentPlaceHolder1控件。 Then use the FindControl function on the ContentPlaceHolder1 control to search for your button: 然后使用ContentPlaceHolder1控件上的FindControl函数搜索您的按钮:

 ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
 Response.Write(((Button)cph.FindControl("a")).Text);

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

if the page to look for has no master page 如果要查找的页面没有母版页

this.Page.Master.FindControl("ContentPlaceHolder1");

else 其他

this.Page.Master.FindControl("ContentPlaceHolder1").FindControl("controlAFromPage");
ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.Master.FindControl("ContentPlaceHolder1");
       Button img = (Button)cph.FindControl("btncreate_email");

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";

To find the master page control on the other pages we can use this: 要在其他页面上查找母版页控件,我们可以使用:

Button btnphotograph = (Button)this.Master.FindControl("btnphotograph");
btnphotograph.Text="Hello!!";

This is probably due to how ASP.NET names the client IDs for nested controls. 这可能是由于ASP.NET如何为嵌套控件命名客户端ID。 Look at the page source and see exactly what ASP.NET is naming your control. 查看页面源代码,确切了解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的引用。

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

See if the ID of the control is in fact being rendered as 'a'. 查看控件的ID实际上是否呈现为“a”。 Use firebug or developer tools while page is loading. 在加载页面时使用firebug或开发人员工具。 You can change client id mode to static and get the same ID each time. 您可以将客户端ID模式更改为静态,并且每次都获得相同的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM