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

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" %>

Related

Rendering an aspx page in razor view (cshtml)

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.

How to access web page public variable from user control in asp .net

How to access the web page public variable from user control in asp .net ?
Your page class derived from asp.net Page has this variable.
Every control has a Page property. If you know that this control will be placed on a particular class derived from Page, you can always cast it to the proper type. Then you have access to the public variable.
var propValue = ((MyControlBasePage)Page).MyProperty;
Ensure that the base class for the pages on which you use this control is Page derived form MyControlBasePage.
If you plan to use this on only one Page you could always directly use
var propValue = ((MyPage)Page).MyProperty;
But then, whats the user of a UserControl that you use on only one page.
you can do like this:
<%= this.usercode %>

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 !).

Can not get Request.Form.Get valus from aspx page that wrappe withe ContentPlaceHolder (form error)

I am writing a web app in asp.net I have a master page that contain a ContentPlaceHolder
and a form that wrapper the ContentPlaceHolder, In a aspx page I realize the ContentPlaceHolder and have some controls in this page.
Now when I Trying to use Request.Form.Get("my control name") (from the aspx behind code), I get null.
If I try to add a form in the aspx page I get an error that say you can have only one form in a page.
How can i get the values in my controls??
thanks for the help.
Request.Form("YourControlName") will not work with server controls because ASP.NET adds some extra stuff to the name of your control when it outputs it to the page. It does this to make sure that the name remains unique across all the controls on the page. So, for example, your control might actually be named something like "ctl00_maincontent_placeholder1_YourControlName" when it gets created on the page.
In ASP.NET this is not usually a problem because you typically do NOT use Request.Forms to get your control values. Instead you use the server control's methods to get the values. So, for a textbox, you would use YourControlName.Text to get the value that was entered into the textbox.
If you are just trying to communicate a value between the master and page, assuming the value is on the master you can cast Page.Master to the correct type. On the master page you can wrap controls on the master.
MasterPage
public string MyControlText
{
get
{
return myControl.Text;
}
set
{
myControl.Text = value;
}
}
On the page
((MyMasterPage)this.Page.Master).MyControlText = "To master from page";
string fromMasterToPage = ((MyMasterPage)this.Page.Master).MyControlText;

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