In traditional ASP, we always had the Application object to store application-wide variables in. This of course came at the price of memory allocations. In .NET we can now take advantage of Static Variables, which in most cases can be faster than accessing the Application object.
In .NET, most objects are actually classes, and Global.asax is no exception. To take advantage of this, we first have to give our Global.asax a Classname. We do this by adding the directive naming mine 'MyGlobals':
<%@ Application Classname="MyGlobals" %>
Then, we specify our Static Variable inside the script tags, using the 'Public' and 'Shared' keywords in the Global.asax:
Public Shared sGreeting as String = "Visit HarrisonLogic.com!"
Public Static String sGreeting = "Visit HarrisonLogic.com!"
Now that we have the variable 'sGreeting' set up, we can call it directly from our .aspx page using the Class name and the Variable name:
x = MyGlobals.sGreeting
But in case of at some sitiations you may need to acces certain data with respect to the applications like storing number of hit counts for the application.
1) Static variables cannot persist its value after server post backs.
2) But Application object will persist its value after server post backs
In this case you have to wite Inline script for this page. Simply create a string and save it as ASPX.
Note that in page directive Codebehind attribute should not be there and do write all the login in
I have a situation where I must add ImageButtons dynamically on a page. This I managed to do, but I need them to respond to the Click event and they won't.
Here's a snippet which represents what I'm doing:
publicpartialclass _Default : System.Web.UI.Page
ArrayList imagenes = new ArrayList();
protectedvoid Page_Load(object sender, EventArgs e)
ImageButton ib3 = new ImageButton();
ib3.ID = "ib3";
ib3.ImageUrl = "Puesta de sol.jpg";
ib3.Click += new ImageClickEventHandler(ImageButton_Click);
ib3.Width = new Unit(100, UnitType.Pixel);
ib3.Height = new Unit(100, UnitType.Pixel);
Session["Imagenes"] = imagenes;
imagenes = (ArrayList)Session["Imagenes"];
foreach (ImageButton ib in imagenes)
protectedvoid ImageButton_Click(object sender, ImageClickEventArgs e)
Page.ClientScript.RegisterStartupScript(GetType(), null, "alert('HEY');", true);
After clicking on the ImageButton, the page reloads, enters the Else clause, the ImageButton is restored, but it won't enter the Click handler.
Also, I tried setting the handler manually inside the Else clause after the control has been added to the form but it won't enter the handler either.
My guess is this is happening because asp.net is actually assigning the handler, but the call to the handler is simply 'lost' during the restoration of the controls since it appears asp.net expects to fire events for 'static' controls only.
I've checked that when a Click event is fired for a 'static' ImageButton, the execution path goes through the Page_Load first and ends into the handler. That's why i re-create the control in the Else clause in Page_Load.
It's not as important for me to get every delegate for each control restored(which i guess it actually is) and working, as it is getting all of them getting into one handler as expected, since I'll possibly assing all of them the same handler.
Thanks a lot in advance for any help you could provide,
Ok, strangely enough, when I set the handler now, after re-creating the controls, its working. I'm sorry, i thought i had tested this yesterday already.
I guess I can go on now.
Nonetheless, I'm still interested in a way of storing and retrieving the delegates just in case i stumble uppon a situation where I actually need to use different event handlers for different controls.
I am using Datagrid and in it Questions are being displayed from Database.So when user print these questions then Datagrid Header is displayed only on first page .I want datagrid header to be displayed on all print pages.How can this be done.
I'm trying to develop an application where, i have a flash file running in an ASPX page and that page contains an aspx save button. I need to capture a portion of the screen(i 've filled a rectangle with color and an image in flash-- this need to be captured) and save that as a jpg file in the server on an aspx button click. Code behind is C#.net.
I am adding reference for microsoft outlook. i have selecting microsoft outlook 11.0 object library.
but in my code it gives error in c#.net 2005. but its working in visual studio 2003. Please help me.
The type or namespace name 'Interop' does not exist in the namespace 'Microsoft.Office' (are you missing an assembly reference?)
using System.Reflection; // to use Missing.Value
using Outlook = Microsoft.Office.Interop.Outlook;
static void Main(string args)
// Create the Outlook application.
// in-line initialization
Outlook.Application oApp = new Outlook.Application();
// Get the MAPI namespace.
Outlook.NameSpace removed = oApp.GetNamespace("mapi");
// Log on by using the default profile or existing session (no dialog box).
oNS.Logon(Missing.Value, Missing.Value, false, true);
// Alternate logon method that uses a specific profile name.
// TODO: If you use this logon method, specify the correct profile name
// and comment the previous Logon line.
//Get the Inbox folder.
Outlook.MAPIFolder oInbox = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
//Get the Items collection in the Inbox folder.
Outlook.Items oItems = oInbox.Items;
// Get the first message.
// Because the Items folder may contain different item types,
// use explicit typecasting with the assignment.
Outlook.MailItem oMsg = (Outlook.MailItem)oItems.GetFirst();
//Output some common properties.
//Check for attachments.
int AttachCnt = oMsg.Attachments.Count;
Console.WriteLine("Attachments: " + AttachCnt.ToString());
//TO DO: If you use the Microsoft Outlook 10.0 Object Library, uncomment the following lines.
/*if (AttachCnt > 0)
for (int i = 1; i <= AttachCnt; i++)
Console.WriteLine(i.ToString() + "-" + oMsg.Attachments.Item(i).DisplayName);
//TO DO: If you use the Microsoft Outlook 11.0 Object Library, uncomment the following lines.
/*if (AttachCnt > 0)
for (int i = 1; i <= AttachCnt; i++)
Console.WriteLine(i.ToString() + "-" + oMsg.Attachments[i].DisplayName);
//Display the message.
my application is actually a web application where i manually upload CSV file to the database.but i want to automate the process,so i have thought of a second stage to this application i.e through a windows application i would download the CSV file from the NSEIndia.com site automatically and then upload the same to SQLServer on the server.i am facing a problem with upload i.e exporting the data to the database.it would be a great help to me if any one could provide me a solution either in VB.Net code or C# code.
Last Visit: 31-Dec-99 18:00 Last Update: 30-Sep-23 14:15