"The Controls collection cannot be modified because the control contains code blocks" - c#

I am trying to create a simple user control that is a slider. When I add a AjaxToolkit SliderExtender to the user control I get this (*&$#()## error:
Server Error in '/' Application. The Controls collection cannot be modified because the control contains code blocks (i.e. `<% ... %>`). Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. `<% ... %>`).
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[HttpException (0x80004005): The Controls collection cannot be modified because the control contains code blocks (i.e. `<% ... %>`).] System.Web.UI.ControlCollection.Add(Control child) +8677431 AjaxControlToolkit.ScriptObjectBuilder.RegisterCssReferences(Control control) in d:\E\AjaxTk-AjaxControlToolkit\Release\AjaxControlToolkit\ExtenderBase\ScriptObjectBuilder.cs:293 AjaxControlToolkit.ExtenderControlBase.OnLoad(EventArgs e) in d:\E\AjaxTk-AjaxControlToolkit\Release\AjaxControlToolkit\ExtenderBase\ExtenderControlBase.cs:306 System.Web.UI.Control.LoadRecursive()
+50 System.Web.UI.Control.LoadRecursive()
+141 System.Web.UI.Control.LoadRecursive()
+141 System.Web.UI.Control.LoadRecursive()
+141 System.Web.UI.Control.LoadRecursive()
+141 System.Web.UI.Control.LoadRecursive()
+141 System.Web.UI.Control.LoadRecursive()
+141 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
Version Information: Microsoft .NET Framework Version:2.0.50727.3074; ASP.NET Version:2.0.50727.3074
I have tried putting a placeholder in the user control and adding the textbox and slider extender to the placeholder programmatically and I still get the error.
Here is the simple code:
<table cellpadding="0" cellspacing="0" style="width:100%">
<tbody>
<tr>
<td></td>
<td>
<asp:Label ID="lblMaxValue" runat="server" Text="Maximum" CssClass="float_right" />
<asp:Label ID="lblMinValue" runat="server" Text="Minimum" />
</td>
</tr>
<tr>
<td style="width:60%;">
<asp:CheckBox ID="chkOn" runat="server" />
<asp:Label ID="lblPrefix" runat="server" />:
<asp:Label ID="lblSliderValue" runat="server" />
<asp:Label ID="lblSuffix" runat="server" />
</td>
<td style="text-align:right;width:40%;">
<asp:TextBox ID="txtSlider" runat="server" Text="50" style="display:none;" />
<ajaxToolkit:SliderExtender ID="seSlider" runat="server"
BehaviorID="seSlider"
TargetControlID="txtSlider"
BoundControlID="lblSliderValue"
Orientation="Horizontal"
EnableHandleAnimation="true"
Length="200"
Minimum="0"
Maximum="100"
Steps="1" />
</td>
</tr>
</tbody>
</table>
What is the problem?

First, start the code block with <%# instead of <%= :
<head id="head1" runat="server">
<title>My Page</title>
<link href="css/common.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<%# ResolveUrl("~/javascript/leesUtils.js") %>"></script>
</head>
This changes the code block from a Response.Write code block to a databinding expression.
Since <%# ... %> databinding expressions aren't code blocks, the CLR won't complain. Then in the code for the master page, you'd add the following:
protected void Page_Load(object sender, EventArgs e)
{
Page.Header.DataBind();
}

