find control in page
Asked Answered
P

9

10

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

 <%@ 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?

The FindControl method works in a simple page fine, but in a master page, does it not work?

The ID of the a is changed to ctl00_ContentPlaceHolder1_a - how can find control?

Presentiment answered 22/3, 2012 at 20:43 Comment(1)
The clientID and the id is not the same, and clientidmode won't change anything in this exampleLocular
C
34

To find the button on your content page you have to search for the ContentPlaceHolder1 control first. Then use the FindControl function on the ContentPlaceHolder1 control to search for your button:

 ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
 Response.Write(((Button)cph.FindControl("a")).Text);
Cookstove answered 22/3, 2012 at 21:4 Comment(3)
Is this code placed in the code behind of the master or the page you are trying to locate controls on. I think you mean the master but just for clarification.Decasyllable
@Chef_Code: Yes. The master.Cookstove
However if you are in a control instead of the page, you can use the following code: this.Page.Master.FindControl("ContentPlaceHolder1");Parenteau
A
4

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

Adkinson answered 22/3, 2012 at 21:16 Comment(0)
P
3

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.

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.

Physician answered 22/3, 2012 at 20:47 Comment(4)
To expand on his last comment, using View Source on your web browser to see the full name that ASP.NET generates (it can be quite long).Dorri
'a' changed to 'ctl00_ContentPlaceHolder1_a' now how can find control?Presentiment
FindControl("ctl00$ContentPlaceHolder1$a")Physician
While this would work if he were using jQuery or JS on the client end, this is not the proper technique to find a control in the code behind. You need to find the content1 control first, then find the a control underneath it as stated in some answers below.Gus
O
1

if the page to look for has no master page

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

else

this.Page.Master.FindControl("ContentPlaceHolder1").FindControl("controlAFromPage");
Outflank answered 14/8, 2013 at 8:16 Comment(0)
M
0

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.

if you know the button is inside the content place holder and you know its id you can do:

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

Mansized answered 22/3, 2012 at 22:43 Comment(0)
S
0
ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.Master.FindControl("ContentPlaceHolder1");
       Button img = (Button)cph.FindControl("btncreate_email");
Shaylyn answered 25/10, 2013 at 18:41 Comment(0)
A
0

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";
Amends answered 9/7, 2014 at 11:17 Comment(0)
S
0

To find the master page control on the other pages we can use this:

Button btnphotograph = (Button)this.Master.FindControl("btnphotograph");
btnphotograph.Text="Hello!!";
Shaylyn answered 1/5, 2015 at 11:9 Comment(0)
U
-3

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.

Uncritical answered 22/3, 2012 at 20:49 Comment(2)
'a' changed to 'ctl00_ContentPlaceHolder1_a' now how can find control?Presentiment
Well you can put that ctl00_ContentPlaceHolder1_a as the name it'd probably find it. Or change the button to clientidmode = static and see what the id is then. It should just stay 'a'.Uncritical

© 2022 - 2024 — McMap. All rights reserved.