Content getting outside ContentPlace - ASP.NET - asp.net

I'm on VS 2008, Windows Xp.
My page:
<asp:Content runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<%= WriteReport()%>
<%= "Hellooo" %>
</asp:Content>
Code-behind:
public string WriteReport()
{
StringWriter swriter = new StringWriter();
using (HtmlTextWriter hwriter = new HtmlTextWriter(swriter))
{
//Write a table...
}
return swriter.ToString();
}
The funny thing is the "Heloo" goes in the contentPlace, but the method's return does not. It's place below the div of the ContentPlace (ends up in the footer div).
Thank you for your help.
EDIT----------------------
Temporary solution
<%= WriteReport()%>
<asp:Table runat="server" ID="testeme">
</asp:Table>
I added a bogus table (id=testeme), it does nothing and has nothing in it. But it works now, what the hell. Only works when below the method.
EDIT2--------------------
I tried to guess the relevant parts from the MasterPage and placed them here.
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="MyAppV2.Views.MasterPage" %>
<div id="general">
<form id="form1" runat="server" enctype="multipart/form-data">
<div id="main">
<div>
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</div>
</form>
</div>
<div id="footer">
<p>MYFOOTER</p>
</div>
First line of the view using the master page:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MP5._Default" MasterPageFile="~/MasterPage.Master" %>

Instead of injecting code into your page using <%= ... %>, you can use an <asp:literal> tag:
<asp:Content runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<asp:Literal id="MyReport" runat="server" />
</asp:Content>
Then you can write to it from your code-behind using the text property. You can call the WriteReport() method from within Page_Load.
public void WriteReport()
{
StringWriter swriter = new StringWriter();
using (HtmlTextWriter hwriter = new HtmlTextWriter(swriter))
{
//Write a table...
}
MyReport.Text = swriter.ToString();
}
That should solve any oddities of where the code ends up on the page.

Related

Two instances of the same usercontrol in an asp.net page

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.

Implementation Plugin (UserControl) based system in ASP WebForm

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"

The state information is invalid for this page and might be corrupted (IE only)

