find control in page

后端 未结 9 1059
孤城傲影
孤城傲影 2020-12-11 02:37

HTML


    
相关标签:
9条回答
  • 2020-12-11 03:23

    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);
    
    0 讨论(0)
  • 2020-12-11 03:23

    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.

    0 讨论(0)
  • 2020-12-11 03:27
    ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.Master.FindControl("ContentPlaceHolder1");
           Button img = (Button)cph.FindControl("btncreate_email");
    
    0 讨论(0)
  • 2020-12-11 03:33

    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

    0 讨论(0)
  • 2020-12-11 03:35

    if the page to look for has no master page

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

    else

    this.Page.Master.FindControl("ContentPlaceHolder1").FindControl("controlAFromPage");
    
    0 讨论(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

    0 讨论(0)
提交回复
热议问题