I just ran into this problem as well but found another solution.
I found that wrapping the code blocks with a asp:PlaceHolder-tag solves the problem.
<asp:PlaceHolder runat="server">
<meta name="ROBOTS" content="<%= this.ViewData["RobotsMeta"] %>" />
</asp:PlaceHolder>
(The CMS I'm using is inserting into the head-section from some code behind which restricted me from adding custom control blocks with various information like meta-tags etc so this is the only way it works for me.)

I can confirm that moving the javascript with <% %> tags from the head to the form tag fixes this error
http://italez.wordpress.com/2010/06/22/ajaxcontroltoolkit-calendarextender-e-strana-eccezione/

Place the JavaScript under a div tag.
<div runat="server"> //div tag must have runat server
//Your JavaScript code goes here....
</div>
It'll work!!

you can do the same functionality if you are using script manager in your page.
you have to just register the script like this
<asp:ScriptManager ID="ScriptManager1" runat="server" LoadScriptsBeforeUI="true" EnablePageMethods="true">
<Scripts>
<asp:ScriptReference Path="~/Styles/javascript/jquery.min.js" />
</Scripts>
</asp:ScriptManager>

I tried using <%# %> with no success. Then I changed Page.Header.DataBind(); in my code behind to this.Header.DataBind(); and it worked fine.

I had same issue in the user control. My page that was hosting the control had comments in the head tag, I removed those comments, everything worked afterwards. Some posts also suggest removing scripts from head and placing them in the body.

In my case, I have replaced <%= %> with <%# %>, and it worked!

Keep the java script code inside the body tag
<body>
<script type="text/javascript">
</script>
</body>

The "<%#" databinding technique will not directly work inside <link> tags in the <head> tag:
<head runat="server">
<link rel="stylesheet" type="text/css"
href="css/style.css?v=<%# My.Constants.CSS_VERSION %>" />
</head>
The above code will evaluate to
<head>
<link rel="stylesheet" type="text/css"
href="css/style.css?v=<%# My.Constants.CSS_VERSION %>" />
</head>
Instead, you should do the following (note the two double quotes inside):
<head runat="server">
<link rel="stylesheet" type="text/css"
href="css/style.css?v=<%# "" + My.Constants.CSS_VERSION %>" />
</head>
And you will get the desired result:
<head>
<link rel="stylesheet" type="text/css" href="css/style.css?v=1.5" />
</head>

I had this problem, but not via the Header. My placeholder was in the body. So I replaced all the <%= with <%# and did
protected void Page_Load(object sender, EventArgs e)
{
Page.Header.DataBind();
}
and it worked.

An alternative way is to have another .aspx page act as the page you want to link to.
This is what the header of the Masterpage looks like:
<head runat="server">
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
<link href="CSS/AccordionStyles.aspx" rel="stylesheet" type="text/css" />
</head>
The referenced .aspx form contains your content:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="AccordionStyles.aspx.cs" Inherits="IntranetConnectCMS.CSS.AccordionStyles" %>
.AccordionHeader
{
cursor: pointer;
background-image: url(<%=ResolveUrl("~/Images/Backgrounds/AccordionPaneHeaderClosed.png") %>);
background-repeat: no-repeat;
}
.AccordionHeaderSelected
{
cursor: pointer;
background-image: url(<%=ResolveUrl("~/Images/Backgrounds/AccordionPaneHeaderOpen.png") %>);
background-repeat: no-repeat;
}
.AccordionContent
{
background-image: url(<%=ResolveUrl("~/Images/Backgrounds/AccordionPaneContent.png") %>);
background-repeat: no-repeat;
}
Finally, you need the .aspx page to tell the browser you're sending CSS content:
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/css";
}

I also faced the same issue. I found the solutions like following.
Solution 1:
I kept my script tag in the body.
<body>
<form> . . . . </form>
<script type="text/javascript" src="<%= My.Working.Common.Util.GetSiteLocation()%>Scripts/Common.js"></script> </body>
Now conflicts regarding the tags will resolve.
Solution 2:
We can also solve this one of the above solutions like Replace the code block with <%# instead of <%= But the problem is it will give only relative path. If you want really absolute path it won't work.
Solution 1 works for me. Next is your choice.

I had the same problem, but it didn't have anything to do with JavaScript. Consider this code:
<input id="hdnTest" type="hidden" value='<%= hdnValue %>' />
<asp:PlaceHolder ID="phWrapper" runat="server"></asp:PlaceHolder>
<asp:PlaceHolder ID="phContent" runat="server" Visible="false">
<b>test content</b>
</asp:PlaceHolder>
In this situation you'll get the same error even though PlaceHolders don't have any harmful code blocks, it happens because of the non-server control hdnTest uses code blocks.
Just add runat=server to the hdnTest and the problem is solved.

