How to access control in user control file from aspx page - asp.net

I have a user control which has one grid control. I used user control in a aspx page twice. Now my question is, Will it create two grid controls with different ID's? If yes, how can I access those two grid controls ?

As you suspect, if you place the UserControl on the page twice, it will create two instances with separate Ids. It is basically the same as placing two TextBoxes on the page as they also share the same class (TextBox), but are created as separate instances with separate ids (TextBox1, TextBox2 for example).
You can access the properties or methods that are provided by a UserControl in the CodeBehind file as you'd access properties or methods of another control type. If you want to access the grids, you'd have to introduce a property on the UserControl that makes the grid accessible from the parent page:
Public ReadOnly Property Grid As GridView
Get
Return grid
End Get
End Property
In the parent page, you can then add code similar to this (for the sake of an example, I hide the grid from the parent page):
userCtrl1.Grid.Visible = False
userCtrl2.Grid.Visible = False
However, in terms of encapsulation, it would be cleaner if the page would not know which controls are placed on a UserControl. This allows you to change the UserControl afterwards without breaking the page. In order to do so, instead of publishing the Grid, you'd add the required properties and methods to the UserControl that the parent page uses to interact with the UserControl. The grid should only be accessed by the properties and methods internally:
User Control:
Public Property ShowDetailData As Boolean
Get
Return grid.Visible
End Get
Set
grid.Visible = value
End Set
End Property
Page:
userCtrl1.Grid.ShowDetailData = False
userCtrl2.Grid.ShowDetailData = False
In this example, if you decide that a Repeater is better suited for displaying the data, you can change this without breaking the page.

Related

Hide Checkbox on a particular page

I am working on ASP.NET application where I am reusing a user control. The user control contains a checkbox and bunch of other controls. I want to display all the controls inside the user control on all the pages but on one single page I want to hide the checkbox.
I was thinking that I can use the databind methods and see if I am on the "pagex" then hide the checkbox. Is there any other way to solve this problem?
If you have access to the code for the control you should be add a new property to the control for hiding/showing the checkbox and then just pass in the property depending upon what page you are on. You'd have to pass in the show/hide property on the load event of the page.
Do you have access to the code??
I would specify a parameter in the code behind for your ascx file. Example:
public bool HideCB = false;
THen when you put your User Control on your aspx page do this:
<uc:TestControl id="TestControl" runat="Server" HideCB="true" />
This way you can do a check on if(HideCB) to determine if you want to make it visible or not.

using same ascx control twice while using Register (ASP.NET)

I'm using Register at the top of the page to register an ascx control, now the thing is I want to use this control twice on the page.
Now, the problem is when I hit buttons of one instance of the control, it fires the validation of both of the controls, and it obviously breaks, because it should be validating only one control - itself! The reason I am sure about this is because if I keep only one instance of the control on the page, then it flows nicely.
What I already tried that did NOT work:
1)Putting the two instances in different ASP Panels.
2)Registering the control twice at top of the page, so each registration has only one instance on the page.
I would not like to modify the validation of the control itself, but it's a huge project and it is being used at other places, and I do not want to disrupt other things. FYI It's using "Page.IsValid" to validate.
Set the ValidationGroup property of the validators and the ValidationGroup property of the buttons dynamically in the Page_Load method of the user control. You can use the user control's ID property as part of the ValidationGroup to differentiate between the two controls.
e.g.,
myRequiredValidator.ValidationGroup = "valGroup_" + this.ID;
myButton.ValidationGroup = "valGroup_" + this.ID;

User control that replaces some another control on aspx page

I have a control on a page (let's say a button). I want to create such a user control, that will have a property TargetControlID, which takes an ID of control (button) on a page and on render will replace (hide) it with another button (a clone of a first button). Is it possible? Did anyone ever made such thing?
It's certainly possible, but I would suggest an alternate approach that will have fewer problems.
Instead of replacing the target control, just hide it by setting it's visibility to false. This way any processing that it does will still work, control ID's will remain static on rendering (automatically generated control ID's depend on order of controls), and it will have the same effect as you're looking for.
Yes, you can do what you're talking about. Create your user control in the usual way. Here's a link in case you're not familiar with that: Creating a User Control
Then create a property in the code-behind like this:
public string TargetControlID { get; set; }
Once it is a property, you can access it through the html:
<Azat:AzatButton ID="abtn1" runat="server" TargetControlID="btnOtherButton1" />
Then in the Page_Load() event handler, you instantiate a button based on this.abtn1.TargetControlID and remove abtn1 from the Page's Controls collection.
this.Controls.Remove(this.abtn1);

ASP.NET: Getting all the page's control as IEnumerable

I'm trying to use the page control's collection with LINQ.
Whereas this works:
dim l = Me.Controls.OfType(Of TextBox).AsQueryable()
the following return an ArgumentExceptionError:
dim l = Me.Controls.AsQueryable()
I need all the controls. Any help?
Thanks
Have you tried:
Me.Controls.Cast(Of Control)
Out of interest, why do you need it as an IQueryable? Isn't IEnumerable<T> enough for you? (That's the result of Cast.)
The problem with just calling AsQueryable is that the control collection doesn't implement IEnumerable<T>, just IEnumerable.
Also, don't forget that controls can be nested, and just asking the page for it's controls will only tell you about the direct children, but it won't tell you about the controls in those controls:
Locate the web forms controls on a page by walking the Controls Collection
This example finds only the controls contained in the Page object and the controls that are direct children of the page. It does not find text boxes that are children of a control that is in turn a child of the page. For example, if you added a Panel control to page, the Panel control would be a child of the HtmlForm control contained by the Page, and it would be found in this example. However, if you then added a TextBox control into the Panel control, the TextBox control text would not be displayed by the example, because it is not a child of the page or of a control that is a child of the page. A more practical application of walking the controls this way would be to create a recursive method that can be called to walk the Controls collection of each control as it is encountered. However, for clarity, the example below is not created as a recursive function.

ASP.NET Passing properties to dynamically generated user controls

I am struggling on something that, I am sure, should be easy. I have been living in backend and winforms code for a long time, and I'm am building my first ASP.NET page in over a year, so apologies for any noobishness
I am inside a User Control on a page.
On this User Control (Parent Control) I need to embed 0 .. n child user controls in a table, where each Child User control is representative of a row in a Listbox on the Parent User Control.
On the child user control I have a few properties that need setting, such as the id of the item in the list box that it represents.
As I see it, I can either
Loop through this list box in code-behind, and try to dynamically insert table rows and child user controls into an existing tag, and set the object's properties, something I have yet to make work - the added controls just don't appear
Use the old fashion ASP trick of looping through the items in the Listbox inside <% %> tags, and embed my < uc:ChildUserControl runat="server" > tags, but I'm not able to set the required properties on the ChildUserControl (I have tried a few things, but the main issue is I can't use <% %> in server controls)
Do something else that is eluding me at present
Any ideas would be greatfully received
repeater control bound to the same data source as the listbox?

Resources