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.
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;
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
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..
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.
allenrajiev
Member
95 Points
83 Posts
Server Control dynamic access
Feb 21, 2007 06:01 AM|LINK
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
Haissam
All-Star
37421 Points
5632 Posts
Re: Server Control dynamic access
Feb 21, 2007 06:56 AM|LINK
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
MCAD.NET
| Blog |
allenrajiev
Member
95 Points
83 Posts
Re: Server Control dynamic access
Feb 21, 2007 07:03 AM|LINK
XOR_
Member
418 Points
110 Posts
Re: Server Control dynamic access
Feb 21, 2007 07:09 AM|LINK
XOR_
Member
418 Points
110 Posts
Re: Server Control dynamic access
Feb 21, 2007 07:09 AM|LINK
allenrajiev
Member
95 Points
83 Posts
Re: Server Control dynamic access
Feb 21, 2007 07:13 AM|LINK
<
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
XOR_
Member
418 Points
110 Posts
Re: Server Control dynamic access
Feb 21, 2007 07:23 AM|LINK
because ur control exist in a content place holder try
body.FindControl(name);
allenrajiev
Member
95 Points
83 Posts
Re: Server Control dynamic access
Feb 21, 2007 08:16 AM|LINK
allenrajiev
Member
95 Points
83 Posts
Re: Server Control dynamic access
Feb 21, 2007 09:03 AM|LINK
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..
jbeall
Participant
813 Points
273 Posts
Re: Server Control dynamic access
Mar 16, 2007 01:36 PM|LINK
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