User Control embedded on a page changes ClientID of controls - c#

I have a user control(.ascx), which I am embedding in my page(.aspx), as below:
<%# Register TagName="UI" TagPrefix="my" Src="~/userControls/UI.ascx" %>
<my:UI runat="server" ID="mytestui" />
When page is loaded the controls(textboxes, dropdowns etc), inside the user control, changes automatically. Say I have a textbox called txtTest it changes to mytestui_txtTest.
As you can see it takes the ID of user control above and appends it to control.
Is there any way to prevent this from happening?

This happens because your control is a subclass of UserControl class, which implements INamingContainer interface. By default all controls that implement this interface prepend children's IDs with their own IDs, thus insuring that every ID on the page is unique.
To aviod this behavior you have two options:
Create server control, inheriting not from UserControl but from Control class directly. This one does not implement INamingContainer. However you won't have markup file, and have to generate all controls programmatically.
[Edit: ASP.NET 4 only] Use ClientIDMode="Static" for child controls. This mode insures that server and client IDs of the control are exactly the same. In this case this is a job of a developer to make sure that this control is used only once on the page to avoid ID duplicates.

What version of .Net are you using? If >= 4.0 you can use ClientIDMode="Static" as Andrei said. You can set it at control or page level as well.

Related

Problem With Custom Control on Page Using a MasterPage

I have a content page which has a related master page.
I register a prefix <%# TagPrefix ..... and can load other custom controls at that namespace.
However, one particualar control at the same namespace, when added to the aspx page, breaks it.
The control in question inherits from asp:Panel, has a parameterless constructor, defines a few public accessors, and creates some standard child controls, and nothing much else.
Are there some fundamental restrictions to creating custom asp controls that I am breaking unknowingly?
Add the control back to the page. Delete the designer file for the page: .aspx.designer.cs
Then right click on the page and select Convert to Web Application. This should give you the actual error the page has when attempting to write the control definition to your designer file.
I suspect there is a compilation error in your custom control.
My control was attempting to access Page.Header which was null as the master page had not marked the tag with runat="server".
I guess that is a fundamental restriction that I was looking for...

Adding a control to a list of Controls dynamically

I have a web page where users need to enter customer contact information. They could enter from 0 to an infinite number of contacts.
I created this page code on page:
<ajaxToolkit:ToolkitScriptManager runat="Server" EnablePartialRendering="true" ID="ScriptManager1" />
<asp:PlaceHolder ID="phCustomerContacts" runat="server" EnableViewState="true">/asp:PlaceHolder>
<asp:LinkButton ID="btnAddContact" runat="server" OnClick="btnAddContact_Click" CssClass="LinkButton" Text="Add Contact"/>
In my code behind I added this:
public void btnAddContact_Click(object sender, EventArgs e)
{
IList<CustomerContactProfile> customerContacts = new List<CustomerContactProfile>();
if (ViewState["CustomerContactList"] != null)
customerContacts = (List<CustomerContactProfile>)ViewState["CustomerContactList"];
CustomerContactProfile contactProfile = (CustomerContactProfile)LoadControl("~/Controls/Embedded/CustomerContactProfile.ascx");
customerContacts.Add(contactProfile);
foreach (CustomerContactProfile contact in customerContacts)
phCustomerContacts.Controls.Add(contact);
ViewState["CustomerContactList"] = customerContacts;
}
This code doesn't work because the ViewState can't handle storing all of that control data. However, I cannot think of another way to store the controls that were already added.
The viewstate of the asp:PlaceHolder control doesn't save anything and I need the controls to be saved so that if a user puts in some data to the first control that the data isn't lost when they add a second one and so on.
Rather than store the entire control, simply store the underlying data in session, and rebuild the control set from that data every time you reload the page.
I'm not sure it's a best way to add contacts dynamically. Wouldn't it be better to create controls via jquery, and send data for creation to web method ?
It would be better to store the number of controls in viewstate to add instead... And then add them in Page Init or PreInit... ViewState would then be retained for each of the dynamic controls. This would be for postbacks after the button click of course.
HTH.
Store the number of controls the user has entered in the view state. Override the LoadViewState page method and add back the number of controls there. The framework will take care of reloading the posted data into the controls for you. You will not lose information. You just have to make sure you add the controls BEFORE the viewstate is restored.
Store it in the Session instead of Viewstate. It's just as bad but it will work!
I think you should not depend on any temporary storage for this -- Viewstate, Session, or otherwise.
You seem to be using your .ascx like I would normally use a class... User control's going to be bigger, though, I imagine, since it has lots of html in it (?).
Anyway, a generic list of a class would be...smaller, at least.
But otherwise, my favorite approach is just to insert each record into a database when it's done (one-by-one) -- at least for manual input, which that's my impression of what you're working with. For example, using a listview, detailsview, gridview, etc.

Remove statically added controls at runtime

