Controls of .aspx pages are not accessible in .aspx.cs page - asp.net

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

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.

(ASP.NET) Button event not firing the first 30 seconds

I'm having a weird issue where a button's click event is not being fired for a while (around 20-30 seconds) and then starts to work as normal. Refresh the page and I have to wait for it to work again.
Please note this: It works when run in debug on the localhost, the behavior described is only found on the server. I'm starting to suspect a server-side issue, but I don't have access to it and would need to pin-point what might be causing this to the people in charge of it (if it's indeed a server-side issue).
Any help to locate the issue is much appreciated! I stripped the page down to the most basics and this is what I have.
ASP:
<%# 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 (http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd)">
<html xmlns="http://www.w3.org/1999/xhtml (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" onclick="Button1_Click" Text="Button" />
</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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("http://www.google.se/ (http://www.google.se/)");
}
}
Config:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
</configuration>

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.

Slow UpdatePanel in ASP.NET application

I was trying to figure out why my UpdatePanel is so SLOW (after Button1's postback) in an ASP.NET application:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MyNamespace.WebForm1" %>
<%# Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
<!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">
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"/>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</ContentTemplate>
</asp:UpdatePanel>
</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;
namespace MyNamespace
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)//this is slow!!
{
Button btn = sender as Button;
btn.Text = DateTime.Now.ToString("hhmmssfff");
}
}
}
I was searching here in StackOverflow but I couldn't find a straight forward answer.
Anyone knows something that might speed this up?
I've seen a faster UpdatePanel button postback when I run my webapplication using http://Junior-PC. But when I use http://localhost is pretty slower.
The only difference between a regular web application and this one is in Web application properties-> Web tab-> Servers section where is marked Use Local IIS Web Server-> Project Url: http://localhost/ and Override application root URL.
If you're using Firefox, don't forget do disable IPv6 in (enter about:config in URL). Except that, code looks fine. What's the speed of regular postbacks? Tried other browsers?
Please try to use classic script manager:
asp:ScriptManager
instead of:
cc1:ToolkitScriptManager.
Morover please unregister AjaxControlToolkit because in this case it is unnecessary. Let us know it helps.

Setting Session Variable from UpdatePanel

I am using ASP.NET 2.0 AJAX Extensions 1.0 with the version v1.0.20229 of the AJAX Control Toolkit (which to my knowledge is the latest for .NET 2.0/Visual Studio 2005).
My web page (aspx) has a DropDownList control on an UpdatePanel. In the handler for the DropDownList's SelectedIndexChanged event I attempt to set a session variable.
The first time the event is fired, I get a Sys.WebForms.PageRequestManagerParserErrorException: "The message received from the server could not be parsed". If I continue, subsequent SelectedIndexChanged's are handled successfully.
I have stumbled upon a solution whereby if I initialise the session variable in my Page_Load (so the event handler is just setting the value of a session variable that already exists as opposed to creating a new one) the problem goes away.
I'm happy to do this, but I'm curious as to exactly what the underlying cause is. Can anyone explain?
(My suspicion is that setting the session variable receives a response from the server which is then returned to the 'caller', but it's not the sort of response it knows how to deal with causing the exception?)
EDIT: I reproduced the problem in a seperate little project:
Default.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SessionTest._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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:UpdatePanel id="upCategorySelector" runat="server">
<ContentTemplate>
Category:
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCategories_SelectedIndexChanged">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
Default.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;
namespace SessionTest
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// If I do this, the exception does not occur.
if (Session["key"] == null)
Session.Add("key", 0);
}
protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
{
// If Session["key"] has not been created, setting it from
// the async call causes the excaption
Session.Add("key", ((DropDownList)sender).SelectedValue);
}
}
}

Resources