asp.net c# GDI application - asp.net

im just going through a tutorial on image creation using GDI on an asp.net page using c#
my code is below
html page code
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Image1.aspx.cs" Inherits="GDI_1.Image1" %>
<!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 runat="server" ID="img" ImageUrl="Image1.aspx" />
</div>
</form>
</body>
</html>
asp.net page behind code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace GDI_1
{
public partial class Image1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bitmap image = new Bitmap(300, 50);
Graphics g = Graphics.FromImage(image);
g.FillRectangle(Brushes.Red, 1, 1, 298, 48);
Font font = new Font("Impact", 20, FontStyle.Regular);
g.DrawString("test text", font, Brushes.Yellow, 10, 5);
image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
g.Dispose();
image.Dispose();
}
}
}
my problem is when i compile it and view the page it is displaying loads of this
GIF89a,2�3f���++3+f+�+�+�UU3UfU�U�U���3�f��������3�f��������3�fՙ������3�f������3333f3�3�3�3+3+33+f3+�3+�3+�3U3U33Uf3U�3U�3U�3�3�33�f3��3��3��3�3�33�f3��3��3��3�3�33�f3ՙ3��3��3�3�33�f3��3��3��ff3fff�f�f�f+f+3f+ff+�f+�f+�fUfU3fUffU�fU�fU�f�f�3f�ff��f��f��f�f�3f�ff��f��f��f�f�3f�ffՙf��f��f�f�3f�ff��f��f����3�f���̙��+�+3�+f�+��+̙
and all before the html tag
thanks

If you are just trying to run this directly within a page then that is what you will get.
To display correctly you need to set it to the source of an image i.e
<asp:Image runat="server" ID="img" ImageUrl="ImageBuilder.aspx" />
Where ImageBuilder.aspx is a page which contains your code.
Edit
What you need to do is have 2 pages.
Image1.aspx contains your image control as you currently have - change the source of the image to Image2.aspx.
Create a new page called Image2.aspx and remove everything from the aspx except the <%# Page line. Now remove the code from Image1 Page_Load and paste it in to Image2 Page_Load.
Rebuild and try again.

Related

How to solve the error Using the AjaxCallHelper write methods outside of an AjaxCall is not allowed

I've searched this error in all google results, but I can't find a solution.
I have this WebForm:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="LoTest.aspx.cs" Inherits="-.-.-.LoTest" %>
<%# Register TagPrefix="ajax" Namespace="MagicAjax.UI.Controls" Assembly="MagicAjax" %>
<!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>
<ajax:AjaxPanel ID="ajaxLG" runat="server" AjaxCallConnection="Synchronous">
<asp:Button ID="btnTest" runat="server" CssClass="button" ToolTip="gin." Text="LogIn" OnClick="btnTest_Click"></asp:Button>
</ajax:AjaxPanel>
</div>
</form>
</body>
</html>
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MagicAjax.UI;
using MagicAjax;
namespace -.-.-
{
public partial class LoTest : AjaxPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnTest_Click(object sender, EventArgs e)
{
AjaxCallHelper.WriteAlert("Test msg");
}
}
}
The web.config:
<httpModules>
<add name="MagicAjaxModule" type="MagicAjax.MagicAjaxModule, MagicAjax"/>
</httpModules>
In the Line “AjaxCallHelper.WriteAlert("Test msg");” throws the error:
Using the AjaxCallHelper write methods outside of an AjaxCall is not allowed.
In IE, just add the site to the compatibility view and that´s all.
Seems like if AjaxMagix.dll works with older elements of the explorer.
In Google Chrome doesn´t work.

Controls of .aspx pages are not accessible in .aspx.cs page

I am converting my existing web site project to web application for that I have created a new web application and the copy the entire source code of web site project in web application project. But now the problem is when I am compiling the project .aspx page controls are not accessible in .aspx.cs page. I am doing this in VS 2012 with target framework 3.5.
Here is the code snippet
HTML Source
<%# Page Language="C#" AutoEventWireup="true" CodeFile="View_Deal_Rights.aspx.cs" Inherits="Modules_View_Deal_Rights"
EnableEventValidation="false" ValidateRequest="false" %>
<!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>
<%=ConfigurationManager.AppSettings["SystemName"]%></title>
</head>
<body leftmargin="0" topmargin="10" marginwidth="0" marginheight="0" rightmargin="0"
bottommargin="0">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" AsyncPostBackTimeout="0">
</asp:ScriptManager>
<asp:Label ID="lblok" runat="server"></asp:Label>
<asp:Button ID="btnSave" runat="server" CssClass="button" Text="Save Deal" ValidationGroup="LastSave"/>
</form>
</body>
</html>
Code Behind Source
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UTOFrameWork.FrameworkClasses;
using System.Collections;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class Modules_View_Deal_Rights : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
btnSave. // Here Button save is not accessable
lblok. // Here lblok is not accessable
}
}
}
You need to change CodeFile to CodeBehind as you converted your application from web site to web application.
CodeBehind="View_Deal_Rights.aspx.cs"
Once you change then build and you will be able to access.
For more details see following question.
CodeFile vs CodeBehind

How to avoid build errors with aspx include files?

