How to access control on user control?

My web form calls a user control with a Link Button. Based on inline condition I have to set visiable =TRUE/FALSE for the Link Button on the user control. Can anybody know how can I do this?


2/7/2006 3:35:35 AM 93655 articles. 6 followers. Follow

8 Replies

Similar Articles

[PageSpeed] 53
Get it on Google Play
Get it on Apple App Store

  u can access control which is in user control using the following,

  Dim urCtl As UserControl = LoadControl("urControl.ascx")
 CType(urCtl , urControl).label1.Visible = True 



2/7/2006 7:08:24 AM
You posted the same question previously and received 2 responses. If these did not solve your problem, could you please post some more details on what your exact issue is in that thread?

My Blog
2/7/2006 9:25:10 AM

User Control I call is "topmenu.ascx" and the control I want to access is LinkButton called "lbLogOff".
This error "compile error' on second line. Error message is below.

What am I doing wrong?

Dim urCtl As UserControl = LoadControl("topmenu.ascx")
CType(urCtl, LinkButton).lbLogOff.Visible = True

Error 38 Value of type 'System.Web.UI.UserControl' cannot be converted to 'System.Web.UI.WebControls.LinkButton'. 


2/7/2006 3:49:40 PM
You are trying to cast your user control object to type linkbutton, which isn't going to work. You need to cast it to the type of your user control. If you are using the method above, you would also need to make your linkbutton a public object, otherwise it wouldn't be accessible from outside the class. However, it would be better practice to include a property in your user control class and access it via that- see your other thread for an example.

My Blog
2/7/2006 5:00:45 PM

  here "urcontrol" refers ur user control not a link button,change ur code like the following,

 Dim urCtl As UserControl = LoadControl("topmenu.ascx")
CType(urCtl, topmenu).lbLogOff.Visible = True
'topmenu.Controls.Add(urCtl)           '   u dont need to add ur user control's linkbutton into usercontrol again.
   u can just add ur user control into ur aspx page by using the following ,
    otherwise add panel to ur page and then add ur control into it by using ,

2/8/2006 2:17:58 AM

Jeevi and booler,

Thanks both of you for your input.

I don't know why this casting doesn't work for me. I got it worked around as booler mentioned above. I have added a local public rotine in the user control and call it from the web from, which just works perfect. I am guessing the visibility might be the issue with LoadControl and CTtype method. Again booler mentioned about define the control publically. Very curious how to make public the control defined as below

<asp:LinkButton ID="lbLogOff" runat="server" OnClick="lbLogOff_Click" Visible="False"
CausesValidation="False">Log Off</asp:LinkButton>&nbsp; <a href="pcalogin.aspx">Login</a>




2/8/2006 4:22:23 AM
In ASP.NET 1.1, there were 2 ways to do it- you could find the control declaration in your code-behind and change the scope from protected to public/friend, or you could set a property. It is far better OO practice to set a property, because all objects should be self-contained, and making an object's field public breaks this containment, and this can cause all kinds of trouble if you get into the habit of doing it. In ASP.NET 2.0, the control declarations are not in the code-behind any more, so you have to use properties anyway.

This is how you set a property in VB.NET:

    Friend Property TextBoxVisible() As Boolean
            Return Me.TextBox1.Visible
        End Get
        Set(ByVal value As Boolean)
            Me.TextBox1.Visible = value
        End Set
    End Property

If you put this in your user control, you can now hide the textbox using:

MyUserControlId.TextBoxVisible = False

Hope this helps.

My Blog
2/8/2006 9:34:20 AM
Works just perfect. Thanks,-Prakash
2/9/2006 3:47:17 AM

Similar Artilces:

access controls on web user control x from web user control Y
I have 2 web users controls in a page and I would like to get or set the selected values of a few dropdown boxes on web user control X from web user control Y and I also would like to know how to access functions that are on a web user control X from a web user control Y, is this possible? How can I do it? User can access and set the values of the User Control from .aspx page through properties,using javascript and in code-behind of aspx page.The details of it are shown below Using PropertiesIf the Sample.ascx control has two textboxes and submit button.You can access the valu...

