aspx does not contain a definition for this.Bounds - asp.net

hello everyone i am writing a program which captures a part of the screen when a button is clicked. Below is my code
**default.aspx**
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="_9marchexp._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Welcome to ASP.NET!
</h2>
<p>
To learn more about ASP.NET visit www.asp.net.
</p>
<p>
You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
</p>
<p>
</p>
<p>
</p>
<p>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</p>
</asp:Content>
and
default.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.IO;
using System.Data;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace _9marchexp
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Rectangle formRect = this.Bounds; // saving information aout your window in formRect <br/>
// (height, width, x, y, etc
using (Bitmap bitm = new Bitmap(formRect.Width, formRect.Height)) // creating new bitmap of the same <br/>
// height and width as our own window
{
using (Graphics graphic = Graphics.FromImage(bitm))
{
graphic.CopyFromScreen(new Point(formRect.Left, formRect.Top), Point.Empty, formRect.Size);
}
bitm.Save("C:\\Users\\xxxx\\Desktop\\myscreenshot.jpg", ImageFormat.Jpeg); // saving the bitmap bile to destination dir. <br/>
// with specified name
}
}
}
}
however the thing is when i execute the code an error message is displayed
***'_9marchexp._Default' does not contain a definition for 'Bounds' and no extension method 'Bounds' accepting a first argument of type '_9marchexp._Default' could be found (are you missing a using directive or an assembly reference?)***
How to correct that, can someone help me??
also if i replace this.Bounds with Screen.PrimaryScreen.Bounds the code runs perfectly but the screenshot of the whole screen is taken which i don't want.

It is impossible to capture the screen of the browser, or of the user, or of the server, or of the desktop or what ever, using the code behind.
A solution that read a page and print it to an image can be found here
Convert webpage to image from ASP.NET

Related

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";
}

sending sms messages from twilio trial number

i have verified my own mobile number with twilio but the number is from a service provider in malaysia. i did the codes to send the sms but i am unable to receive it on my number. do i need a us or canada number or do i have to purchase a number from twilio in order to test the codes. below are the codes:
WebForm1.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication4.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h4>To Number: </h4>
<asp:TextBox ID="ToNumber" TextMode="password" runat="server"></asp:TextBox>
<h4>SMS Message</h4>
<asp:TextBox ID="Message" runat="server"></asp:TextBox>
<br/>
<br/>
<br/>
<asp:Button ID="SendMessage" OnClick="SendMessage_OnClick" runat="server" Text="Send Message" />
</div>
</form>
</body>
</html>
WebForm1.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.Configuration;
using Twilio;
namespace WebApplication4
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SendMessage_OnClick(object sender, EventArgs e)
{
string ACCOUNT_SID = ConfigurationManager.AppSettings["ACCOUNT_SID"];
string AUTH_TOKEN = ConfigurationManager.AppSettings["AUTH_TOKEN"];
TwilioRestClient client= new TwilioRestClient(ACCOUNT_SID, ACCOUNT_SID);
client.SendSmsMessage("(862) 373-1913", ToNumber.Text, Message.Text);
}
}
}
i have checked with visual studio 2012 that the codes are working but still can't seem to receive the sms. what should i do?
some numbers are not supported by twilio like start with 94,87.
By the it's work with vodafone company numbers.
Please try it.
use:
var result = client.SendSmsMessage("(862) 373-1913", ToNumber.Text, Message.Text);
and take a look at result. If most of the fields are null, look at result.RestException. A common issue is with authentication.

AjaxFileUpload doesn't fire OnUploadComplete Event

