This question has been asked a few dozen times before; but has never been solved.
i have an UpdatePanel
<asp:UpdatePanel ID="UpdatePanelSetupToolbar" runat="server">
<ContentTemplate>
...
</ContentTemplate>
</asp:UpdatePanel>
But the visual page designer in Visual Studio (2010 (Professional (Windows (7 (Professional (64-bit)))))) gives the error:
Error Creating Control - UpdatePanelSetupToolbar
Type 'System.Web.UI.UserControl' does not have a public property named 'ContentTemplate'.
Edit: pretty colors in pretty screenshots are added for pretty effect
Now, strictly speaking, that is true: UserControl does not have a public property called ContentTemplate.
Fortunately my UpdatePanel is an UpdatePanel, and it does have a public property named 'ContentTemplate'.
So how do i convince Visual Studio that my UpdatePanel is an UpdatePanel?
Important additional notes
The code above doesn't actually fail as is:
<asp:UpdatePanel ID="UpdatePanelSetupToolbar" runat="server">
<ContentTemplate>
...
</ContentTemplate>
</asp:UpdatePanel>
It only fails when i have content inside the ... ContentTemplate:
<asp:UpdatePanel ID="UpdatePanelSetupToolbar" runat="server">
<ContentTemplate>
<Vista:Toolbar ID="ToolbarSetup" runat="server">
<ContentTemplate>
<asp:LinkButton ID="bbSetupDays" ToolTip="Specify how many allocations will be available on these selected days"
OnClick="bbSetupDays_Click"
runat="server">Setup Selected Days</asp:LinkButton>
</ContentTemplate>
</Vista:Toolbar>
</ContentTemplate>
</asp:UpdatePanel>
but Visual Studio is not complaining about inner ToolbarSetup
it's complaining about the outer UpdatePanelSetupToolbar control
So what's the deal with
Type '%s' does not have a public property named '%s'.
?
Unimportant additional notes
The ASP.net web-site compiles, builds, and runs fine. It's just the Visual Studio (2010) designer that complains.
So what's the deal with Type '%s' does not have a public property named '%s'.?
Series
This question is one in the ongoing Stackoverflow series, "Templating user controls":
How to add a Templating to a UserControl?
How to inherit from Control, rather than UserControl?
UserControl has IsPostBack, but Control does not
UserControl does not have public property named ContentTemplate
How do i specify CodeFileBaseClass from web.config?
The update panel is really cool but you really can't make user controls with it. The good news is you can make asp.net user controls that have a javascript payload by following the same design pattern used to make the updatepanel for the ajaxcontroltoolkit.
I built this back in Mar 2009 because I needed a message box that didn't block the ui. I followed the design pattern for usercontrols using the ajaxcontroltoolkit. It has complete source and the VS intelisense works on the javascript. It's a usercontrol that you just drop on the page and it carries it's own javascript payload that you can ajaxify to your hearts content. It even works in a data repeater with no modifications. It was actually accepted as a ajaxcontroltoolkit usercontrol on the website asp.net.
It's free you could put it on github or other repository if you wanted.
http://gosylvester.com/downloads/messagebox.aspx
http://gosylvester.com/blog.aspx?id=55
You should solve this per design.
Put your update panel on a page and make the user control without an update panel.
reason: the update panel is intended to autogenerate clientside scripting for asyncronous postback as you surely know.
The user control is a building block with a self contained functionality encapsulating complexity which should be placeable anywhere without requirements to the context and in this case at least you would need a scriptmanager on the page which instantiates the control.
Related
My code in my MasterPage is causing a full page_load on my content pages every 5 seconds. Sadly, that means it is breaking some of my content pages and how they are programmed. My hope was that a separate UpdatePanel would allow only that region to be updated and not refresh the other UpdatePanels and resources on the content pages.
I've attempted moving the Timer out of the UpdatePanel, changing the UpdatePanel to UpdateMode="Conditional" and even using a third party AjaxPanel (Telerik).
All results are the same, the content page reloads every 5 seconds clearing the title template (Shown below) and sometimes breaking some functions on the site.
Until I can figure out a way to ONLY have the literal update without reloading the other resources I have to leave the Timer disabled.
I'll be happy to post more code if needed, but I didn't want to provide information that may not have any importance.
MasterPage - Site.Master
<title><%: Page.Title %> - My ASP.Net Site</title>
<asp:UpdatePanel ID="UpdatePanelMenu" runat="server">
<ContentTemplate>
<span>Tickets<asp:Literal ID="LiteralUnassignedTickets" runat="server"></asp:Literal></span>
<asp:Timer ID="TimerAutoRefreshMenu" runat="server" Interval="5000" Enabled="true"></asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
if you have to use update panels make the update panel update conditional and set the timer as an asynchronous trigger(very important).
update panels are even worse when they are on master pages then when they are on the content page
a better option for minimal change is setTimeout to a function that does an ajax call to update the number of unassigned tickets. you can grow this to update a whole bunch of fields/elements using a json object. Plus the techniques can be very useful if you move to MVC.
We are in ASP.Net Webforms. I come from a LAMP Stack mindset...
ASPX Code (part of it)
<asp:Content runat="server" ContentPlaceHolderID="PH_MainContent">
<h3 class="fleft">
<asp:Literal runat="server" ID="li_title" />
...
<Example:userControl runat="server" someVariable="text" otherVariable=<%=li_title.Text%> thirdVariable=<%=Items["sort"].toString()%> />
So, li_title is a literal set in the code behind, I want to reuse it later, passing it to a userControl, where it shall be displayed in a javascript. Items is page.Items. I have also tried this.li_title (which is suggested to me in Visual Studio 2015).
After reading "quite a few" very similar questions, this seem the solution. However obviously I'm missing the point. What would that be..?
You can create a public property for each variable on your user control and set them in aspx. See this for more information
here is some code in my page :
<asp:UpdatePanel ID="UpdatePanelEQSelector" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<uc12:EQSelector ID="custEQSelector" OnEqChange="custEQSelector_OnEqChange" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Inside my user Control, I'm asked to add a fileuploader.
After coding it simply, I tested and my fileuploader is always empty.
I've searched a while and discovered that it was a normal behavior.
The solution to make it work is to add a PostBackTrigger for the updatePanel.
When I tested it in my aspx page, I achieved to do it and my fileUploader had the file.
Then I tried to add it dynamically (to finally do it in my control), it worked with that :
PostBackTrigger trigger = new PostBackTrigger();
trigger.ControlID = this.btnTest.ID;
this._UpdatePanelEQSelector.Triggers.Add(trigger);
But I can't manage to make this code work in my control (I passed my updatePanel as a parameter to my control set in Load, the fileUpload is always empty)
Do you see a solution ?
Thanks
Does it postback, but leaves the control empty? or is it not posting back at all. If it's not posting back at all I'd add this
ScriptManager.GetCurrent(this).RegisterPostBackControl(btnTest);
to the page_load of the user control.
If it is posting back, but the control is empty, then I'd wager there is probably some dodgy html somewhere on the page and the values are getting lost.
I didn't want to have a ton of code on one page, but to keep the code modular and simple. So in my TabContainer I have the following where each tab refers to a web page and my code is inside each web page. My TabContainer itself is inside the default.aspx page.
<asp:TabContainer ID="tabTOL" runat="server" ActiveTabIndex="0" CssClass="tol">
<asp:TabPanel ID="tabHome" runat="server" TabIndex="0" HeaderText="Home">
<ContentTemplate>
<iframe src="Home.aspx"></iframe>
</ContentTemplate>
</asp:TabPanel>
...
Of course, the problem is that I cannot refer to other tabs or the TabContainer/default page from within any tab. I'm trying to update a TextBox on the default.aspx page from a tab, but there is no reference to it.
Should I bite the bullet and have one huge web page with all the html and code behind? There are a dozen tabs in my TabContainer. I would think this would slow down processing as well. Or, is there a cleaner way to do this and still retain the ability to reference controls on the main page or other tabs?
I'm working in VS2008 and .Net 3.5 and AJAX 3.5.
Thanks!!
Larry
I would suggest that you change the structure of Home.Aspx into a Web User Control (*.ascx). The advantage is that you are now running within the context of the parent page. Therefore all it's functionality is within reach. For example, to acces a textbox on the parent page, from the Home.Ascx, you would do this:
((Default)this.Parent).txtMyTextBox.Text = "Hello";
I developed a user control that displays a list of products and it works pretty good. Then I dropped this user control into another user control that allows the user to pick different criteria and the product UC updates to show those products, all pretty slick and with AJAX via UpdatePanel.
All was working just fine... then another requirement came in. The "search" control needs to be separate from the product control (so they can be positioned separately). Initially, I thought this was no problem as I would give the search control a reference to the product control and then it would talk to it via reference instead of directly inside the control (which has been removed).
And they do talk. But the product control loads, but refuses to display.
I checked and it is being passed via reference and not a copy ( as best I can tell ).
There is an updatepanel in the search control. There is an update panel in the product control. And then for good measure, there is an update panel surrounding them both in the actual search aspx page.
I've tried setting the product control update panel to conditional and then fire the .Update() method manually.
What's the secret here?
TIA!
SOLVED
Thanks to Jamie Ide for the tip to use events.
Search Control and Product control still have internal update panels, and NO LONGER have them on this particular page.
Search Control now raises an event OnSearchResultsUpdated and exposes the found items in properties. The page subscribes to this event and takes the properties and passes them to the product control and triggers triggers a .Refresh() method on the product control which simply calls the .Update() on its internal updatepanel.
The Product control, FYI, accepts products in several different flavors. A list of distinct SKUs, a list of product ids, a named collection in our database and finally a given product category.
Our designers need to be able to create a new page, drop the control onto it and set some properties and voila! New site page. They don't want to require a programmer's involvement. So keeping the controls self contained is a requirement. Fortunately all the changes I made still work completely with the other uses of the product control.
THANKS AGAIN SO MUCH!
I don't think there's really enough information to work with here, but my best guess is that the Product control is not getting data bound. You may try calling myProdcutsCtrl.DataBind() from the search control (or something inside the Product control that cause a DataBind() for instance myProductCtrl.Search(value1, value2, value3).
One other thing you might try is removing the UpdatePanels and seeing if things work. Then add them back in once you get core functionality going.
UPDATE: I've gone ahead and put some example code that works here which I believe accomplishes what you want. What follows are snippets for the sake of saving space, but include all code necessary to make it run. Hopefully this will at least give you something for reference.
Things to try:
EnablePartialRendering="true|false" setting it to false will force the natural postbacks and is good for debugging UpdatePanel problems.
Make sure you are seeing Loading... come up on your screen. (maybe too fast depending on your dev computer)
Page.aspx
<%# Register Src="~/Product.ascx" TagPrefix="uc" TagName="Product" %>
<%# Register Src="~/Search.ascx" TagPrefix="uc" TagName="Search" %>
...
<asp:ScriptManager runat="server" ID="sm" EnablePartialRendering="true" />
Loaded <asp:Label ID="Label1" runat="server"><%= DateTime.Now %></asp:Label>
<asp:UpdateProgress runat="server" ID="progress" DynamicLayout="true">
<ProgressTemplate><b>Loading...</b></ProgressTemplate>
</asp:UpdateProgress>
<uc:Search runat="server" ID="search" ProdcutControlId="product" />
<uc:Product runat="server" ID="product" />
Search.ascx
<asp:UpdatePanel runat="server" ID="searchUpdate" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<p>
<asp:Label runat="server" AssociatedControlID="filter">Less than</asp:Label>
<asp:TextBox runat="server" ID="filter" MaxLength="3" />
<asp:Button runat="server" ID="search" Text="Search" OnClick="SearchClick" />
</p>
</ContentTemplate>
</asp:UpdatePanel>
Search.ascx.cs
public string ProdcutControlId { get; set; }
protected void SearchClick(object sender, EventArgs e)
{
Product c = this.NamingContainer.FindControl(ProdcutControlId) as Product;
if (c != null)
{
c.Search(filter.Text);
}
}
Product.ascx
<asp:UpdatePanel runat="server" ID="productUpdate" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:Label runat="server">Request at <%= DateTime.Now %></asp:Label>
<asp:ListView runat="server" ID="product">
<LayoutTemplate>
<ul>
<li id="itemPlaceHolder" runat="server" />
</ul></LayoutTemplate>
<ItemTemplate>
<li><%# Container.DataItem %></li></ItemTemplate>
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
Product.ascx.cs
IEnumerable<int> values = Enumerable.Range(0, 25);
public void Search(string val)
{
int limit;
if (int.TryParse(val, out limit))
product.DataSource = values.Where(i => i < limit);
else
product.DataSource = values;
product.DataBind();
productUpdate.Update();
}
Code does NOT represent best practices, just a simple example!
I'm fairly new to AJAX, but I don't think it's a good idea for user controls to have UpdatePanels. I would also advise you not to have the user controls reference each other; they should communicate through events and methods controlled by their container.
I do something similar with two user controls for a master-details display. The master raises an event when an item is selected from a list, the containing page handles the event and calls a method on the details display to display the selected item. If I remember correctly, my first attempt had UpdatePanels in the user controls and I wasn't able to make that work. Having the user controls inside an UpdatePanel on the page works fine.
If I understand you right you have a layout like this:
Outer UpdatePanel
SearchControl
Search UpdatePanel
ProductControl
Product UpdatePanel
Databound Control
Which one of those update panels is actually being called?
I assume that if you check the network traffic with something like Fiddler or Firebug if you're using Firefox, you aren't seeing any HTML for the product update panel coming back?
Have you tried doing something like:
UpdatePanel productUpdate =
Page.FindControl("Product UpdatePanel") as UpdatePanel;
if (null != productUpdate){
productUpdate.Update();
}
By default, if a postback is made from an UpdatePanel, only that control will be updated/re-rendered (this is called partial page-rendering).
To also update/re-render other UpdatePanels, you have to either:
set their UpdateMode property to Always
add the control that makes the postback to their Triggers collection
Check this page in MSDN for details.