The Scenario: I have an asp.net website where I show a div popup on page load for taking a few user details. When a user inputs the details, or closes the popup, I set up a flag cookie so that the popup is not displayed again for the user. The div is in the MasterPage so that it is displayed no matter on which page a user lands first time. The div contains an UpdatePanel which has all the controls required for taking the details. This whole functionality is working fine.
The Problem: Now this div popup is not showing(by setting display:none) on subsequent postbacks(which I want), but the html markup is still loading with the page unnecessarily adding to the page size. What I would idealy want to do is: Check if flag cookie is set. If no, show the popup, else remove the popup's markup from the page.
Now since the div is not a server control, I cannot possibly remove it and the all the controls inside it. So, I thought of removing the UpdatePanel from the page:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["flag"] != null)
{
if (Page.Controls.Contains(updpnl_contact))
{
Page.Controls.Remove(updpnl_contact);
updpnl_contact.Dispose();
}
}
}
But I guess this tends to work with dynamically added controls only, and since the control is added at Design Time, it is not being removed.
Is there any way I can achieve this?
If you add a runat="server" attribute to your <div> element, it will be available in the code-behind. You'll need an id on it as well. Then you can just toggle the Visible property. If this property is false, the control won't be rendered to the client (i.e. no HTML markup).
What you're trying to do is not at all the usual workflow. I tend to think that it will not work as it would mess up control tree, maybe even corrupt the viewstate and so on.
As a possible solution, you can put it's visibility to hidden in the code behind. This, in the contrary to the usual 'gut feeling', doesn't work like the css propery 'display:none' for example - instead the control will not even be rendered into the page when it's not visible. This may be the workaround for you.
Happy coding.
A more efficient approach would be to create the panel as a UserControl and load it dynamically in codebehind when it's needed, then add it to your page. E.g, in code:
MyPopupControl popup = (MyPopupControl)Page.LoadControl("/path/to/usercontrol.ascx");
PopupPanel.Controls.Add(popup);
Where PopupPanel is an empty <asp:Panel>. Then, not even the markup will need to be loaded/processed except when its needed.
There is no reason that all the code you use to display and process this panel couldn't also be in the usercontrol, isolating it from the master page.
Can you build the panel dynamically, based on the cookie setting?

How do you use ScriptManagerProxy in a custom ASP.NET control?

Basically, I have created a custom control that uses an UpdatePanel, and as I understand it I need to use a ScriptManagerProxy to make it work ( since there should only be one ScriptManager per page, and is is declared in my .aspx page ).So, how do you use this beastie, is it just a matter of adding:
<asp:ScriptManagerProxy ID="ScriptManagerProxy3" runat="server">
</asp:ScriptManagerProxy>
to your control, or is there more to it?
Could anyone please post a link to a good tutorial/example set? I tried googling for this, but I didn't find anything useful...
Your control does not require a ScriptManagerProxy to work. You would only need a ScriptManagerProxy if you needed to add a ServiceReference or ScriptReference in your control. The ScriptManagerProxy allows you to do this without having to have knowledge about the page that is going to host your control. When the page is rendered, the properties of the ScriptManagerProxy controls are merged with those of the ScriptManager control on the host page.

ASP: Extend control (ASCX) and access base markup file from subclass code

I'm building form validation controls for our C# ASP application. The bulk of the work is handled by a BaseValidator control (subclassing System.Web.UI.UserControl), which also has the markup for the validation output. This is then extended by subcontrols like PasswordValidator, that provides the Validate method and any extra fields needed by that validator control.
(The end goal is to have controls like <uc1:PasswordValidator ControlId="txtPassword" /> which we can plop into any form with minimum duplication.)
However, PasswordValidator.ascx.cs cannot access the form elements defined in BaseValidator.ascx; the only way I've found to do so is to duplicate the markup in each subcontrol's *.ascx file. How can I extend BaseValidator.ascx.cs and access BaseValidator.ascx's markup in the subclass?
I'm pretty sure you'll have to create Server Controls to accomplish this. Meaning, you'll need to generate the outputted Markup from code in the control rather than in the .ascx file.
If you have a true baseclass for your BaseValidator control which your PasswordValidator extends, then any markup/controls in the baseclass should be available through protected properties. I would even go so far as to argue that a true base should not have an ascx portion. The base class should provide methods and properties that expose controls to built on the fly (probably during Page_Init to maintain viewstate).
If you have 2 separate controls on the same page, your parent page can be modified to provide brokerage methods to allow such communication.
If you have the PasswordValidator and you just need the controls/markup, you can use the LoadControl method to create an instance of the BaseControl in memory, access its controls/markup programmatically and either add it or destroy it depending on what you want to do with it.
Barring any of that, it would be just as #Shawn said. Server controls.
Thanks for the suggestions.
I solved this using a service and the strategy pattern. There is a central FieldValidator class which provides validation for any user interface. For the web interface, these methods are exposed through a WebService and (through the WebService) a UserControl. There are no issues with accessing the page elements, because there is now only one UserControl class that stands between the form and the WebService.
For example, <uc1:FieldValidator ControlType="Password" ControlToValidate="txtPassword" runat="server" > will plop down fully-functional clientside and serverside validation, with no need for code behind. Great! :)
(I mentioned resolution a while back in a reply to my question, but I can't mark that as answered.)

Categories