Wednesday, March 16, 2005

Playing Flash Files in ASP.NET

Copy the HTML code below and paste it into your HTML

codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"
WIDTH="550" HEIGHT="400" id="myMovieName">

NAME="myMovieName" ALIGN="" TYPE="application/x-shockwave-flash"
PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">



1. Change the HEIGHT and WIDTH parameters to match the height and width of the movie dimensions or use percentage values, if desired.
2. Change "moviename.swf" where it appears in the OBJECT and EMBED tags to the name of movie to be played.

Sending mail

You can use the System.Web.Mail.MailMessage and the System.Web.Mail.SmtpMail class to send e-mail in your ASPX pages. Below is a simple example of using this class to send mail

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Mail" %>


Mail Test






TIP#1 : ASP.NET

Generally when using Forms authentication in ASP.NET you are able to secure your .aspx pages but you have not secured Images or documents like Ms-Word, Excel etc. A quick tip to secure these items is that you can configure that these extensions should be served by asp.net and not IIS.

1. In your IIS , Right click on the Virtual Directory and select Properties.
2. On Configuration Ta, a dialog box appears with the list of file extensions.
3. Click Add and Enter the extension type in the textbox such as .doc .ppt etc.
4. Point your path to aspnet_isapi.dll found under %windir%\Microsoft.NET\Framework\v1.1.4322 5. In "Limit to" radio button and put the same properties as like for aspx files i.e. GET, POST etc.

Its Done!

Enjoy!!

Pagination in ASP.Net DataGrid

Quick steps to work out pagination in your datagrid.. Go to your data grid
properties... Set AllowPaging property to true... Set the PageSize to your
desired page size... Now go to events section in the properties... You have
to override PageIndexChanged Event... In the event handler... Write code
similar to the one below:

private void dgdImages_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
//Change the current page index to the new one
dgdImages.CurrentPageIndex = e.NewPageIndex;
//Update your data if required
dgdImages.DataSource = CreateDataSource();
//bind the data again
dgdImages.DataBind();
}

That's it... Your datagrid does not need to make your page long scrollable
now... Do note though that if you change the data in the background and the
new data does not contain as many pages as your current page index now you
might land up getting exceptions... So make sure that you do the necessary
work there...

Mitesh Mehta

Dr. Dotnetsky's Cool .NET Tips & Tricks

Nice tips & tricks on .Net...a regular must read which doesn't
restrict only on langauge but speaks about even on DB's, Operating
systems, SDK's, various Service packs etc etc

More enjoyment here...
http://www.eggheadcafe.com/articles/20040821.asp

ASP.NET Vulnerability

This is today's HOT NEWS for ASP.NET developers!

There is a report on ASP.NET vulnerability and Microsoft is currently investigating on this issue. The issue is that ASP.NET is failing to perform proper canonicalization of some URLs.

This issue affects Web content owners who are running any version of ASP.NET on Microsoft Windows 2000, Windows 2000 Server, Windows XP Professional, and Windows Server 2003.

To know more about this issue and recommended guidance on best practices visit

http://www.microsoft.com/security/incident/aspnet.mspx

Saturday, October 23, 2004

Default Button for a TextBox (Data entry screen) in ASP.NET

Sometimes I get annoyed when hitting the enter key in a TextBox (say login screen) resulting in undesired effects like the wrong Button being “clicked“. After so many code tricks, I found the below method that allows you to specify a default Button to submit when the user hits the enter key in a TextBox.

When you press a key on your keyboard, the js OnKeyPress event is fired. This calls a function to which we pass the id of the button associated with the TextBox. The function gets a reference to the button and simuilates a mouse-click on the Button. We perform a browser detection because IE and Netscape have different event models. The function finally returns false so that the keypress event is cancelled (otherwise a second form submit will be raised). I tried this code with newer versions of IE 6.0/Netscape 7 it worked great!.

//client side js
function clickButton(e, buttonid){
var bt = document.getElementById(buttonid);
if (typeof bt == 'object'){
if(navigator.appName.indexOf("Netscape")>(-1)){
if (e.keyCode == 13){
bt.click();
return false;
}
}
if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1)){
if (event.keyCode == 13){
bt.click();
return false;
}
}
}
}

//code behind
TextBox1.Attributes.Add("onkeypress", "return clickButton(event,'" + Button1.ClientID + "')");

The code behind generates the following code:

< input name="TextBox1" type="text" id="TextBox1" onkeypress="return clickButton(event,'Button1')" / >

This causes web control Button1 to be clicked when the enter key is hit inside TextBox1.



With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/

Adding Cross-Site Scripting Protection to ASP.NET 1.0

ASP.NET 1.1 added the ValidateRequest attribute to protect your site from cross-site scripting. What do you do, however, if your Web site is still running ASP.NET 1.0? This article shows how you can add similar functionality to your ASP.NET 1.0 Web sites.

Check out the article @
http://msdn.microsoft.com/library/en-us/dnaspp/html/ScriptingProtection.asp

With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/

Multilingual Support in .NET

This is a Web Solution designed to teach all newcomers into the .Net field on how to incorporate Multi-language support into your Web Applications. You have to download the accompaning ZIP file.
This tutorial will teach you

- How to create the Resource files for various languages.
- How to access the Resource file data from the web pages.
- How to store Unicode (multilingual) data into the database.
- How to access multilingual data from the database.

You can find the tutorial article at http://www.developersdex.com/gurus/articles/500.asp?Page=1


With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/