How to access individual controls from a form containing the web user control containing those controls
 Below is a block of code in my .aspx form named default.aspx. In this form I load a web user control. I then add it to a place holder control.(Lines 3 and 4 below) So how do I access the individual controls in my web user control? I want to set the properties on those controls, but I am baffled as to how to access the control. For example I have a text label control named "label3". How do I access this control. Because for example, under certain situations I want to hide that control. ( Label3.Visible = false;)  1    if(!Page.IsPostBack) 2&nb...

Problem in accessing web form data in user controls when create user control dynamically
HI. I have a problem using web user control. I designed a user control named NewsForm. This user control contains some web form elements like <asp:textbox>. When I add it in an .ASPX page, all things may be true. I can know which user filles in textbox (for example and other controls) <%@ Register TagPrefix="uc1" TagName="NewsForm" Src="NewsForm.ascx" %> and <UC1:NewsForm id="newsForm" runat="server"></UC1:NewsForm> But when i create it in code behined dynamically, however user control has web form controls but their value are blank for example //aspx co...

User Control Within UserControl
If I have a user control A and then inside it a user control B embedded/referenced within it.  User Control A has a panel defined (cpeNewUser) that I want to manipulate from the code behind of User Control B based on some event that happens.So putting this in my code behind of User Control B won't work as it has no idea of cpeNewUser:cpeNewUser.Collapsed = true;So is it because only at runtime they are married up and because of this you cannot access each other's objects at compile time?  How would I get this to work becuase I'm using all user controls inside a AJAX Too...

Web User Control to Web User Control...
Hi, I have 2 User controls on a page and want Control1 to call a method of Control2. Is this Possible? Example: Con1 = Datalist showing products, each with a Quantity Box and a Button. Con2 = Repeater showing Cart Preview (name, Price) in page.aspx. When I click on the button on the DataList in Con1 it adds the item into the Session based Cart. However it does not update the Cart Preview. I need to call BindPreview() in Con2 in order to update the Cart Preview... Does that make sense? argh! any help much appreciated! PeteWeb Design Nottingham, UK Well I can call BindPreview() from m...

How to access a user control from another user control in a Wizard control
My page has a wizard control and each step is a user control.  I could find the value of a control in step1 user control in the wizard page but not in step2 user control.From wizard.aspx.vb – it worksDim m_title As DropDownList = CType(Me.PersonalBox1.FindControl("ddlTitle1"), DropDownList) Question:How can I find the value of a control(textbox or dropdownlist)in step1 user control from step2 user control? I want to find “PersonalBox1.FindControl("ddlTitle1")” from Step2 AddressBox1. I have tried several ways to access it but no luck. From AddressBox.ascx.vb –...

Accessing form controls from a user control
What's up all,I have a user information form in a user control(standard input fields). When I used use VS 2003 I could set the declared Web Controls to "public" so I could access them from the page referencing the user control..But with VS 2005 I can't seem to find a way to access the declarations of the individual Web Controls to make them public.Would it be a better idea to make a property for each control, within the user control and make those public to retrieve the data? Thanks!Gary It's a good OOP practice to create a public property within the UserControl to expose access...

Access SqlDataSource on Web Form from a Web User Control...
Hello, I want to access a SqlDataSource on a Web Form fromout. a Web User Control. To do this, I have in my code behind of the Web User Control the folowing code: DropDownList1.DataSource = (this.Page.FindControl("SqlDataSource3") as SqlDataSource);DropDownList1.DataTextField = "Name;DropDownList1.DataValueField = "NameIDropDownList1.DataBind(); But, the control (ic. SqlDataSource3) is not found. Even other controls like labels and buttons are not found with the FindControl. What am I doing wrong? Thanks Andromedaris     This is not much of a surprise,...

Access a Web User Control Method from a Web Form
I'm trying to pass the Selected Index value from a web user control in a web form to the user controls selectedIndexchanged Sub.  I can't seem to figure out how to pass the information that I need to the user control and have it execute the sub routine. In the SelectedInedxChanged Sub I have a switch Statement and I want to get the index value from the web form where the user control is located and compare it to each case and then to have it execute the code for the case that it matches. What do I need to do? Hi, I am not sure if I understood your question correctly. I su...

Access parent control in Web user control
Iam using Web User Control. I need to assign some values to the parent control. Is it possible to do it?. Thanks in advance. Cheers!!! Prashanthan KThanks! Prashanthan K I got the solution for this. Me.Parent.FindControl("") returns the parent control. Thanks Prashanthan KThanks! Prashanthan K...