I solved an error similar to this by putting the <script> inside a contentplaceholder inside the <head> instead of putting the <script> outside the said contentplaceholder inside the <head>

I had the same issue with different circumstances.
I had simple element inside the body tag.
The solution was:
<asp:PlaceHolder ID="container" runat="server">
<a id="child" href="<%# variable %>">Change me</a>
</asp:PlaceHolder>
protected new void Page_Load(object sender, EventArgs e)
{
Page.Form.DataBind(); // I neded to Call DataBind on Form not on Header
container.InnerHtml = "Simple text";
}

I had the same issue with my system, I removed the JavaScript code from the of my page and put it at body just before closing body tag

For some cases ResolveUrl and ResolveClientUrl works but not all times especially in case of js script files. What happens is it works for some pages but when you navigate to some other pages it might not work due to relative path of that particular page.
So finally my suggestion is always do a complete recheck of your site pages for whether all your javascript references are fine or not.
Open your site in Google Chrome -> right click on the page -> click view source page -> HTML appears -> now click your JS hyperlinks; if its working fine it should open the js file in another browser window, otherwise it will not open.

Try writing your java script code outside the head tag it will definitely work.Its resolved when i copy and paste my Java Script code to the bottom of page. In the previous its placed in HEAD tag now just before closing the form tag.
</div>
<script>
function validate() {
try {
var username = document.getElementById("<%=txtUserName.ClientID%>").value;
var password = document.getElementById("<%=txtPWD.ClientID%>").value;
if (username == "" && password == "")
alert("Enter Username and Passowrd");
else {
if (username == "")
alert("Enter Username");
else if (password == "")
alert("Enter Password");
}
}
catch (err) {
}
}
</script>
</form>

Remove the part which has server tags and place it somewhere else if you want to add dynamic controls from code behind
I removed my JavaScript from the head section of page and added it to the body of the page and got it working

In my case I got this error because I was wrongly setting InnerText to a div with html inside it.
Example:
SuccessMessagesContainer.InnerText = "";
<div class="SuccessMessages ui-state-success" style="height: 25px; display: none;" id="SuccessMessagesContainer" runat="server">
<table>
<tr>
<td style="width: 80px; vertical-align: middle; height: 18px; text-align: center;">
<img src="<%=Image_success_icn %>" style="margin: 0 auto; height: 18px;
width: 18px;" />
</td>
<td id="SuccessMessage" style="vertical-align: middle;" class="SuccessMessage" runat="server" >
</td>
</tr>
</table>
</div>

Tags <%= %> not works into a tag with runat="server". Move your code with <%= %> into runat="server" to an other tag (body, head, ...), or remove runat="server" from container.

Related

How to set visibility of a control within an UpdatePanel that uses an UpdateProgress control?