Better Ways to Manage Your Cache in ASP.NET

Caching is one of the features in ASP.NET 1.x that most developers are familiar with. And it has helped to fuel a yearning for cache dependency in SQL Server. The ability to refresh the cache if a row in a table has been modified is very useful to a lot of developers. And hence in ASP.NET 2.0, Microsoft built SQL Server cache dependency right into the product.

Read more here http://www.devx.com/asp/Article/21751/1954?pf=true


With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/

Tuesday, September 28, 2004

Web Forms DataGrid and DataSet Programming

This is a working C# .NET program that demonstrates how to integrate most of the features of the DataGrid and DataSet in a single project including select, insert, update, delete, confirm delete, sort, filter and page.

Get more here http://www.developerfusion.com/scripts/print.aspx?id=3801


With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/

How server form post-back works?

One of the most important features of the ASP.NET environment is the ability to declare controls that run on the server, and post back to the same page. Remember the days of classic ASP? We would create a form which would accept the user's input, and then we would most probably have to create another page that would accept all those inputs, either through HTTP GET or POST, and perform some kind of validation, display and action. Sometimes, even a third page was necessary to perform our actions. This wasted a lot of time and complicated things when you had to make a change. But of course, this is not necessary any more with ASP.NET. There is no need to create second pages that accept the inputs of the first, process them and so on. Form fields and other controls can be declared to run on the server, and the server simply posts the page back to itself and performs all the validation, display and actions. Our life as web developers has become a million times better. But how exactly is this done?

When a control is declared to run on the server, a VIEWSTATE is created which remembers the ID of that control, and the method to call when an action is performed. Submit event calls the JavaScript function __doPostBack (that's 2 underscore symbols in front of it). You do not write this function, instead it is generated by the ASP.NET engine and automatically included in our page. It submits the form to the same page, and accepts 2 arguments:

eventTarget: the control doing the submission
eventArgument: any additional information for the event

At least one control needs to be set to Visible, for the server to generate the __doPostBack function in our page. Even if we have numerous web controls declared to run on the server, but they are all set to Visible=false, then the javascript function will not be included and we will not be able to perform any actions.



With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/

How is the viewstate encoded? Is it encrypted?

No, it isn't encrypted. It's Base64 encoded. By default, there's also a HMACSHA1 digest appended that prevents it from tampering. It's protected with an HMACSHA1 digest computed with the viewstate and a randomly generated 512 server secret so it is tamper resistant.




With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/

Do we HAVE TO name the ASP.NET configuration file as web.config?

Yes, if you want ASP.NET to read it. You can store arbitrary stuff for your app elsewhere but you'll have to consume it explicitly.



With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/

How do we handle session across ASP and ASP.NET pages in the same Site?

Unfortunately, they can't share. ASP supports in-proc state only and they run in different processes.If you need to support this, you might want to try keep state in a database and using a cookie to track it.


With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/

Asynchronous Programming in Web Services

Callback functions are the way to implement Asynchronous programming in Web
services... We will get into details of this topic sometime but now just an
overview...
A client calls the Begin method on the server object and passes a reference
to the callback method... The asynchronous method on the server object
finishes execution and gives a call back to the method on the client
object... This method in client object now causes the call to End method on
the server object.... The End Method then returns the result to the
client...
Well why would you do such a thing??... The simple reason can be when your
server side execution is really going to take long and you do not want to
wait for all that processing to happen...



With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/

Tuesday, September 21, 2004

Top questions about Datagrid

Top questions about Datagrid

The DataGrid Web server control is a powerful tool for displaying information from a data source. It is easy to use; you can display editable data in a professional-looking grid by setting only a few properties. At the same time, the grid has a sophisticated object model that provides you with great flexibility in how you display the data.

This paper addresses some of the questions about customizing grid display that are commonly asked in newsgroups, on Web sites, and in other developer forums.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchTopQuestionsAboutASPNETDataGridServerControl.asp




Q) When I try to do an Update from my Datagrid, I keep getting the old/original values. Why?

This is caused by calling DataBind() on the grid before retrieving the updated values. The DataBind() overwrites the updates with the original values. Usually caused by having the initial DataBind() of the grid called on every Page_Load. Solution: call DataBind() only the first time the page is loaded:

Sub Page_Load(s As Object, e As EventArgs)
If Not IsPostBack Then BindGrid()
End Sub


With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/

How do I specify more than one parameter for my HyperlinkColumn?

The HyperlinkColumn's DataNavigateUrlFormatString only supports one parameter. If you need more than one, switch to a TemplateColumn with an .
Note: You should always encode values passed into querystring to protect against spaces and special characters.













With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/

ASP.NET State Management Overview

HTTP is a stateless protocol. Each request is serviced as it comes; after the request is processed, all of the data is discarded. No state is maintained across requests even from the same client.

However, it is very useful to maintain state across requests for certain solutions. ASP.NET enables you to maintain both application state and session state through use of application and session variables respectively.

Learn more about

Application State
Session State
Configuring Session State

here - http://support.microsoft.com/default.aspx?scid=kb;en-us;307598&Product=aspnet



With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/

ASP.NET Page Framework Overview

The ASP.NET page framework is a scalable programming model that you can use on the server to dynamically generate Web pages. The ASP.NET page framework is the successor to Active Server Pages

Learn more about

Page Life Cycle
Page Events
Page Directives
Inline Versus Code-Behind Programming Models
here - http://support.microsoft.com/default.aspx?scid=kb;en-us;305141&Product=aspnet


With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/