I know you can't put a control inside a C# function like this
<%= VirtualPathUtility.ToAbsolute(<umbraco:Item field="background" runat="server" />) %>
but I am wondering if you can pass the value from a control to a C# function.
umbraco:Item above outputs something like ~/media/bg1.jpg
Here is what I am trying to do:
<%# Master Language="C#" MasterPageFile="/masterpages/Master.master" AutoEventWireup="true" %>
<asp:content ContentPlaceHolderId="cphHead" runat="server">
<style type="text/css">
#content {
background: url('<%=VirtualPathUtility.ToAbsolute(<umbraco:Item field="background" runat="server" />)%>');
}
</style>
</asp:content>
Anyone know any solution? Thanks in advance.
If it's just a plain textstring then you can do the following:
<%# Master Language="C#" MasterPageFile="/masterpages/Master.master" AutoEventWireup="true" %>
<%# Import Namespace="umbraco" %>
<%# Import Namespace="umbraco.presentation" %>
<%# Import Namespace="umbraco.presentation.nodeFactory" %>
<asp:content ContentPlaceHolderId="cphHead" runat="server">
<style type="text/css">
#content {
background: url('<%=VirtualPathUtility.ToAbsolute(Node.GetCurrent().GetProperty("background").Value)%>');
}
</style>
</asp:content>
However, if the "background" property is a media picker then you need a little more help than just that, which will involve the umbraco.cms.businesslogic.media namespace. I suggest you check out the libraries with Reflector, or the source code repository on Codeplex to find out the classes you should be using and how to subsequently populate your property.
HTH,
Benjamin
Related
I have an aspx page with this tag in the <body>:
<asp:ScriptManager ID="scriptManager1" runat="server" />
Now elsewhere in the aspx page I have some VB code (embedded in the page using <% %>, not in a code behind vb file):
scriptManager1.RegisterClientScriptBlock(Me.GetType(), "mandatoryAdditionalFieldRules_ContactType", tempStr)
Problem is, I get a compile error on that line saying that scriptManager1 is not declared. I thought all controls with runat="server" are accessible in code? Why is this not working?
edit:
Oh, I figured it out - sort of. RegisterClientScriptBlock is a shared method of ScriptManager for some reason, so I need to call it like so:
ScriptManager.RegisterClientScriptBlock(Me.GetType(), "mandatoryAdditionalFieldRules_ContactType", tempStr)
Calling it as an instance method won't work.
However - now I am getting an error saying that the ScriptManager class itself doesn't exist! I am referencing the appropriate namespace like so at the top of the page:
<%# Import Namespace="System.Web.UI" %>
Why is ScriptManager not being found? I even get an error if I explicitly reference the class by namespace:
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me.GetType(), "mandatoryAdditionalFieldRules_ContactType", tempStr)
edit: here is a simple page which should reproduce the error:
<%# Page Language="VB" ContentType="text/html" ResponseEncoding="UTF-8" %>
<%# Import Namespace="System.Collections.Generic" %>
<%# Import Namespace="System.Data" %>
<%# Import Namespace="System.Data.SqlClient" %>
<%# Import Namespace="System.Web.UI" %>
<%# Import Namespace="System.IO" %>
<html>
<head>
<title>FRED</title>
</head>
<body>
<asp:ScriptManager ID="scriptManager1" runat="server" />
<%
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me.GetType(), "mandatoryAdditionalFieldRules_ContactType", "alert('fred');")
%>
</body>
</html>
edit: tried this as suggested, same error:
<%# Page Language="VB" ContentType="text/html" ResponseEncoding="UTF-8" %>
<%# Import Namespace="System.Collections.Generic" %>
<%# Import Namespace="System.Data" %>
<%# Import Namespace="System.Data.SqlClient" %>
<%# Import Namespace="System.Web.UI" %>
<%# Import Namespace="System.IO" %>
<script runat="server">
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreRender
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me.GetType(), "mandatoryAdditionalFieldRules_ContactType", "alert('fred');")
End Sub
</script>
<html>
<head>
<title>FRED</title>
</head>
<body>
<asp:ScriptManager ID="scriptManager1" runat="server" />
</body>
</html>
Based on the arguments you are trying to use in the method call, it looks a bit like you are mixing up two separate classes:
System.Web.UI.ScriptManager
System.Web.UI.ScriptManager docs link
and
System.Web.UI.ClientScriptManager
System.Web.UI.ClientScriptManager docs link
The first one of these two lives in the System.Web.Extensions.dll assembly. Have you checked that you are referencing this assembly?
The second one is exposed through the ClientScript property on the Page class. I think what you should be doing is this (note C# syntax, should be easy to change to VB though):
<%
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "mandatoryAdditionalFieldRules_ContactType", "alert('fred');");
%>
Check this example here, it looks like you may need to call it in Page_PreRender - the example is in C# rather than VB but the premise should be the same.
https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.scriptmanager.registerclientscriptblock?view=netframework-4.8
I took your sample with the call in Page_PreRender and with a few adjustments (different parameters for RegisterClientScriptBlock, putting a form with runat=server around the script manager) I was able to get it to work for me.
<%# Page Language="VB" ContentType="text/html" ResponseEncoding="UTF-8" %>
<%# Import Namespace="System.Collections.Generic" %>
<%# Import Namespace="System.Data" %>
<%# Import Namespace="System.Data.SqlClient" %>
<%# Import Namespace="System.Web.UI" %>
<%# Import Namespace="System.IO" %>
<script runat="server">
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreRender
System.Web.UI.ScriptManager.RegisterClientScriptBlock(scriptManager1, Me.GetType(), "mandatoryAdditionalFieldRules_ContactType", "alert('fred');", True)
End Sub
</script>
<html>
<head>
<title>FRED</title>
</head>
<body>
<form runat="server">
<asp:ScriptManager ID="scriptManager1" runat="server" />
</form>
</body>
</html>
I'm not sure why your parameters for RegisterClientScriptBlock were different though, so I'm wondering if there's a difference in .NET version? Still, hopefully this helps.
In my webpage I use two user controls:
ucControl1 and ucControl2. The ucControl1 control also contains an instance of the ucControl2 control inside it.
When you run the application and go to the page in question, it appears that only the instance that is inside the ucControl1 is working correctly. When trying to execute the functionality of the ucControl2 that is directly on the page, it correctly executes the backend code, opens the modal correctly but does not show the results on the screen.
What could be happening?
The page is like this
<%# Page Language="C#" AutoEventWireup="true" CodeFile="PageSample.aspx.cs" MasterPageFile="~/MasterPage.master" Inherits="PageSample" %>
<%# MasterType VirtualPath="~/MasterPage.master" %>
<%# Register Src="~/Controls/ucControl2.ascx" TagPrefix="uc2" TagName="ucControl2" %>
<%# Register Src="~/Controls/ucControl1.ascx" TagPrefix="uc1" TagName="ucControl1" %>
<asp:Content runat="server" ContentPlaceHolder ID="cpSampleID">
<div>
<uc1:ucControl2 runat="server" ID="uc2" />
</div>
<uc1:ucControl1 runat="server" ID="uc1" />
</asp:Content>
And the usercontrol1
<%# Control Language="C#" AutoEventWireup="true" CodeFile="ucControl1.ascx.cs" Inherits="ucControl1" %>
<%# Register Src="~/Controls/ucControl2.ascx" TagPrefix="uc1" TagName="ucControl2" %>
<%-- Something Something Something --%>
<uc1:ucControl2 runat="server" ID="ucControl2" />
I've found a solution. The modal is opened by javascript.
ucControl2
<%# Control Language="C#" AutoEventWireup="true" CodeFile="ucControl2.ascx.cs" Inherits="ucControl2" %>
<%# Register Src="~/Controls/ucControl2.ascx" TagPrefix="uc2" TagName="ucControl2" %>
<div id="modal">
<!-- Some Textboxes -->
</div>
Method to open the modal
public void OpenModal()
{
string id = "modal";
string js = $#"
$(document).ready(function() {{
if ($('.modal.in').length > 0) {{
$('.modal').on('hidden', function() {{
$('.modal').off('hidden');
$('#{id}').modal({{show: true, keyboard: false, backdrop: 'static'}});
}});
$('.modal').modal('hide');
}} else {{
$('#{id}').modal({{show: true, keyboard: false, backdrop: 'static'}});
}}
}});";
ScriptManager.RegisterStartupScript(this, this.GetType(), id, js, true);
}
So when the control is called, the javascript does not know which of the controls should open, and in this case it is opening the first control that is on the page.
To solve it I've changed the div through an asp panel, in this way asp generates a different client ID for each control and the controls are displayed correctly.
I got one problem, I have three user controls say Control1,control2, control3.
I want to make Control1 into 3 divs and see contol1 UI in first part
control2-2nd part of div
Control3- in 3 Part of div
Is it possible?
I wanted to do this just for the sake i can maintain with less code .Is there any alternative .other this which will suit me best.
Please suggest
Yes, you can put multiple user control on a single user control. However your question is down voted the solution of your problem is as given below :
Create a sample website. Add Webusercontrol1.ascx ,Webusercontrol2.ascx, Webusercontrol3.ascx, Webusercontrol4.ascx in it and modify the code as given below.
Default.aspx page html
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="SO_1._Default" %>
<%# Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
</asp:Content>
UserControl-1 Html : This is container user control and this will have all required user control on it.
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs"
Inherits="SO_1.WebUserControl1" %>
<%# Register Src="WebUserControl1.ascx" TagName="WebUserControl2" TagPrefix="uc1" %>
<%# Register Src="WebUserControl2.ascx" TagName="WebUserControl2" TagPrefix="uc2" %>
<%# Register Src="WebUserControl3.ascx" TagName="WebUserControl3" TagPrefix="uc3" %>
<%# Register Src="WebUserControl4.ascx" TagName="WebUserControl4" TagPrefix="uc4" %>
<p>
<b>DIV-1 Container User control</b></p>
<div>
<b>DIV1</b>
<br />
<uc2:WebUserControl2 ID="WebUserControl21" runat="server" />
</div>
<div>
<b>DIV2</b>
<br />
<uc3:WebUserControl3 ID="WebUserControl31" runat="server" />
</div>
<div>
<b>DIV3</b>
<br />
<uc4:WebUserControl4 ID="WebUserControl41" runat="server" />
</div>
UserControl-2 html
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="SO_1.WebUserControl2" %>
User Control-1
UserControl-3 html
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="SO_1.WebUserControl2" %>
User Control-2
UserControl-4 html
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="SO_1.WebUserControl2" %>
User Control-3
I have many UserControls and I wanna load them depended on query string in one aspx page.
I tired 2 following ways:
Add a Placeholder in aspx page and in Page_Load/Page_Render event:
UserControl uc = (UserControl)LoadControl("/PATH/ProductGroups.ascx");
phMain.Controls.Add(uc);
Add a MasterPage, then add an aspx and using of masterpage, then register the UserControl on the aspx
But in both, I got following error:
The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.
BTW, I have used telerik TextEditor in the loaded UserControl.
How I can handle that?
I think the only way that I have, multiple aspx per UserContorl, without MasterPage, this is sucks, as you know ! because I have to fix HTML/CSS/JS in all pages, one by one and this is not pro !
NOTE: without using telerik TextEditor, everything working fine, but I need this one, also this wat (loading UserControl in PlaceHolder) is not really good way.
I'm looking for something like DNN.
I tried to replicate this error, but i couldn't. It is just working fine for me. Here is what I tried in VS2013.
1) Added MasterPage
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="Test.Site1" %>
<%# Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
2)Added WebUserControl
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="Test.WebUserControl1" %>
<%# Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<telerik:RadEditor runat="server" ID="RadEditor1" SkinID="DefaultSetOfTools" Height="675px">
</telerik:RadEditor>
3) Added WebForm
<%# Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="Test.WebForm3" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:PlaceHolder runat="server" ID="phMain" />
</asp:Content>
4) Loaded the control
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Test
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UserControl uc = (UserControl)LoadControl("~/WebUserControl1.ascx");
phMain.Controls.Add(uc);
}
}
}
Here is the resolution for the error "Control collection cannot be modified"
I have the folling code in my content page:
- masterpage
- strong type of masterpage
- ajaxcontroltoolkit
I think the error is caused by the fact that I have
<%# MasterType VirtualPath="~/dashboard/ProfMaster.master" %>
in the code. But I do not know how to solve it. I read some workaround like http://www.west-wind.com/weblog/posts/2006/May/27/The-Controls-collection-cannot-be-modified-because-the-control-contains-code-blocks-ie-
but that does not solve it.
Any ideas?
My
Code:
<%# Page Title="" Language="C#" MasterPageFile="~/dashboard/ProfMaster.master"
AutoEventWireup="true" CodeBehind="profEditArticle.aspx.cs"
Inherits="NoteBook.dashboard.profEditArticle" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit.HTMLEditor"
TagPrefix="cc1" %>
<%# MasterType VirtualPath="~/dashboard/ProfMaster.master" %>
<asp:Content ID="Content2" ContentPlaceHolderID="CPHPROFRIGHT" runat="server">
<cc1:Editor ID="Editor1" runat="server" />
</asp:Content>