I have an update panel on a page which contains nested Panels. It has a Panel with a small form (like a log-in form) and a button. When the button is clicked, a few conditions are checked, and if passed, the first Panel is hidden and the second Panel with a larger form is displayed.
Because the larger form can take some time to load (it pulls data from other sources), the small form panel contains an UpdateProgress control.
All of this works fine.
Then I added a nested Panel within the login form Panel, placed under the UpdateProgress that will display a meaningful error message, such as if the login code is incorrect. This displays as expected. However, if the user then corrects their information and clicks the button again, the UpdateProgress displays properly, but the error message remains visible, even though I try to set it to Visible = false in code-behind.
This page does not use a master page.
Debugging shows that the Visible property does get set to false, although it still displays in the browser.
ASPX code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="custom_Default" %>
<!DOCTYPE html>
<html lang="en">
<head runat="server">
<!-- meta and link tags -->
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<!-- h1 and all that -->
<asp:ScriptManager ID="scriptManager" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="pnlUpdate" runat="server">
<ContentTemplate>
<asp:Panel ID="pnlLogin" runat="server">
<!-- label and text field -->
<asp:Button ID="btnBegin" runat="server" OnClick="btnBegin_Click" />
<asp:UpdateProgress ID="updProgress" runat="server" AssociatedUpdatePanelID="pnlUpdate">
<ProgressTemplate>
<p>Loading, please wait... </p>
</ProgressTemplate>
</asp:UpdateProgress>
<asp:Panel ID="pnlMessage" runat="server" Visible="false">
<asp:Literal ID="ltlMessage" runat="server" />
</asp:Panel>
</asp:Panel>
<asp:Panel ID="pnlMainForm" runat="server" Visible="false">
</asp:Panel>
<asp:Panel ID="pnlConfirmation" runat="server" Visible="false">
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
<!-- javascript - jquery and bootstrap -->
</body>
</html>
Code Behind (I've tried setting visible to false in the Page_Load, in the pnlUpdate_Load event, and in the btnBegin_Click event - but that error message will not go away.
protected void btnBegin_Click(object sender, EventArgs e)
{
pnlMessage.Visible = false;
Page.Validate();
if (Page.IsValid)
{
// check some conditions. if fails:
ltlMessage.Text = "Reason for failure";
pnlMessage.Visible = true; // works
}
}
I have tried removing the Visible="false" from the pnlMessage on the ASPX page and placing it in the code behind in Page_Load but I still can't hide it after the message has already been displayed.
How can I hide the pnlMessage Panel after the btnBegin is clicked the second time?
I managed to find the solution to this. Apparently there isn't a way to do it in .NET - it has to be done in JavaScript.
Here is the code I used to accomplish this.
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(initializeRequest);
prm.add_endRequest(endRequest);
var _postBackElement;
function initializeRequest(sender, e) {
if (prm.get_isInAsyncPostBack()) {
e.set_cancel(true);
}
$get('pnlMessage').style.display = 'none';
}
function endRequest(sender, e) {
$get('pnlMessage').style.display = 'block';
}
</script>

Escape Key Issues with Update Panel

while hitting ESC key twice when on a page containing an update panel with a any control inside, say textbox or listbox im getting a System.ArgumentException: Invalid postback or callback argument. I have checked in remaining browsers its working fine but in Internet Explorer 8 its creating above said problem.
the possible solution i found, is making EnableEventValidation="false" either at page level or webconfig or disabling escape key. i dont want to go with 2 former solutions as either my site security will compromise and i dont want my escape key to disable.
Any Suggestions/Ideas appreciated.
Code :
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"
EnablePageMethods="true">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanelHeader" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="Search">
<table style="empty-cells: hide;" width="100%" cellpadding="0" cellspacing="0" class="controlsTable">
<tr>
<td class="td4Caption">
Some Text
</td>
<td class="tdpadding">
<asp:TextBox ID="txtbox" ClientIDMode="Static" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:ImageButton SkinID="Export" AlternateText="Generate Report" ToolTip="Generate Report"
ID="ibtnGenerateReport" ValidationGroup="Generate" runat="server" OnClick="ibtnGenerateReport_Click" />
</td>
</tr>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
•Another thing you can try is: EnablePartialRendering="false" to asp:ScriptManager
Make sure your site is rendering with the proper compatibility by using "edge":
< meta http-equiv="X-UA-Compatible" content="edge" />
If nothing above works, please try controlling the exception handler like this:
http://encosia.com/how-to-improve-aspnet-ajax-error-handling/

How to get ScriptManager to work with ScriptmanagerProxy

I have been dealing with an unusual set of errors when I try to compile my asp.net page. This page is inheriting from my masterpage. It should be getting the Scriptmanager from there. But the errors I am getting suggest that it is not.
Now I have this in my page:
<%# Page Title="MassUpdate" Language="C#"
MasterPageFile="~/Site1.Master"
AutoEventWireup="true"
CodeBehind="Update.aspx.cs"
Inherits="AdminSite.Update"
MaintainScrollPositionOnPostback="true" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server" >
<div id="contentarea">
<div>
<h3 style="color:Red; padding-left:5px;">
WARNING - This page can push large amounts of data into the database. Use care when using it!
</h3>
</div>
<asp:ScriptManagerProxy runat="server" >
</asp:ScriptManagerProxy>
And in my masterpage, I have this:
<body>
<header>
<div id="banner">
<h1 style="color:#DBFFFF">UAC Parts Admin</h1>
</div>
</header>
<form id="form1" runat="server">
<div id="container">
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
<asp:LoginView ID="LoginView1" runat="server" EnableViewState="false">
<LoggedInTemplate>
<div id="menubar">
<h6>Reports</h6>
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/Scripts/jquery.js" />
<asp:ScriptReference Path="~/Scripts/jqueryui.js" />
<asp:ScriptReference Path="~/Scripts/menubar.js" />
</Scripts>
</asp:ScriptManager>
The first error is this:
The control with ID '' requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it.
It happens when I don't have ScriptManager on my page and use ScriptManagerProxy instead.Even though I have ScriptManager on my Master page.
Now when I put a ScriptManager on my page I get a different error.
Only one instance of a ScriptManager can be added to the page.
What do I need to do to get this to work?
Is this an issue with one of my Nuget Packages? (JuiceUI,Widgmo, etc)
I would be glad to post code if requested.
EDIT:
Yeah, this whole thing has been weird. Oddly the master page did not have issues itself. But only when the other pages used it did I have any problems. Moving it to the first element after the form tag was the solution I believe. Though I had also moved the ScriptManagerProxy up a bit in my code too.
The ScriptManager must appear before any ContentPlaceHolders. Generally as Joshua pointed out, the script manager is put at the first element after the form tag. Like so:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/Scripts/jquery.js" />
<asp:ScriptReference Path="~/Scripts/jqueryui.js" />
<asp:ScriptReference Path="~/Scripts/menubar.js" />
</Scripts>
</asp:ScriptManager>
<div id="container">
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
The reason for this is because the .aspx pages that uses this master page provide content that is placed into the ContentPlaceHolder controls. Because your ContentPlaceHolder appeared before your ScriptManager, the ScriptManagerProxy located in your content page was throwing because the ScriptManager would not be defined until later down the page.
This can be a bit odd to think about because you are defining controls in multiple different places. But the codebehind does not execute until everything is put together.
Put the ScriptManager on masterpage and ScriptManagerProxy on the .aspx page.
you need to put the script manager near your form declaration in Master page;
have a look on this Also: Walkthrough: Creating an Ajax-Enabled Web Site
<body>
<form runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
...
</form>
</body>
For Your Second Question:ScriptManager Control Overview
Only one instance of the ScriptManager control can be added to the page.
The page can include the control directly, or indirectly inside a nested
component such as a user control, content page for a master page, or nested
master page. If a page already contains a ScriptManager control, but a nested
or parent component needs additional features of the ScriptManager control, the
component can include a ScriptManagerProxy control.
For example, the ScriptManagerProxy control enables you to add scripts and
services that are specific to nested components.
you either have to remove your script manager from master page if wanted to use in Content page or Use proxy in content page with in-addition of script manager in master page

updatepanel updating whole page instead of just the contenttemplate

<%# Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" ClientIDMode="AutoID" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="server">
<%
foreach (var item in AllSales)
{
//Here i have just set a breakpoint to see if it loops the AllSales list when I press the update button
}
%>
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<p>Update Panel: DateTime.Now: <%= DateTime.Now.ToString() %></p>
<asp:Button runat="server" ID="Submit" Text="Update" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
The script manager code is in the masterpage :
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
Problem here is that everytime i click the Update button it loads the page again and loops the "AllSales" list , i want to only update a section and not have to do unnecessary loops.
Here is the fun part : If i remove the masterpage, it works ! But with the masterpage, it dont , why?!
You need to use a server control to initiate the update rather than a normal html input button. Try using:
<asp:Button ID="ActivitySubmit" Text="Submit" runat="server" />
The reason for such behavior is that you use plain html submit button instead of ASP.NET server button. Thus page submitted to server without involving ASP.NET Ajax functionality. Replace ActivitySubmit button with asp:Button control
Set the UpdateMode mode property to Conditional. The default value for UpdateMode is Always, If the UpdateMode property is set to Always, the UpdatePanel control's content is updated on every postback that originates from anywhere on the page, reference
Edit: You are using input type="submit" which probably causing the post back replace it with asp:Button to get the ajax call to work.
I have tested the same code with only change in the button type, and it refreshes only the update panel content. The problem is your site is targeting .NET 3.0 but you need to target at least to 3.5. I have tested on 3.0, it does not work but on 3.5 and 4.0, it works fine. So the easier and safer solution is to target 3.5 onwards. But If you want to use .NET 3.0, I will try to find a workaround. Please let me know.
I tried to recreate your problem but it is working fine in my case. Try creating a new .aspx file and paste the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>Outside UpdatePanel</p>
<asp:ScriptManager runat="server" ID="ScriptManager1">
</asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<p>DateTime.Now: <%= DateTime.Now.ToString() %></p>
<asp:Button runat="server" ID="SubmitButton" Text="Go" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
This was tested in VS2010 using .NET 4.0 in an Empty Web Application. Does this work for you as well? I tried the same example using a Master page with a content placeholder. This yields the same (properly working) results. From this I gather there is something else going on on your page that we're missing. Is there?
[Edit]
I made another simple example, this time with a Master page and a Content page.
Master page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager runat="server" ID="ScriptManager1"></asp:ScriptManager>
<p>Master Page: DateTime.Now: <%= DateTime.Now.ToString() %></p>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Aspx page:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<p>Update Panel: DateTime.Now: <%= DateTime.Now.ToString() %></p>
<asp:Button runat="server" ID="Submit" Text="Update" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Again this works without a problem in the same environment as the other example. It looks like you'll have to tell me about the .NET framework you're targeting and what else you've got set up in your master page and aspx page, because:
In .Net 3.5 and up this will work. In lower versions however, it won't. I'm afraid I've been unable to figure out how to fix it in lower versions though. :(
In your Page tag just add
ClientIDMode="AutoID"
I think you can try and put the form inside the updatepanel, or you can try and change children as triggers property of the updatepanel like this :
<asp:UpdatePanel runat="server" ID="UpdatePanel1" ChildrenAsTriggers="False">
and then add the button as trigger like this
ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(SubmitButton);
try addind this part to your UpdatePanel:
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Submit" />
</Triggers>

Referencing Page.Title after it has been set as part of a asp:contentplaceholder

I've got a master page setup with a contentplaceholder control inside the title tag as so:
<head id="head1" runat="server">
<style type="text/css">
body { font-family: Tahoma; font-size: 9pt; }
</style>
<title><asp:contentplaceholder id="title" runat="server" /></title>
</head>
That contentplaceholder is implemented inside a page that uses that masterpage as so:
<asp:content runat="server" contentplaceholderid="title">
Welcome: <%= this.BasketID %>
</asp:content>
I'm trying to then get a copy of the substituted title inside the masterpage body (also tried inside the page - and this doesnt work either) like:
<p>
<strong>Subject:</strong> <%# Page.Title %>
</p>
In all cases Page.Title and Page.Header.Title are "" (I've tried both databinding and using the <%= %> syntax to no avail.
Does anyone know what is going on here and how I can overcome this?
Thanks.
The problem you're getting is because you are 'tricking' the page cycle. You'd better use this in the codebehind of the page:
Master.Title = "Welcome: " + basketId
You could do it this way however; on the masterpage: create a HtmlTextWriter, configure it to write to a MemoryStream. Render the 'title' contentplaceholder to the HtmlTextWriter, attach a StreamReader to the stream to grab the content as a string, and output this to your page. Yet, this is not efficient, and way too much work :-)

Categories

Resources