i am trying to get that AjaxFileUpload-Control(used in ContentPage) working. But it does not fire OnUploadComplete Event at server side
I am using version 4.1.60919.0 of the ControlToolkit. I have tried everything i found on the internet.
Here just a few steps:
Added enctype="multipart/form-data" method="post" to the form-element in my MasterPage
Nested the AjaxFileUpload into an UpdatePanel with UpdateMode=Always
Tried events UploadedComplete and OnUploadComplete, but stayed at the second one
Added a try-catch-block in the EventHandler to catch unknown exceptions and print the ExceptionMessage to a label on the site --> nothing happened
Tried it with(out) a ThrobberImage...
Many other tipps that did not work...
So, i hope we will find a solution together in this community. Heres my markup:
<%# Page Title="New Download" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="NewDownload.aspx.cs" Inherits="Internals_NewDownload" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<ajax:ToolkitScriptManager ID="ToolkitscriptManager" runat="server"> </ajax:ToolkitScriptManager>
<h1>Create a new Download</h1>
<ajax:AjaxFileUpload ID="FileUpload" runat="server" ThrobberID="ThrobberLabel" OnUploadComplete="FileUpload_UploadComplete" />
<asp:Label ID="ThrobberLabel" runat="server" Style="display: none;"><img alt="UploadingPicture" title="Please wait while uploading..." src='<%= Constants.DomainString + "/Data/Images/loading-small.gif" %>' /></asp:Label>
<asp:Label ID="DownloadLabel" runat="server"></asp:Label>
</asp:Content>
And this is my CodeBehind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Internals_NewDownload : System.Web.UI.Page
{
private string m_LanguageCode;
protected void Page_Load(object sender, EventArgs e)
{
if (RouteData.Values.ContainsKey("LanguageCode"))
m_LanguageCode = RouteData.Values["LanguageCode"].ToString();
//if (IsPostBack)
// return;
if (!User.IsInRole("Administrator") && !User.IsInRole("Kunde") && !User.IsInRole("Mitarbeiter"))
Response.Redirect(Constants.DomainString + "/PermissionDenied.aspx");
Session[Constants.NonGlobalizedString] = true;
Session[Constants.MenuInfoSession] = new ClsMenuInfo("NewDownload");
}
protected void FileUpload_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
try
{
string filePath = "~/upload/" + e.FileName;
DownloadLabel.Text = filePath;
}
catch (Exception ex)
{
DownloadLabel.Text = ex.Message;
}
}
}
Please, if you have ANY idea, do not hesitate to let me know it. I am very confused as i think that i just did in that howtos i found on the internet...
Thanks in advance!
Take into account that AjaxFileUpload control use contextkey QueryString parameter to detect own postback. I believe the reason of you issue is that this parameter lost in result of rewriting url.
I'm not expert in applying routing but in my opinion you need to register contextkey parameter in routing tables and tweak AjaxControlToolkit sources to use RouteData instead of Request.QueryString for retrieving it value.
Check this link for more info: AjaxControlToolkit Source Code

ASP Hyperlink not found in Site.master.cs?

I have this code in my Site.master :
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster" %>
<!-- ... -->
<AnonymousTemplate>
[ <asp:HyperLink ID="LoginHyperLink" runat="server" EnableViewState="false">Log In</asp:HyperLink> |
<asp:HyperLink ID="RegisterHyperLink" runat="server" EnableViewState="false">Register</asp:HyperLink> ]
</AnonymousTemplate>
I have this code in my Site.master.cs :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
// ...
protected void Page_Load(object sender, EventArgs e)
{
// this is just placeholder for now.
RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
}
I'm getting a Compile-Time error in Visual Studio 2010 at the RegisterHyperLink.NavigateUrl saying :
"The name 'RegisterHyperLink' does not exist in the current context."
Not really sure what's up. I've seen this work in non-Master pages, so does this just not work in Masters?
I'd think it would...
It´s a bug, add a new HyperLink in Desing/Source save and try again. Delete the new hyperlink to finish
Ensure that your code-behind is inheriting from System.Web.UI.MasterPage.
Ensure the aspx has the appropriate directive added and that it is spelt right with correct case:
<%# Master Language="C#" CodeFile="Site.master.cs" Inherits="MasterPage" %>
It is because of this AnonymousTemplate. It probably creates new naming container thus is not directly accessible from Page_Load.

asp.net c# GDI application

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.

Resources