Accessing a control inside a Web User control
Hello, I have a bunch of controls inside a Web User Control (.ascx) file. This user control is used inside an aspx page. I wanted to access the controls inside the ascx from the codebehind for aspx (have to bind a drop down list inside the ascx from the codebehind for the aspx) How can this be done? ...tried using FindControl method but it somehow returns 'null' Thanks in advance,Kumar A better solution is to expose a property from the User Control (perhaps a DataSet) and then fill in that DataSet and bind the control inside the UserControl and have the UserControl do the work,...

Accessing Web Controls within a User Control
Hi, I've built a page which contains a user control. The contents of this user control is a dynamically-built series of radiobuttonlists. What I need to be able to do is access these radiobuttonlists from the code behind the outer page, the one containing the user control. However, it seems as though I cannot do this. Looping through control.controls I can see a placeholder control within my user control, but not the dynamically generated radiobuttonlists beneath it. As far as I'm aware you can usually access dyanamically generalted controls by using control.controls, but this seems di...

web controls and web user control
i have a problem in a web form , I have a webform that contains a web user control which in turn contains a html input form ,the web control events don't fire up ,When I click button ,no effect ,I don't know what is the effect of the html form which is in the web user control on the web controls in the web form ?? can any one help me the web control event should be handle in the code behind page, is the event handler for the button you are clicking registered? You can try double clicking the button, should give you the default btn_click event handler in code behind. Also, make ...

Unable to access user control's user control's function\property from another user control
Hi, I used to call an user control's user control function as stated below from my user control in ASP.Net 1.1 wucCompany.wucEmployee.GetEmployeeSomething() After migrating to ASP.Net 2.0, I am unable to use any properties/functions(even the public ones) of the user control's User control's from another user control.  The way I have to do is create property\function in wucCompany which calls the wucEmployee's property\function and call the wucDepartment.GetEmployeeSomething().Since I need to do this change in too many places, I can...

UpdatePanel within a web user control resets data of controls outside the user control
Hello, I have a usercontrol on my page which displays a running marquee text. It makes use of an UpdatePanel (within the user control) to refresh the marquee text (within the updatepanel) at periodic intervals. For this I added a timer control and its tick event as an async postback trigger for the UpdatePanel. I have a lot of textboxes and dropdown list boxes (all with runat=server) on the page outside the above user control. These are also contained in different UpdatePanels. Problem is, when the user control does a postback to refresh the marquee text, it causes some of...

Web resources about - How to access control on user control? -

Control - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

TERROR IN JAKARTA: 'Situation under control' after explosions, gunfire erupt in Indonesian capital
At least six people are dead, including three police officers, after several explosions at the Sarinah shopping mall in an apparent multi-site ...

DFES downgrades bushfire in Dunsborough as it is contained, but not under control
A BUSHFIRE south of Dunsborough has been contained but is not yet under control.

WA fires: threatening Jindalee blaze brought under control
A fire that was at one stage a threat to lives on homes in Jindalee on Thursday has been brought under control.

Queensland police shut down out-of-control party in Berserker, Rockhampton
Police shut down a party of 150 young people who had gathered at a house in Berserker, Rockhampton on Friday night.

BMW recalls i8 for stability control problem
... , BMW , Safety , Coupe , Hybrid , Performance BMW will recall a small number of 2015 i8s because of a problem with the dynamic stability control ...

ESI cost control and networks
There is an interesting discussion thread on Twitter about how the way Medicare for All saves money is by significantly reducing provider, device, ...

While No One Was Watching, Obama Took Control of the Courts
... and President Obama’s legacy.” It didn’t favor conservatives but there was a dramatic shift to the far left. The appellate is under the control ...

Apple has landed a very rare sponsorship deal to ensure it has control of the hottest ticket in San Francisco ...
Apple has signed up for the first time to become a sponsor of the Super Bowl Host Committee, Daniel Roberts at Yahoo Finance reported. It's ...

Leonardo DiCaprio: The Oscar stuff is ‘completely beyond my control at this point’
... last night’s London premiere of The Revenant . Leo is looking pretty good these days. He’s de-bloated, his facial hair is very much under control, ...

Resources last updated: 1/17/2016 4:01:16 AM