Load .ascx from string (SQL) - c#

I like to to ask that is there a way to load an ascx control from string? To be more precise: I like to make/store a new .ascx in sql then load it to the website.

Yes, you should be able to use the ParseControl function to do what you want:
http://msdn.microsoft.com/en-us/library/kz3ffe28.aspx
Example from MSDN:
<%# Page language="c#" Debug="true" %>
<!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>
<title>ASP.NET Example</title>
<script runat="server">
// System.Web.UI.TemplateControl.ParserControl;
// The following example demonstrates the method 'ParseControl' of class TemplateControl.
// Since TemplateControl is abstract, this sample has been written using 'Page' class which derives from
// 'TemplateControl' class.
// A button object is created by passing a string to contstruct a button using ASP syntax, to the
// 'ParseControl' method. This button is added as one of the child controls of the page and displayed.
void Page_Load(object sender, System.EventArgs e)
{
Control c = ParseControl("<asp:button text='Click here!' runat='server' />");
myPlaceholder.Controls.Add(c);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:placeholder id ="myPlaceholder" runat="server" />
</form>
</body>
</html>

Related

asp:Button how to capture Response.WriteLine

I am using the OnClick of the asp:Button tag to compute a report which I would like to display as an innerHTML of a div tag. I would like to use the Response.WriteLine method to return this html file. Does ASP.NET webforms provide a way of capturing this response?
Am I forced to use JavaScript instead? I am using C# on the Server side.
The Web Forms way of doing this is to use a control and set its InnerHTML from the code behind. Here's a self contained example:
<%#Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
protected void GenerateReport(object sender, EventArgs e)
{
var reportHTML = "<p>This is a <span style='font-weight: bold;'>report!</span></p>";
ReportDiv.InnerHtml = reportHTML;
}
</script>
<!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>
<title>Test</title>
</head>
<body>
<form runat="server">
<asp:Button runat="server" OnClick="GenerateReport" Text="Generate Report" />
<div runat="server" id="ReportDiv"></div>
</form>
</body>
</html>

How to respond without html in asp.net

Easy question perhaps.
Okay, I have a post to my page and need to respond with one string.
in php, you could simply do something like this:
<?php
die ("test");
then you can place this page on a webserver and access it like this:
localhost/test.php
so, I need to do exact same thing in c#.
When I try to respond with:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("test");
Response.End();
}
I'm getting: "<html><head><style type="text/css"></style></head><body>test</body></html>"
as a response.
How can I make asp.net to just return exact response, without html?
I know that I probably missing some basic knowledge, but cannot find anything online.
You can clear the previous response buffer and write your new output.
Response.Clear(); // clear response buffer
Response.Write("test"); // write your new text
Response.End(); // end the response so it is sent to the client
Make sure in your *.aspx file, at the top you have AutoEventWireup="true", if it's false (or not there?) your Page_Load event handler will not be called.
Also, make sure you compiled your page.
Another suggestion is to use a Generic Handler (ie *.ashx), these do not use the typical webforms lifecycle and might be better suited to what you're doing.
I think you're looking for :
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/plain";
Response.Write("test");
Response.End();
}
For me it only Generates actual text in response.write(); statement. I am uploading the complete code for clarity.
Visual Studio: 2010
Code Behind:
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("I CAN ONLY SEE THIS NO OTHER HTML TAG IS INCLUDED");
Response.End();
}
}
HTML CODE
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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>
</div>
</form>
</body>
</html>
OUTPUT & HTML Source:
I CAN ONLY SEE THIS NO OTHER HTML TAG IS INCLUDED
I am getting the desired result. I have tried this code with Master-Page also i get the same result.
Please make sure your AutoEventWireup="true" if i turn this false then HTML SOURCE changes to this
<!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><title>
</title></head>
<body>
<form method="post" action="Default2.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZGivF0fgbeE6VebNR51MYSu3yJdsZ9DwEtIPDBVRf4Vy" />
</div>
<div>
</div>
</form>
</body>
</html>
As the previous answer suggests above, you need AutoEventWireup="true" as well as Response.End() in code behind.

Adding masterpage to aspx webpage

