Friday, April 23, 2010

Cookies in asp.net

Introduction

Cookies are a very small text file placed on your hard drive by a Web Page server. It is essentially your identification card, and cannot be executed as code or deliver viruses. It is uniquely yours and can only be read by the server that gave it to you

cookies purpose is to tell the server that you returned to that Web page

It may help you by saving your time

If you personalize pages, or register for products or services, a cookie helps Microsoft remember who you are.

Next time you return, we know to show you the information you requested. Or, when you register for another product or service, all you need to do is type in your e-mail address and a password. We then fill in any questions you've already answered. Of course, if you never register or leave personal information with Microsoft, then the server only knows that someone with your cookie has returned to the Web site. You are in charge of deciding whether we know anything about you. But the more you tell us about yourself, the more we can help you find information or products you want

how to use it

in order to handle a cookie in your system you should aware of 2 thing:

1-how to create them

2- how to delete them

so,first in order to create a cookie you have 2 variable must be defined as they are the cookie name and the duration of the cookie before it expired

in order to create a cookie it looks like this one
////////////SET THE COOKIE//////////////

Response.Cookies["test"].Value = UserName.Text ;

Response.Cookies["test"].Expires = DateTime.Now.AddYears(30);


where test is the cookie name

and the username.text is the textbox that will be used to define the cookies information

in order to delete a cookie use this
Response.Cookies["test"].Expires = DateTime.Now.AddYears(-30);

in order to check whether the cookie is found or not and retrieve the cookies information use this
/////////check to see if cookie presented//////////

if (Request.Cookies["test"] == null)

TextBox2.Text = "No cookie found";

else

TextBox2 .Text = Request.Cookies["test"].Value;

/////////END check to see if cookie presented//////////

Source:http://www.codeproject.com/KB/aspnet/cookies_in_c_.aspx

Wednesday, April 21, 2010

open popup window in asp.net

script language="javascript" type="text/javascript">

var win=null;

function NewWindow(mypage,myname,w,h,scroll,pos)
{
if(pos=="random"){LeftPosition=(screen.width)?Math .floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',l ocation=no,directories=no,status=no,menubar=no,too lbar=no,resizable=yes';
win=window.open(mypage,myname,settings);
}


/script>

input type="button" value="Add Course" onclick="NewWindow('RegisterCourse.aspx','Register','800','550','yes','center');return false" onfocus="this.blur()" />


from: http://p2p.wrox.com/asp-net-1-0-1-1-basics/36479-open-popup-window-asp-net.html