I have an aspx file that will contain a lot of sections and I would prefer to have each section in its own include file. I'm okay to put all of the code behind into the main file. I'm using: <!-- #include file ="section1.aspx" --> in the body. When I build (I'm still only including the first section with code), I get "The name 'lbl_phone' does not exist in the current context" because the code behind has a routine that references this screen field which IS in the include file. What is a better way to go about this?
Edit (as it is now, hope I cut it down properly here, builds good now but error in browser):
file ola.aspx
<%# Page Language="C#" AutoEventWireup="true" Inherits="_ola" Codebehind="ola.aspx.cs" %>
<%# Register TagPrefix="section" TagName="account" Src="account.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><body><form id="ola_form1" runat="server">
<section:account id="section_account" runat="server" />
</form></body></html>
file: ola.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
public partial class _ola : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lbl_phone.Text = "000-000-1234";
}
}
file: account.ascx
<%# Control Language="C#" ClassName="_ola" CodeBehind="account.ascx.cs" %>
Call us at <asp:Label ID="lbl_phone" runat="server" Text="lbl_phone"></asp:Label>
At runtime in the browser, I get: Object reference not set to an instance of an object. (for the line with lbl_phone.Text = ...)
Don't use server side includes. They don't work well in the ASP.NET architecture.
Instead, embed reusable stuff into .ASCX user controls. MSDN How To article.
ASCX File MyControl.ascx
<% # Control Language="C#" ClassName="Spinner" %>
<table>
<tr><th>Column 1</th><th>Column 2</th></tr>
<tr><td>Some content</td><td>Some more content</td></tr>
</table>
Then this can be embedded on as many ASPX pages as you like.
<%# Register TagPrefix="uc" TagName="MyControlName" Src="~\Controls\MyControl.ascx" %>
<uc:MyControlName ID="MyControl1" runat="server" />
Edit- You should make these ASCX files as portable as possible in your application so that they're self contained units. There shouldn't be much of a need for your ASPX code behind to reference a control contained in the ASCX file. You can do code behinds for user controls (MyControl.ascx.cs or MyControl.ascx.vb) if necessary, or you can use a script block to embed the code directly in the ASCX page.
.NET Framework will not follow server side includes and so it won't compile properly if you try to reference a control that is being included via SSI. But you could potentially reference a control in an ASCX file. For example...
MyAdvancedControl.ascx
<% # Control Language="C#" ClassName="Spinner" %>
<asp:Label runat="server" id="lbl_phone" />
<script runat="server">
public Label Lbl_phone {get {return lbl_phone;}}
</script>
MyPage.aspx
<%# Register TagPrefix="uc" TagName="MyAdvancedControlName" Src="~\Controls\MyAdvancedControl.ascx" %>
<uc:MyAdvancedControlName ID="MyControl2" runat="server" />
MyPage.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
MyControl2.Lbl_phone.Text="Galaxy S4";
}
Controls defined as part of a Page or Master Page or User Control are private by default. To get around that, add a public property that exposes it. Put this property in account.ascx.cs
public Label lbl_phone_public_version{
get {
return lbl_phone;
}
}
When you try to reference lbl_phone from code on ola.aspxcs, you have to do it through the parent control, like this:
protected void Page_Load(object sender, EventArgs e)
{
section_account.lbl_phone_public_version.Text = "000-000-1234";
}

DropDownList1 does not exists in the current context error

I have an aspx application. I have defined a dropdownlist in the .aspx file and try to load some data into it from database in .aspx.cs file. But I am getting the error in .aspx.cs as "DropDownList1" does not exists in the current context.
Below is the code:
Wiki.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Wiki.aspx.cs" Inherits="FinalProj2._Wiki" %>
<!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>
<style type="text/css">
body { margin: 4%; }
#space1 { height:1em; }
#space2 { height:1em; }
</style>
</head>
<body>
<h1>WIKI</h1>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" Width="100%"></asp:DropDownList>
</div>
<div id="space1"></div>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack = "true" Height = "150px" Width = "100%"></asp:ListBox>
<div id="space2"></div>
<asp:TextBox ID="TextBox2" runat="server" Height="250px" Width="100%" TextMode="MultiLine"></asp:TextBox>
</form>
</body>
</html>
Wiki.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FinalProj2.Controllers;
namespace FinalProj2
{
public partial class _Wiki : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FinalProj2.Models.DataClasses1DataContext db = new FinalProj2.Models.DataClasses1DataContext();
Response.Write("<br/>Page.User.Identity.Name: " + Page.User.Identity.Name);
Response.Write("<br/>Page.User.Identity.IsAuthenticated: " + Page.User.Identity.IsAuthenticated);
Response.Write("<br/>Page.User.Identity.AuthenticationType: " + Page.User.Identity.AuthenticationType);
var query = from meet in db.Meets
select meet.Summary;
// where meet.Meeting_ID = (from meet_emp in db.Meet_Emps
//where meet_emp.Employee_Name == Page.User.Identity.Name
//select meet_emp.Meeting_ID)
DropDownList1.DataSource = query;
DropDownList1.DataBind();
}//end
}
}
What could be the possible reason. According to me I feel everything is fine as had used the same way for a list box into another application.
Make sure you have no files that accidentally try to inherit or define the same (partial) Mlass as other files.
Thus no other code behind file should have same signature in FinalProj2 Namespace.
public partial class _Wiki : System.Web.UI.Page

Why do I get the result zero when I try to get the width of a DropDownList control in asp.net?

After I click button1, it display 0, why? How can get correct width of a DropDownList control? Thanks!
<%# 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>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Text = DropDownList1.Width.Value.ToString();
}
}
It's because the value Width is not set. In html, when the width value is not set, you let the browser choose the size.
Moreover, the asp.net engine only host properties to be able to render the correct Html. If you look at the code of the textbox, you will see that if you put a witdh, it will produces a style="width:yourvalue". ASP.net runs on the server side, so it do not have any idea of the actual size of the control.
If you actually require the client size of the dropdownlist, either specify it explicitly, or use some custom javascript to calculate the actual size of the control, then to pass the calculted size to an hidden field. In the code behind you be able to get the value from the Page.Request.Form collection.
Please clarify your need if you need further explanation.

Resources