Server Control dynamic access

Last post 03-16-2007 9:36 AM by jbeall. 9 replies.

Sort Posts:

  • Server Control dynamic access

    02-21-2007, 2:01 AM
    • Member
      93 point Member
    • allenrajiev
    • Member since 02-14-2007, 1:17 PM
    • Posts 80

    Hi All,

    I have the names of Some server controls in string variables. It can be textbox or linkbutton or button or anything.

    I want to hide these controls depending on some conditions.

    How can i get access to these controls. I am using master page. But these controls are in content page.

    I tried, this.page.Findcontrol(ctrName) , but its returning null, though its very much there in the page. I can access this controls using this.btnSave and all, but I cant use this.ctrName, being ctrName a string.

    Also is there any way to cast anytype of Control to a common contol?

    What if i am having some controls in User controls, what If  I am having some custom controls. how will i access, controls inside that in present page.

     

    Thanks for tyhe help

  • Re: Server Control dynamic access

    02-21-2007, 2:56 AM
    • All-Star
      37,391 point All-Star
    • Haissam
    • Member since 10-05-2006, 6:25 AM
    • Beirut - Lebanon
    • Posts 5,632

    The common control to cast is the Control object

    Control ctr = (Control)Page.FindControl("CONTROLID")

    try to look into the below link

    MasterPage And Page.FindControl

    HC

     

    Haissam Abdul Malak
    MCAD.NET
    | Blog |
  • Re: Server Control dynamic access

    02-21-2007, 3:03 AM
    • Member
      93 point Member
    • allenrajiev
    • Member since 02-14-2007, 1:17 PM
    • Posts 80
    But when i try to find Page.FindControl("ControlId") i am getting null. I have no idea why is that happening. Any idea about that.
  • Re: Server Control dynamic access

    02-21-2007, 3:09 AM
    • Member
      418 point Member
    • XOR_
    • Member since 02-09-2007, 2:35 AM
    • Posts 110
    can u post some of ur code in front?
  • Re: Server Control dynamic access

    02-21-2007, 3:09 AM
    • Member
      418 point Member
    • XOR_
    • Member since 02-09-2007, 2:35 AM
    • Posts 110
    can u post some of ur aspx page code.
  • Re: Server Control dynamic access

    02-21-2007, 3:13 AM
    • Member
      93 point Member
    • allenrajiev
    • Member since 02-14-2007, 1:17 PM
    • Posts 80

    <

    asp:Content ContentPlaceHolderID="contentBody" ID="body" runat="server">

    <table style="background-color:whitesmoke">

    <tr>

    <td colspan="6">

    <uc2:DriverSearch ID="_driverSearch" runat="server" />

     

    </td>

    </tr>

    <tr>

    <td>

    <asp:Label ID="Label1" runat="server" Text="Driver Lic. Number:"></asp:Label>

    </td>

    <td>

    <asp:TextBox ID="txtDriversLicense" Width="150px" runat="server"></asp:TextBox>

    </td>

    <td>

    <asp:Label ID="Label2" runat="server" Text="State:"></asp:Label>

    </td>

    <td>

    <asp:TextBox ID="txtState" Width="30px" runat="server"></asp:TextBox>

    </td>

    <td>

    <asp:Label ID="Label3" runat="server" Text="License Exp. Date:"></asp:Label>

    </td>

    <td>

    <cc1:CalendarPopup ID="calLicenseExpiration" runat="server">

    </cc1:CalendarPopup>

    </td>

    </tr>

    <tr>

    <td>

    <asp:Label ID="Label4" runat="server" Text="Physical Date:"></asp:Label>

    </td>

    <td>

    <cc1:CalendarPopup ID="calPhysicalDate" runat="server">

    </cc1:CalendarPopup>

    </td>

    <td>

    </td>

    <td>

    </td>

    <td>

    <asp:Label ID="Label5" runat="server" Text="Physical Exp. Date:"></asp:Label>

    </td>

    <td >

    <cc1:CalendarPopup ID="calPhysicalExpiration" runat="server">

    </cc1:CalendarPopup>

    </td>

    </tr>

    <tr>

    <td>

    <asp:Label ID="Label6" runat="server" Text="Hazmat Qualification"></asp:Label>

    </td>

    <td>

    <asp:DropDownList ID="lstHazmatQualification" runat="server">

    <asp:ListItem Value="Y" Text="Yes"></asp:ListItem>

    <asp:ListItem Value="N" Text="No"></asp:ListItem>

    </asp:DropDownList>

    </td>

    <td>

    </td>

    <td>

    </td>

    <td>

    <asp:Label ID="Label7" runat="server" Text="Hazmat Exp. Date:"></asp:Label --- its here

    </td>

    <td>

    <cc1:CalendarPopup ID="calHazmatExpiration" runat="server">

    </cc1:CalendarPopup>

    </td>

    </tr>

    <tr>

    <td colspan="6">

    <uc1:grdLicenseEndorsements ID="_grdLicenseEndorsements" runat="server" />

     

    </td>

    </tr>

    <tr>

    <td colspan="6" align="center">

    <table>

    <tr>

    <td>

    <cc2:SaveButton ID="cmdSave" runat="server" OnClick="cmdSave_Click"></cc2:SaveButton>

    </td>

    <td>

    <cc2:ResetButtonServer ID="cmdReset" runat="server"></cc2:ResetButtonServer>

    </td>

    <td>

    <cc2:CancelButton ID="cmdCancel" runat="server"></cc2:CancelButton>

    </td>

    </tr>

    </table>

    </td>

    -------------------------------

    string

    controlsToHide = objControls.GetControlsToHide(SiteMap.CurrentNode.Url.ToString(), group);

    if (this.FindControl("Label7") != null)     --------- here I am getting null

    {

    int a = 10;

    }

    if (controlsToHide.Trim() != "" && controlsToHide != null)

    {

    string[] splitList = controlsToHide.Split(';');

    for (int l = 0; l < splitList.Length; l++)

    {

    switch (splitList[l].ToString())

    {

    case "cmdSave":

    this.cmdSave.Visible = false;

    break;

    case "cmdReset":

    this.cmdReset.Visible = false;

    break;

    case "cmdCancel":

    this.cmdCancel.Visible = false;

    break;

    default:

    break;

    }

    if (this.FindControl(splitList[l]) != null)

    {

    this.FindControl(splitList[l].ToString()).Visible = false;

    }

    }

    }

    }

    will this much code be enough? Please let me know if u need full code of aspx n cs

  • Re: Server Control dynamic access

    02-21-2007, 3:23 AM
    • Member
      418 point Member
    • XOR_
    • Member since 02-09-2007, 2:35 AM
    • Posts 110

    because ur control exist in a content place holder try

    body.FindControl(name);

  • Re: Server Control dynamic access

    02-21-2007, 4:16 AM
    • Member
      93 point Member
    • allenrajiev
    • Member since 02-14-2007, 1:17 PM
    • Posts 80
    when i do this.FindControl("body") or Page.findControl("body") i am getting null..so I am not able to use body.FindControl.. Is there any other method to access a control inside a Content Place holder
  • Re: Server Control dynamic access

    02-21-2007, 5:03 AM
    Answer
    • Member
      93 point Member
    • allenrajiev
    • Member since 02-14-2007, 1:17 PM
    • Posts 80

    http://www.codeproject.com/aspnet/InsideMasterPages.asp?df=100&forumid=208804&exp=0&select=1375274


    To get a control in the <childMaster> page, it’s a bit tricky but
    keep in mind two thingsAll controls are in the topmost master page.
    Controls that belong to child master pages are in the appropriate
    ContentPlaceHolder control in the topmost master page. Now take a look at the trace output and check this code snippet below

    Button btn =
      myChildMaster.Master.FindControl("ContentPlaceHolder1").FindControl("button1")
      as Button;
    btn.Text = "by the content page";

     

    This will help and it worked for me..Thank you all..

  • Re: Server Control dynamic access

    03-16-2007, 9:36 AM
    • Participant
      804 point Participant
    • jbeall
    • Member since 07-06-2005, 12:46 PM
    • Posts 253

    I decided to go with recursive lookups.  Rick Strahl has a code snippet demonstrating how to do this.  I simply overrode the FindControl method to use recursive lookups if it couldn't find the control on the first pass.

    Rick expressed some concern about performance, but from the limited amount of benchmarking I've done, it seems to be be fine (see my comment on his blog; search for "Beall").  But do your own due diligence, of course, and make sure you're happy with the performance.

    http://www.west-wind.com/WebLog/ShowPost.aspx?id=5127

      -Josh
     

    -jb
Page 1 of 1 (10 items)