International Characters in ASP.Net

In HTML files, for any international characters, it was necessary to convert each separate character into an HTML entity.

Now, with the advent of ASP and ASP.Net, we have Server.HTMLEncode at our disposal. So now, we can just surround our characters with Server.HTMLEncode to take care of the problem. Let’s say you have a label called ‘Label1′ on your page – - :

label1.text=Server.HTMLEncode ("ö, ü, ä, ß...")

BTW – ASP Express has a feature which, for HTML files, will convert all international characters on the page to HTML entities and back again, if necessary.)

Changing the SiteName in ASP.Net Forums Installation

There are three places you need to Enter the site name, so that your name (not ASP.Net Forums) shows up on the page:
1. Web.Config in main directory
2. This changes the name on the main place of the page:
[install directory/skins/default/skins/Skin-Navigation.ascx

3. This one changes it on the Search box in the upper right corner:
[install directory/skins/default/skins/Skin-SearchRedirect.ascx

Make Any Button Invisible While Printing

So, let’s say you have a button on your page to print the page (maybe a Javascript routine), but you don’t want the actual image of the button, itself, to print out. Here’s a simple Javascript to include on your page that will do the trick:

<script language="Javascript" event="onbeforeprint" for="window"> document.forms[0].btnPrint.style.visibility = 'hidden'; document.forms[0].btnClose.style.visibility = 'hidden'; </script>

This makes two buttons, called btnPrint and btnClose, invisible – repeat as necessary. No Problem!
Then – to make them visible again – after printing:

<script language="Javascript" event="onafterprint" for="window"> document.forms[0].btnPrint.style.visibility = 'visible'; document.forms[0].btnClose.style.visibility = 'visible'; </script>

MaxLength and Multi-Line ASP.Net TextBox Controls

Unfortunately, the MaxLength property has no bearing on the ASP.Net TextBox Server Control, when it’s in Multiline mode. It only works in Single Line mode.

To get around this, you must use Javascript. Here’s a JS function (found on the ASP.Net forums – posted by ‘chunshahab’), along with a Textbox, showing how to use it, that takes care of this problem:

<script language="javascript"> <!-- Begin function textCounter(field, maxlimit) { if (field.value.length > maxlimit) // if too long...trim it! { alert ("You have reached 500-character limit."); field.value = field.value.substring(0, maxlimit); } else // otherwise, update 'characters left' counter { //countfield.value = maxlimit - field.value.length; } } // End --> </script> You can use it this way: <asp:TextBox ID="txtComments" Rows="15" runat="server" TextMode="MultiLine" Columns="75" ToolTip="Insert your Text Here" onKeyDown="textCounter(this.form.txtComments,500);" onKeyUp="textCounter(this.form.txtComments,500);" />

Filling Textboxes with Data from DataSet

OK – you’ve got a form with ASP.Net TextBox server controls AND you’ve got a Filled DataSet – - then, you probably want to know:br>
“How can I populate those textboxes with the data from the DataSet?”

This is quite easy – - just use the following example :

TxtOrdNum.Text = Ds.Tables(0).Rows(0)("ordnum")

Another, more complicated way, would be to assign each field to a variable (sOrdNum = Ds.Tables(0).Rows(0)(“ordnum”)). Inside the TextBox, you can then add add the data, using the Text property of the TextBox:

<%# sOrdNum %>

BUT – be sure, after assigning the data to the variables to do one last thing:

Page.DataBind

MachineToApplication error

if you are getting the following error:

“It is an error to use a section registered as allowDefinition=’MachineToApplication’ beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS.”

Try going into IIS and create a virtual directory for the directories where this error is being generated. Also, make sure that you have created an IIS application for that directory also. Either one, or the other, or both, should do the trick.

To create an IIS application, open up IIS and:

  1. Locate the web directory. Right click on the folder and choose Properties.
  2. Enter an application name, and click the ‘Create’ button to create an application.
  3. Set execute permissions for Scripts only
  4. Click the ‘Apply’ button
  5. Click the ‘OK’ button to leave IIS

Programming Outlook with C#.net

1) add a reference in ur project to the MSOUTL9.OLB or create a typelibrary This will create an interop dll for Outlook
2) Start an Outlook Session by using the ApplicationClass instance and using its logon() function call.
using Outlook;

Outlook.Application outlookApp = new Outlook.ApplicationClass(); Outlook.NameSpace outlookSes = outlookApp.GetNamespace("MAPI"); outlookSes.Logon ("exchtest","net",false,true);

Here the ApplicationClass will create an instance of the outlook and the GetNamespace() function call will create a new outlook profile (session). The logon() function takes parameters in this order :-
profile, password, showdialog, newsession.

Using the ItemDataBound Event

An ItemDataBound event is raised each time an item (row) is data bound to a DataGrid. Once this event is raised,an argument of type DataGridItemEventArgs is passed to the event handling method and the data relevant to this event is available.This data is is no longer available once your application exits the event handling method. The eventhandler for ItemDataBound isOnItemDataBound.We can specify the method that should handle the event which takes the event source object(in our case the datagrid) and the DataGridItemEventArgs as arguments.

This can be explained easily with the help of an example. Consider the case where you want to calculate the total of a column in a datagrid.

First, let us create a data table and add rows to it with the following C# code:

protected DataTable dt = new DataTable("itemprice");
dt.Columns.Add("item",typeof (String));
dt.Columns.Add("price",typeof(float));

DataRow dr; dr = dt.NewRow();

dr["item"]="pen";
dr["price"]=3.5;
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["item"]="camera";
dr["price"]=10.8;
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["item"]="coffee maker";
dr["price"]=40.2;
dt.Rows.Add(dr);

dg.DataSource=dt.DefaultView;
dg.DataBind();

After binding to the datgrid, the display looks like this:

<form id=”Form1″ name=”Form1″ action=”itemdatabound.aspx” method=”post”>

item price
pen 3.5
camera 10.8
coffee maker 40.2

</form>

Since we are using ItemDataBound event to to total up the price column in the data grid, we have tospecify on the web form (.aspx) page, the method that would handle the ItemDataBound event (Event wiring).This is acheived by mentioning within the datagrid control tag that on the occurence of the event

(OnItemDataBound),itemDataBound method should be called.

<asp:datagrid id=”dg” runat= “server”  OnItemDataBound=”itemDataBound” ShowFooter=”True”>

On the code behind file (.aspx.cs), we define theitemDataBoundmethod for handling theItemDataBound Event.

floattotal = 0; protected void itemDataBound(object sender,DataGridItemEventArgs e)        { if  (e.Item.ItemType!=ListItemType.Header && e.Item.ItemType!=ListItemType.Footer)                 {
  total += float.Parse(e.Item.Cells[1].Text);
e.Item.ForeColor = System.Drawing.Color.Blue;
}
else if (e.Item.ItemType == ListItemType.Footer)
{
e.Item.Cells[0].Text = "Total";
e.Item.Cells[1].Text = total.ToString();
}
}

The new datagrid looks like this when rendered on the page:

item price
pen 3.5
camera 10.8
coffee maker 40.2
Total 54.5

How does this code work? The itemDataBound method is called  as each row(item) is bound to the datagrid.
Within the method, we check if the current row is a header or a footer row.To do this we use ListItemType enumeration.
For now, all we need to know is that ListItemType enumeration contains different types of items(header, footer,item, alternating item etc. ) that can be included in a list control (in our case, a datagrid). Therefore, if we want to check if the it  bound  a datagrid footer,a simple if statement like this would do the trick:

if (e.Item.ItemType == ListItemType.Footer) { // your code }

In our method,  we add the value in the price column to the total if the datagrid item is not a header or footer. Finally, when thedatagrid footer is encountered, we display our total on the footer row of the datagrid.This is possible because ItemDataBound eventis the last oppurtunity to access the data item before it is displayed in the browser.

If you have any questionsor comments, please feel free to contact me at ranjiranji@yahoo.com

Create a Printer Friendly page (no Headers or Footers)

So, you’ve got this great dynamic page, with user controls for your header and your footer, but you need to have the ability for the end-user to have a print-out without the header and the footer.

One way to accomplish this would be to have your content section, completely in a User Control also. That way, you could just create a blank page, and put the user control there. From the original page, just link to this new blank page. All you’ll see is the content.

Then, you can use the Javascript Print Function for printing.
Inlude an ASP button Server Control in your new Print-Friendly page, called ‘btnPrint’. Then, in the Page_Load event, type in:

btnPrint.Attributes("onClick") ="javascript:window.print();"

Voila! – that’s all there is to it. You can get fancier, if you’d like, actually adding an ‘OnServerClick’ sub, so that you could use ASP.Net to return a ‘Successful Print’ message…from this point on, it’s up to you on how you’d like to customize this!