Tuesday, November 3, 2009

Tips and tricks(ASP .Net)

ASP.NET 1) To access requested page url
Page.Request.UrlReferrer.AbsoluteUri
2) Not to save page in browser's Cache
call Page.Response.Cache.SetNoStore(); Page.Response.Cache.SetAllowResponseInBrowserHistory(false);
in Base Master file's OnLoad() event or Page_Load() of page load event
3) To post any data as a Form's POST method use following class
3.1) First need to call Add() method to set fields name and values
Note: If it is asp.net target site and not handled Remote form posting request in Page_Load() then need to pass __ViewState value as parameter and also submit button type and ID should match to remote site page submit button. In this case remote site's button click event will get triggered automatically.
3.2) Post() method to post requset
public class RemotePost { string url; private System.Collections.Specialized.NameValueCollection Inputs = new System.Collections.Specialized.NameValueCollection(); public string Url { get { return url; } set { url = value; } } public string Method = "post"; public string FormName = "TestForm"; public void Add(string name, string value) { Inputs.Add(name, value); }
public void Post() { try { //Clear old respose System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.Write(""); System.Web.HttpContext.Current.Response.Write(string.Format("")); System.Web.HttpContext.Current.Response.Write(string.Format("
", FormName, Method, Url)); //Add all parameters as html input hidden controls with id,name and value for (int i = 0; i < name="\" id="\" type="\" value="\">", Inputs.Keys[i], Inputs[Inputs.Keys[i]])); } //Hide submit button System.Web.HttpContext.Current.Response.Write(string.Format("
")); System.Web.HttpContext.Current.Response.Write("
"); //trigger submit button click event using javascript click() method System.Web.HttpContext.Current.Response.Write(""); System.Web.HttpContext.Current.Response.Write(""); //Complete request HttpContext.Current.ApplicationInstance.CompleteRequest();
} catch (Exception ex) {
throw ex; }
} }
4) Force browser to download file(image filem .js or .css etc) from server then need to add querystring parameter where we have referening perticular filee.g. Here we are passing ?Version=1 value as a query string parameter and if file name from browser's temp folder and this mismatch thenbrower will download file from server and will refer latest one.If you modify CustomValidation.js file then you can add Version=2 value as query string parameter
This problem is mostly with IE 7.0 brower.
5) To determine who caused postback, use Request.Form["__EVENTTARGET"]. It holds the UniqueID of the control that caused postback.If the control is a button, its UseSubmitBehavior must be set to false. So in case you are getting nulls, you probably have UseSubmitBehavior=true.
6) Not to ask Remember Password or any field by any browser Add Autocomplete=off Attribute(like ControlID.Attribute.Add("Autocomplete","off");) to the User ID Textbox control.
7) Allow User to Read/Write/Delete disk file even if another user is using, Use FileShare mode with FileStream object as shown below.
using (FileStream fs = new FileStream("C:\\dat.txt", FileMode.Create, FileAccess.Write, FileShare.Write FileShare.Read FileShare.Delete)) { while (true) { fs.WriteByte(84); fs.Flush(); Console.Write("."); Thread.Sleep(1000); } }

No comments:

Post a Comment