Rendering an aspx page in razor view (cshtml) - asp.net

I need to render a crystal report in a asp.net mvc application. Since mvc does not provide the support for reportviewer.I'm forced to redirect to an aspx page.However i'm losing the layout.cshtml menu in that redirect .
Is there anyway we can do show the partial view of aspx page on the same razor page , in the same window?
Iframe is one solution that I'm aware but do not want to use it as it is not a good practice to use iframes in modern apps.
Thanks.

Well, if iframe isn't a practical option for you, then you perhaps should consider creating a WebForms Master page specifically for your report page(s).
Then on that Master page, you can set its default PlaceHolder controls to invisible and explicitly render the Razor layout view within server tags.
<% var buildTitle = new StringBuilder();
var buildMain = new StringBuilder();
TitleContent.RenderControl(new HtmlTextWriter(new System.IO.StringWriter(buildTitle)));
ViewBag.Title = buildTitle.ToString().Trim();
MainContent.RenderControl(new HtmlTextWriter(new System.IO.StringWriter(buildMain)));
ViewBag.MainContent = buildMain.ToString().Trim();
%>
<%= Html.Partial("YourRazorLayoutView", viewData: ViewData)%>
For more details, please go check out this blog.

Related

Is it possible to render a web control dynamically?

We have a web control that we want to render into a div on the already loaded page. This needs to happen when the user clicks on a hyperlink in the menu.
The web control has some javascript files that are added to it dynamically in the C# code.
We also arent using aspx files, and want to load the web control onto a normal html page.
Please let me know what would be the best way to do this, or direct me to an article as I'm struggling to find anything useful on the web. any help is appreciated.
You can render your control dynamically in a string, send the string back and place it inside your div. Using ajax you call the code behind to render the control and return you the results.
// load the control
var oCConrol = Page.LoadControl("CustomControl.ascx");
// here you need to run some initialization of your control
// because the page_load is not loading now.
// a string writer to write on it
using(TextWriter stringWriter = new StringWriter())
{
// a html writer
using(HtmlTextWriter renderOnMe = new HtmlTextWriter(stringWriter))
{
// now render the control inside the htm writer
oCConrol.RenderControl(renderOnMe);
// here is your control rendered output.
strBuild = stringWriter.ToString();
}
}
Alternative you can get the same results by just make an extra empty aspx page with only your control inside and call it using Ajax. The result will be again the control
The javascript how ever is dificult and can not go with the control. You can not send text back and then make it run as javascript inside the control, this must be done separated.
I have done something similar by creating a separate, standalone .aspx page and Jquery ajax. Call to the .aspx page using Jquery ajax and return all the rendered HTML from that page via the ajax response.
As far as I know, there is not any way to render a web control outside the context of a .aspx page.

ASP.NET and ajax response

