Weird question. I need to use a JavaScript array with ID's in it, to fetch additional information from a database, using the ID's as the row ID.
I need to then use this additional information and send it to another file (aspx) using Ajax, which will then use this information to rotate images.
Unless I can use ASP Classic, and ASP.NET (C#) in the same file?
- Or can I use the more or less same ASP code to access my database?
rotate script
<%# Page Language="C#" Debug="true" %>
<%# Import Namespace="System" %>
<%# Import Namespace="System.Drawing" %>
<%# Import Namespace="System.Web" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//string url = Request.QueryString["url"];
string url = #"C:\inetpub\wwwroot\testing\image.jpg";
string rotate_dir = Request.QueryString["dir"];
//create an image object from the image in that path
System.Drawing.Image img = System.Drawing.Image.FromFile(url);
//Rotate the image in memory
if (rotate_dir == "clockwise")
{
//Rotate clockwise
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
} else if (rotate_dir == "anticlockwise")
{
//Rotate anti-clockwise
img.RotateFlip(RotateFlipType.Rotate90FlipXY);
}
//Delete the file so the new image can be saved
System.IO.File.Delete(url);
//save the image to the file
img.Save(url);
//release image file
img.Dispose();
}
</script>
What I use to access my database
'Create connection and load users database
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.ACE.OLEDB.12.0"
conn.Open Server.MapPath("/nightclub_photography/data/database/jamsnaps.mdb")
Hope you understand what I'm trying to do?
As you should know you can freely use Javascript (and thus ajax) in asp-classic.
this means you can easily do the following;
set conn = Server.CreateObject("ADODB.Connection")
set rs = Server.CreateObject("ADODB.Recordset")
conn.open Server.MapPath("foobar path") + ", Microsoft.ACE.OLEDB.12.0"
%><script type="text/javascript">
rotationArray = Array(id_Array.length);
for(int i = 0; i < id_Array.Length; i++
{
<% rs.open("SELECT rotation FROM images WHERE id="+ id_Array[i])%>
rotationArray[i] = <%= rs("rotation") %>;
<%rs.close() %>
}
//send rotationArray via ajax
but in general I would suggest you use Database tools for asp.NET instead.
then you just send your JS-IDArray to the aspx file and do the processing there.
for reference you can check here
You can use C#.Net and ASP together. Its not a very nice way to do it. My understanding is you have to create the C#project first then add any asp pages. This will allow you to call your C# pages from within the asp application.
I havent personally done one of these but I have seen it done for sure so I know its technically possible.
Related
I am new to ASP.NET and user controls. I am trying to generate a javascript array from my C# code.
On the main .aspx page I have this:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="main.aspx.cs" Inherits="main" %>
<%# Register Src="~/table.ascx" TagPrefix="uc1" TagName="myTable" %>
Then on my table.asc.cs I have this:
protected void Page_Load(object sender, EventArgs e)
{
(...)
this.LoadDataFromDB();
(...)
}
private void LoadDataFromDB()
{
(...)
Response.Write(array);
(...)
}
My problem is that the array is being written before the <html> tags. It still works fine, but, how could I put it inside the <head> tags for instance?
Thank you
UPDATE:
I added this to my main.aspx
<asp:Literal ID="Literalarray" runat="server" Mode="PassThrough" Text="" />
and this to my ascx.cs:
Literal Literalarray= new Literal();
Literalarray.Text = output;
What am I missing?
Use a Literal control instead of Response.Write. Place it on your control somewhere and set its Text property.
You have to place it on your control, not on your page and you don't need to reinitalize it.
This code in the ascx.cs:
Literal Literalarray= new Literal();
Literalarray.Text = output;
should be:
Literalarray.Text = output;
Which should be in the Page_Load as a designer file will declare the literal type and allocate the space for it. By declaring a new one, the old one may be hidden. Also, be aware that if you are generating a JavaScript array that you also generate the script tags as part of the output as a literal doesn't do much decorating around the result.
I'd probably suggest putting a literal in the head on the main.aspx and load the data in there that way for one idea.
You could also do dynamic controls so that in the table.ascx.cs you create a Literal like you did previously and then add that to the head of the page assuming the head tag has a "runat=server" attribute so the code behind can use it. I'm pretty sure that in the code behind for the table you could do something like this:
Literal Literalarray= new Literal();
Literalarray.Text = output;
this.Page.head.AddControl(Literalarray);
i'm seeing a really strange issue with asp.net rendering. i have EXACTLY this in the relevant part of the .aspx (just replaced names of paths and controls):
<div id="header">
<% if (SiteSettings.SiteName.Equals("sx") || SiteSettings.SiteName.Equals("sw"))
{ %>
<sc:sublayout runat="server" renderingid="{B04CFA1A-6B5B-49D3-8000-339DBE9899C1}"
path="/layouts/AX/HeaderSublayout.ascx" id="AXHeader" placeholder="content"></sc:sublayout>
<% }
else
{ %><!-- bla1 --><ax:strangeBehavingControl id="HeaderInclude" runat="server" IncludeType="Header" /><!-- bla2 -->
<% } %>
</div>
the rendered html looks like:
<!-- bla1 -->
""
expected content from strangeBehavingControl
<!-- bla2 -->
the .ascx for strangeBehavingControl is really simple:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="strangeBehavingControl.ascx.cs" Inherits="layouts.strangeBehavingControl" %>
no extra spaces anywhere, checked already many times. the code behind is also really simple:
public partial class strangeBehavingControl: System.Web.UI.UserControl
{
protected override void Render(HtmlTextWriter writer)
{
var filePath = GetFilePath();
if (!string.IsNullOrWhiteSpace(filePath) && File.Exists(Server.MapPath(filePath)))
Response.WriteFile(filePath);
}
}
so i was thinking that the strange "" where inside the rendered included files, but i checked them manually and they start with the expected characters. any idea how can those characters being inserted there?
You are generating markup for your control incorrectly. Your render method should be using the HtmlTextWriter instance given to it, and not using any direct output writing method on Response.
It also looks like a rather strange setup, as you are writing out the contents as a UserControl, meaning it will be rendered within a page. Is it correct to assume you are always outputting either valid HTML or plain text?
I would suggest you change your Render method as follows:
protected override void Render(HtmlTextWriter writer)
{
var filePath = GetFilePath();
if (!string.IsNullOrWhiteSpace(filePath) && File.Exists(Server.MapPath(filePath)))
using (var sr = new StreamReader(filePath))
writer.Write(sr.ReadToEnd());
}
Sometimes the control file has another charset.
Verify that your file does not have extra characters at the start, and if so remove them.
You may have to use a smart text editor that allows you to do that.
I had the same extra space problem, but my control was not implementing a Render() method.
I have a table on SQL Server 2008, I saved on it videos and audios as binary.
I have to play these media (videos and audios) on my website.
Can I do that?
Is there a way that I could request the media as a link? so I can embed it...
Any help appreciated.
Sorry for my english.
They are probably other ways to do this but i'll just show mine below.
create an asp.net page (image.aspx for instance) that has no content on the page just the #page attributes... see below.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="image.aspx.cs" Inherits="image" %>
You code behind file might look like below:
public void Page_Load(object s, System.EventArgs e)
{
int id = Request.QueryString["image_id"]; //assuming u are getting it from query string
byte[] bt = GetMediaData(id); //image data from database as byte array
Response.Buffer = true;
Response.ContentType = "audio/mp3"; //set mimetype of appropriate media type
Response.BinaryWrite(abt);
Response.Flush();
}
on the page that needs to use the resource:
<asp:Image runat="server" id="Image1" ImageUrl="image.aspx?image_id=xx" />
i'm sure this can be done using http handlers though.
As title, basically I have a user control placed inside a page and then the page is placed inside the master page. I want to extract a block of javascript and put it back to the page head. When I try to wrap the code inside any block control (e.g. a div runat server) and access divID.InnerText then ASP.NET bust off with
Cannot get inner content of startup_script because the contents are not literal.
I dont want to extract JS inside cs file, thats awfully ugly approach (all sort of escapes and people wont even notice you have JS written unless they drill your CS file), what can I do?
You could store the javascript in a separate file, and then add it to the page using Page.RegisterClientScriptBlock()
Add the javascript you want to a .js file and add the .js file to your project.
Alter the properties of the .js file so that it is an Embedded Resource.
Then use code like this somewhere in your UserControl (maybe the Page_Load) to pull the code from the file and drop it into the page:
string javaScript = "";
// the javascript is in a separate file which is an 'embedded resource' of the dll
using (StreamReader reader =
new StreamReader((typeof(ThisClass).Assembly.GetManifestResourceStream(typeof(ThisClass), "NameOfJavaScriptFile.js"))))
{
javaScript = String.Format("<script language='javascript' type='text/javascript' >\r\n{0}\r\n</script>", reader.ReadToEnd());
}
Page.RegisterClientScriptBlock("MyScriptBlock", javaScript);
Note that RegisterClientScriptBlock() will put the script near the top of the page, but apparently not in the page header.
(edited bit about header after comment)
<%# Page Language="C#"%>
<script runat=server>
protected override void OnLoad(EventArgs e)
{
string scriptText = someID.InnerHtml;
//if you really want it in the header...
//Page.Header.Controls.Add( new LiteralControl(String.Format( "<scr" + "ipt language=\"javascript\">{0}</scri" + "pt>\\n", scriptText )));
//doesnt add to header and requires form runat=server
Page.ClientScript.RegisterStartupScript(this.GetType(), "SomeScript", scriptText, true);
base.OnLoad(e);
}
</script>
<head runat=server></head>
<form runat=server>
<div id="someID" runat=server>alert('hi');</div>
</form>
Okay, I've probably done this in a horrible, horrible way, but I wanted to add a script to the head element in a recent project from a user control. This script didn't require any data from my server, but I only wanted it on specific pages, so I put the script in a .js-file, and added it to the head like this:
HtmlGenericControl script = new HtmlGenericControl("script"); // Creates a new script-element
script.Attributes.Add("type","text/javascript");
script.Attributes.Add("src","src/to/js-file.js");
Page.Master.FindControl("head").Controls.Add(script); // Finds the element with ID "head" on the pages master page.
Not entirely sure if the code works as I think it does as the code I wrote for the project is on another machine, but you get the idea, right?
Edit:
After googling your error message for a bit, I think this might be a solution to your problem:
using System.IO;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
yourDivID.RenderControl(h);
String str = sw.GetStringBuilder().ToString();
If you combine the two examples, you should be able to get the result you want. I haven't tested this, but it works in my head.
Is there a way to bind a MemoryStream to asp:image control?
Best bet is to create an HttpHandler that would return the image. Then bind the ImageUrl property on the asp:Image to the url of the HttpHandler.
Here is some code.
First create the HttpHandler:
<%# WebHandler Language="C#" Class="ImageHandler" %>
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
public class ImageHandler : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
context.Response.Clear();
if (!String.IsNullOrEmpty(context.Request.QueryString["id"]))
{
int id = Int32.Parse(context.Request.QueryString["id"]);
// Now you have the id, do what you want with it, to get the right image
// More than likely, just pass it to the method, that builds the image
Image image = GetImage(id);
// Of course set this to whatever your format is of the image
context.Response.ContentType = "image/jpeg";
// Save the image to the OutputStream
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
else
{
context.Response.ContentType = "text/html";
context.Response.Write("<p>Need a valid id</p>");
}
}
public bool IsReusable
{
get
{
return false;
}
}
private Image GetImage(int id)
{
// Not sure how you are building your MemoryStream
// Once you have it, you just use the Image class to
// create the image from the stream.
MemoryStream stream = new MemoryStream();
return Image.FromStream(stream);
}
}
Next, just call it inside your aspx page where you are using the asp:Image.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
<asp:Image ID="myImage" ImageUrl="~/ImageHandler.ashx?id=1" runat="server" />
</div>
</form>
</body>
</html>
And that is it.
A handler can accept a url parameter like any other request. So instead of linking your <asp:image/> to image.ashx you'd set it to image.ashx?ImageID=[Your image ID here].
I am assuming you need to generate dynamic images from asp.net
You might be in luck
http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=16449
Hanselman blogged about it recently
http://www.hanselman.com/blog/ASPNETFuturesGeneratingDynamicImagesWithHttpHandlersGetsEasier.aspx
#Will and Ben Griswald: instead of "image.aspx" use "image.ashx".
It's more light-weight than a full ASP.Net Page, and it's specifically designed to handle content-types other than text/html.
While Databinding a MemoryStream to a Image is not possible, it could be possible to use a Label/GenericControl, some Code and the data URI scheme to embed Images in Pages, but there are severe issues with that approach:
Disadvantages
Embedded content must be extracted and decoded before changes may be made, then re-encoded and re-embedded afterwards.
Cookies are not supported.
Information that is embedded more than once is redownloaded as part of the containing file, and thus does not benefit from the browser's cache.
Browsers may limit URI lengths, creating an effective maximum data size. For example, URIs in previous versions of Opera had limits of 4kB, and 32kB for IE8 Beta 1[citation needed]
Data is included as a simple stream, and many processing environments (such as web browsers) may not support using containers (such as multipart/alternative or message/rfc822) to provide greater complexity such as metadata, data compression, or content negotiation.
Microsoft's Internet Explorer, through version 7 (some 70% of the market as of 2008 Q2), lacks support.
The better Approach is to use a separate "Image.aspx" Page which takes and outputs your MemoryStream, kinda like I did in my Photo Album software that i've created when I started learning ASP.net:
(Don't laugh, that was my first attempt at ASP.net :-)
Edit: Agreed on ASHX, the code above is just to show one sample implementation. When I come around to update the Photo Album, it will use ASHX for that.
You can use Telerik's BinaryImage control for ASP.net.
More info in here:
http://www.telerik.com/products/aspnet-ajax/binaryimage.aspx
Nope.
But you can create a special page to stream that image out. First, you set the URL of the image to the page that performs the streaming, including some url parameters that let you know where to get the image:
<img src="GetImage.aspx?filename=foo" ... />
in GetImage.aspx, you get the filename (or whatever) from the URL, load the image in your MemoryStream, and then write the content of that memory stream directly to the HttpResponse:
response.Expires = 0;
response.Buffer = false;
response.Clear();
response.ClearHeaders();
response.ClearContent();
response.ContentType = "image/jpeg";
response.BinaryWrite(stream);
response.Flush();
response.Close();
For me it was necessary to add "buffer="false" to the #Page. Otherwise I would keep getting the same picture all the time...