I am currently referencing this website for AD role management.
The C# code is working fine, but when I pasted the code inside my webpage along with the masterpage, the page gave me an error which says:
Content controls have to be top-level controls in a content page or a nested master page that references a master page.
May I know how should I set my masterpage in this case?
.ASPX page without masterpage:
<%# Page Language="C#" %>
<%# Import Namespace="System.Web.Security" %>
<%# Import Namespace="System.Web.UI" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
string[] rolesArray;
MembershipUserCollection users;
string[] usersInRole;
public void Page_Load()
{
....
}
public void AddUsers_OnClick(object sender, EventArgs args)
{
....
}
public void UsersInRoleGrid_RemoveFromRole(object sender, GridViewCommandEventArgs args)
{
....
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Role Membership</title>
</head>
<body>
<form runat="server" id="PageForm">
<font face="helvetica" size="6" color="#455c75"><strong>Role Membership</strong></font><font face="helvetica" size="5" color="#455c75"><strong> Management</strong></font>
<br /><asp:Label ID="Msg" ForeColor="maroon" runat="server" /><br />
<table cellpadding="3" border="0">
...
</table>
</form>
Masterpage:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MainPage.master.cs" Inherits="MainPage" %>
<!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 id="Head1" runat="server">
<title>SOD</title>
<link href="Styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
...
</body>
</html>
EDIT:
I tried inserting MasterPageFile="~/MainPage.master" into the first tag, which start my .aspx page with <%# Page Language="C#" MasterPageFile="~/MainPage.master"%>, giving me the error mentioned above.
The Masterpage contains HTML code and content areas. The pages that use the masterpage must, MUST, have all code in an <asp:Content> tag. You can't have any sort of code outside of the Content tag.
You need to place your code inside an <asp:Content ID="content1" ContentPlaceHolderID="Content_pageBody" Runat="Server"></content> tag. This tag should map to your master page, where ContentPlaceHolderID = the place holder name in your master page

add JavaScripts files to the body dynamically instead of to the head

I have done enough research and have decided to move the JavaScript to the bottom of the page.
Here is why i should do it http://developer.yahoo.com/performance/rules.html#js_bottom
I wrote the code below, this code only can add the JavaScript within the header.
How do I add the JavaScript before the </body> of the page? I am thinking about using
The code below will adding the JavaScript to the header not the body.
public static string AddJavascript(string filename, Page ThisPage)
{
HtmlGenericControl js = new HtmlGenericControl("script");
js.Attributes["type"] = "text/javascript";
js.Attributes["src"] = filename;
ThisPage.Header.Controls.Add(js);
//Add to bottom of the page.
return null;
}
protected void Page_Load(object sender, EventArgs e)
{
AddJavascript("myjd.js", Page);
}
I like to produce something like below this dynamically. I have several JavaScript files so i really like to keep them separate.
<!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>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
</title>
</head>
<body>
<div>
my content here
</div>
<!---javascript should go below here-->
<script type="text/javascript" src="myjd.js">
</script>
</body>
</html>
Use ThisPage.ClientScript.RegisterStartupScript(this.GetType(), "myjd", "[script]", true) to register it at the end (just before closing of form tag).
You could create a placeholder at the bottom of your page and then use you code to add the js to the placeholder.

Dispay html textbox with value from code behind using c#

How can I display an html textbox with value obtained from code behinh?
As long as your string value is not private, you should be able to do something like this
<input type="text" value="<%= YourStringValue %>" />
You could use an actual <asp:Textbox /> and set it's "text" value directly from the code behind. If you want to directly inject text into a "normal" html textbox (or anywhere else for that matter), you can use <%= SomeValue %>. Yet another way is to include the "runat=server" attribute on standard html elements, allowing you to manipulate them from the codebehind.
Normally I'd just go for the built-in ASP textbox control so I don't have to worry about persisting values/wiring up viewstate/etc. Injecting dynamic content into plain html elements tends to be an edge case requirement...
<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//assign text value here
txt1.Value = "hello world!";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="txt1" runat="server" type="text" />
</div>
</form>
</body>
</html>

Categories

Resources