Saturday, March 21, 2009

To select All Controls from a Page

/// To select All Controls from a Page
foreach (Control ctr in Page.Form.Controls)
{
if (ctr is DropDownList)
{
DropDownList x = (DropDownList)ctr;
x.SelectedIndex = 1;
}

}

Friday, February 13, 2009

Download a file from server

using System.IO;

string filename = "resumexavier1.doc";

if (filename != "")
{

string path = Server.MapPath(".\\Files\\" + filename);
// Here resumexavier is in "Files" folder,thats y given ".\\Files\\"
// other wise we can write string path = Server.MapPath(filename);

System.IO.FileInfo file = new System.IO.FileInfo(path);

if (file.Exists)
{

Response.Clear();

Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

Response.AddHeader("Content-Length", file.Length.ToString());

Response.ContentType = "application/octet-stream";

Response.WriteFile(file.FullName);

Response.End();

}

else
{

Response.Write("This file does not exist.");

}

File Upload to server

using System.IO;
////////////

if (fupApplicationForms.HasFile)
{
string[] FileType = new string[] { ".doc", ".pdf", ".docx" };
string FileExtension = Path.GetExtension(fupApplicationForms.FileName).ToLower();
Boolean FileOk = false;
for (int i = 0; i < FileType.Length; i++)
{
if (FileExtension == FileType[i])
{
FileOk = true;
}
}
if (FileOk == true)
{

ApplicationFormsSP apsp = new ApplicationFormsSP();
string str, formname;
str = System.IO.Path.GetFileName(fupApplicationForms.PostedFile.FileName);
formname = Server.MapPath(".\\") + "ApplicationForms\\" + str;
FileInfo apform = new FileInfo(formname);
if (apform.Exists)
{
lblError.Text = "Sorry! A file with the name you specified already exists. Specify a different filename";

}
else
{

fupApplicationForms.PostedFile.SaveAs(formname);
int size = fupApplicationForms.PostedFile.ContentLength;
double kbsize = (double)size / 1024;
kbsize = Math.Round(kbsize, 2);
string sizeikb = kbsize + " KB";
apsp.AddApplicationForm(str, sizeikb);


}
}
else
{
lblError.Text = "File extention doesn't support";

}
}
else
{
lblError.Text = "Nothing to Upload";

}

Saturday, February 7, 2009

To Popup a window without tool buttons

public void Popup()
{
string popupScript;
popupScript = "script language='javascript'>";
popupScript += "window.open('examination.aspx','popup','width=1015, height=700,menubar=no,resizable=yes')";
popupScript += "script";
Response.Write(popupScript);
//Page.RegisterStartupScript("Popup", popupScript);
}

Avoid Cache memory (Disable back)

public void AvoidCache()
{
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Expires = 0;
HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
HttpContext.Current.Response.AddHeader("pragma", "no-cache");
HttpContext.Current.Response.AddHeader("cache-control", "private");
HttpContext.Current.Response.CacheControl = "no-cache";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetAllowResponseInBrowserHistory(true);
}


/////////////// Or //////////////

protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);

StringBuilder javaScript = new StringBuilder();

javaScript.Append("\nscript language=JavaScript>\n");
javaScript.Append("window.history.forward(0);\n");
javaScript.Append(" Page.RegisterClientScriptBlock("HistoryScript", javaScript.ToString());
}

Page Close

public void pageclose()
{
string str;
str = "script language=JavaScript";
//str += "window.opener = 'x';"
str += "window.close();";
str += "/script";
Response.Write(str);
}

To Expire Page history

public void pageExpire()
{
javaScript.Append("\nscript language=JavaScript>\n");
javaScript.Append("window.history.forward(0);\n");
javaScript.Append(" Response.Write(javaScript);
}