I have a main web page ("Base Page") that makes an ajax call (using jQuery) to the server. The server-side page ("Ajax Page") is an ASP.NET web form (no MVC in use here). Because (in this case) I'm using a GridView to render data within the ajax response, I have to include the <form runat="server"> tag.
My complaint is this--when the Base Page receives the AJAX response, it inserts it into the DOM, but because the AJAX response is comprised of HTML markup that includes a <form> tag, this sometimes results in nested html forms, which is bad.
I realize that I can use jquery to only insert a fragment of the response into the Base Page DOM--or that I could use jquery to subsequently strip out the offending <form> tag. But these feel like klunky work-arounds. Is there really no way to prevent the ASP.NET page from serving out its response with a <form> tag? I realize that the form tag is the heart of the ASP.NET webform model, but it sure makes using AJAX in ASP.NET a complicated affair--very much swimming upstream. Surely Microsoft has realized that the postback / server-side model is a thing of the past?
I also realize that Microsoft has some server-side AJAX libraries that probably address this issue--but I'd still like to know if there's a solution native to ASP.NET webforms.
Any suggestions?
You can add your GridView to a Web User Control and then render it to a string like this:
public static string ExecuteToString(this Control control)
{
Page page = new Page();
page.Controls.Add(control);
StringBuilder sb = new StringBuilder();
using (StringWriter writer = new StringWriter(sb))
{
HttpContext.Current.Server.Execute(page, writer, false);
}
return sb.ToString();
}
This means you don't need to point your ajax request to a page. You can use a web service. Query a specific method and then dynamically load the User Control, render it to a string and return the HTML. Because you put your HTML and code in a User Control you don't need to worry about stripping out form tags and you can still use all the asp controls as you would on a page.
I have no idea about the performance costs of using this method but I've been using it for a while and it seems fine to me.
Working with WebForms & AJAX for many years I can understand your frustration.
Usually when working with loading WebForm pages using jQuery AJAX, I wrap an ajax class around my page, just nested inside the form:
<html>
<head></head>
<body>
<form runat="server">
<div class="ajax">
Content here..
</div>
</form>
</body>
</html>
Then when I load the page, I call just this ajax class:
$("element").load("http://domain.com/webpage.aspx .ajax");
This means the form element isn't rendered into my page, preventing nested form issues, but your GridView can still be rendered into HTML successfully.
I was dealing with the same kind of issue, I resolved it the following way:
Response.Write("whatever content")
Response.End()
This will send ONLY what you put in "Response.Write()"... You're still free to alter headers, etc.
If your ajax server page is an aspx page(ex : ajaxpage.aspx) , you can remove the HTML makup in the ajaxpage.aspx except the first line which mentions the Page directive. So it should be something like this.
<%# Page Language="C#" CodeBehind="ajaxpage.aspx.cs" Inherits="MyProject.ajaxpage" %>
And in the code behind, you can return the data using response.Write
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("your markup to return here");
}
If it is to return some kind of (clean) HTML markup / some part of data, I am inclined to use a generic handler (ex : ajaxresponse.ashx )file instead of an aspx file and return data from that
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("<h3>Here is the Markup for UserDetails</h3>");
}
You can not use ASP.NET server controls on an ashx file. And you can not have a page to have a server control (ex : GridView ) without a form tag. It should be placed inside a form tag. Note that your server controls like gridview is going to render an HTML table markup only.
Personally if i want to get some clean & controlled HTML markup which i want to show /inject into my current page using ajax, i will use an ashx handler and write my code to output my required markup. ASHX files has performance advantages compared to a aspx file as it wont go thru the normal ASPX page life cycle ( All those events !).

How to generate a full ASP.Net Webform via Server Controls?

I need to make a full asp.net webform with Server Control. I Wonder how to do this?
There is a class called Page in .Net but I don't know how to work with it.
A Page or WebForm must have a URL so from other pages can be accessible. Who knows this?
Your problem is to generate your page without using aspx, am I right ?
If you want your page to be accessible from your WebServer, you will need the .aspx file, even if you completely generate your page by code.
To create your page, you create a class who inherits from the Page class, and you add your server controls to it.
public class TestCodePage : Page
{
public TestCodePage()
{
HtmlForm form = new HtmlForm();
LiteralControl l = new LiteralControl("I write a text in my form");
form.Controls.Add(l);
this.Controls.Add(form);
}
}
In order to have an url for this page, you just need to create an aspx page with this content :
<%# Page Inherits="MyWebApp.TestCodePage" %>

Where to put Datagridview be Place in ASP.NET Webforms?

I am studying ASP.net webforms and i am confused where to placed Datagridview and other Controls
from webpage.aspx or in a Control.ascx .
//Which Validation and CRUD operations was performed
Which is the Best Practice.
Thanks in Regards
ascx
- fragment
- re-usable you can place it anytime in your webpages(aspx)
- Need to attached to aspx to make it works
aspx - the actual page it self which you call on your browser which your ascx are placed
have a look at this same post:
Post 1
post 2
Building User Controls
Regards
You can put the thing in both but its depends on your requirement
aspx - is asp.net page so when you put control on it , it get utilize with in that page only i.e not resuable.
ascx - is user control , i.e usable control with in your application . once you design it you can use it in any page of by registering it on page.
so go for ascx when its resuable and going to use in no pages. go for aspx when its page specific.
If you want your DataGridView (maybe including some search form, button) appear on every page. Then use ascx. Else aspx. Ascx is a page fragment, not page!

Render a ASP.NET control in the master page code-behind

I'm using a Substitution control in my master page, and I want to render a user control content (related to the login area of my website) in the Substitution .
Seems like I must have a reference for the requested page so that it could render the control. But I need to render the control in the master page itself, as it's shared across multiple pages in my website. What are the guidelines to achieve that?
Tks
So you want to render a user control from your MasterPage code-behind, and add it to a Substitution that's also in the master page? Why do you need a reference to the page that's using the master?
Assuming your using VB and that I understand your question, try this in your MasterPage code-behind:
Dim someControl As MyControl = CType((New Page()).LoadControl("~/Path/To/MyControl.ascx"), MyControl)
mySubstitution.Controls.Add(someControl)

Resources