I've see all of the other pages with this questions, but no amount of research has solved my problem. I am getting the "The state information is invalid for this page and might be corrupted" error. My page loads fine the first time. The page contains a div that has tabs that change the info on the page and the tabs are loaded dynamically and are linked to a different page. The tabing uses jquery. Without changing anything on the page, when I try to reload this page by click on the link that gets to this page or even by clicking on ANY link in the menu bar that goes to other pages, I this error. Again, this only happens in IE and I've put break points all over my code and it never goes back to the server.
<%# Page Language="C#" MasterPageFile="~/Templates/Main.Master" AutoEventWireup="true"
CodeBehind="Reporting.aspx.cs" Inherits="Pegged.Reporting" Title="Workforce Planning Report" %>
<%# Register Src="Controls/ucWebPageNavigation.ascx" TagName="ucWebPageNavigation"
TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript">
$(function () {
$("#tabs").tabs({
ajaxOptions: {
error: function (xhr, status, index, anchor) {
$(anchor.hash).html(
"Couldn't load this tab. We'll try to fix this as soon as possible.");
}
}
});
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div id="IconLinks" class="TopLinks">
<uc1:ucWebPageNavigation ID="ucWebPageNavigation1" runat="server" />
</div>
<div id="divContainerReport" class="container_12">
<!--Facility Tabs-->
<div id="tabs">
<ul id="FacilityTabs" runat="server" ></ul></div>
</div>
</asp:Content>
This is the code that loads the tabs on page load:
ServicesDLL.Services ps = new ServicesDLL.Services();
string encryptString = "";
string encryptString2 = "";
encryptString = ps.EncryptQueryString("Test");
encryptString2 = ps.EncryptQueryString("Testing");
string innerhtml = "<li><a id=\"id1\" href=\"ReportFacility.aspx?facility=" + encryptString + "\">Test</a></li><li><a id=\"id2\" href=\"ReportFacility.aspx?facility=" + encryptString2 + "\">Testing</a></li>";
FacilityTabs.InnerHtml = innerhtml;
And this is the page the tabs load:
<%# Page Title="" Language="C#" MasterPageFile="~/Templates/Popup.Master" AutoEventWireup="true" CodeBehind="ReportFacility.aspx.cs" Inherits="Pegged.ReportFacility" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div id="divReportView" class="grid_8 BlueBorder">
<p id="testText" runat="server"></p>
</div>
</asp:Content>

ASP.NET Repeater Eval

I am trying to execute the following code in a .aspx page:
<asp:Repeater ID="rptComentarios" runat="server">
<ItemTemplate>
<% if (Convert.ToInt32(Eval("int_tipo")) == 1)
{ %>
<div class="resp">
<div class="top">
</div>
<div class="cont-resp">
<h3>
<%# Eval("txt_nome") %></h3>
<p>
<%# Eval("txt_comentario") %></p>
</div>
</div>
<% }
else
{%>
<div class="usuario">
<div class="top">
</div>
<div class="cont-usuario">
<h3>
<%# Eval("txt_nome") %></h3>
<p>
<%# Eval("txt_comentario") %></p>
</div>
</div>
<% } %>
</ItemTemplate>
</asp:Repeater>
It throws a runtime exception in the first line:
<% if (Convert.ToInt32(Eval("int_tipo")) == 1)
System.InvalidOperationException: Databinding methods such as Eval(), XPath() and Bind() can only be used in the context of a databound control.
What's wrong? Any ideas?
I had a similar problem and the following code worked for me:
<asp:Repeater ID="rptComentarios" runat="server">
<ItemTemplate>
<asp:PlaceHolder ID="placeholderBlaBlaBla" runat="server" Visible='<%# Convert.ToInt32(Eval("int_tipo")) == 1 %>'>
Your optional HTML
</asp:placeholder>
Other HTML
</ItemTemplate>
</asp:Repeater>
Some more comments:
Please note that single quotes are used to define the value Visible attribute of asp:placeholder. I tried double quotes too and they didn't work.
Anytime you want to get some optionally displayed HTML you should use a control to show/hide it. asp:placeholder works fine for that purpose. Don't ever do <% if(..) { %> - this is evil.
<%# ... %> is used to calculate or display expressions inside a repeater. These expressions can be displayed as HTML or passed as attributes of server side controls. You can't use if inside it.
I think there needs to be a # sign for the enclosure <%# ..Eval...%>
Or try the full Eval version
<%# if (Convert.ToInt32(DataBinder.Eval(Container.DataItem, "int_tipo"))
== 1) { %>

How can I enable live preview for FCKeditor in an ASP.Net site?

Over in this question, Scott writes that it is possible to get the current HTML for what's written in the FCKeditor by using FCKeditorAPI.__Instances['instanceNameHere'].GetHTML();
Could someone provide step-by-step instructions on how to accomplish this in an ASP.NET page? All I currently have so far in the .aspx file is this:
<%# Register Assembly="FredCK.FCKeditorV2" Namespace="FredCK.FCKeditorV2" TagPrefix="FCKeditorV2" %>
<%# Page Title="" Language="C#" ... %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create a new piece</h2>
<form id="form1" runat="server">
<FCKeditorV2:FCKeditor ID="FCKeditor1" runat="server">
</FCKeditorV2:FCKeditor>
<input id="Submit1" type="submit" value="Submit" runat="server" />
</form>
</asp:Content>
In javascript
Well you can do this here:
<script type="text/javascript">
var oEditor = FCKeditorAPI.GetInstance(’IDFromFCKEditor’);
oEditor.Events.AttachEvent( 'OnSelectionChange', function() {
document.getElementById("PreviewDiv").innerHTML = oEditor.GetHTML(true);
}) ;
</script>
Source http://docs.fckeditor.net/FCKeditor_2.x/Developers_Guide/JavaScript_API

Resources