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);
}

Password Encription

public void EncryptPassword(string strPwd)
{
int i;
double x = 1.0,j = 0;
for (i = 1; i <= strPwd.Length; i++)
{
char str = char.Parse(strPwd.Substring(i-1, 1));
x = Math.Sqrt(x * i * Convert.ToInt32(str));
}
for (i = 1; i <= 5; i++)
{
j = x * i * 15;
int val=(int)j;
encrpass = encrpass + Convert.ToString(val, 16).ToUpper();
}

}

To popup a page using script

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

Split Date and Time

DateTime.Now.ToString("dd / MMM / yyyy - hh:mm:ss tt");

For Confirmation Messagebox

Client Side between HTML Head tags
.................................
Inside of Head (after Title)

script language="javascript" type="text/javascript">
function getconfirmation(str)
{
if (confirm(str)==true)
return true;
else
return false;
}
script>

...............

In Grid
..............

protected void gvAdressProof_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lbtnDelete = new LinkButton();
lbtnDelete = (LinkButton)e.Row.Cells[2].FindControl("lbtnDelete");
string str = "Are you sure to delete?";
lbtnDelete.Attributes.Add("OnClick", "return getconfirmation('" + str + "');");
}
}

.................
If Details View
..................

protected void dvUser_ItemCreated(object sender, EventArgs e)
{
if (dvUser.CurrentMode == DetailsViewMode.ReadOnly && dvUser.Rows.Count > 0)
{
LinkButton lbtnDelete = new LinkButton();
lbtnDelete = (LinkButton)dvUser.FooterRow.FindControl("lbtnDelete");
string str = "Are you sure to delete?";
lbtnDelete.Attributes.Add("OnClick", "return getconfirmation('" + str + "');");
}
}
}

To Print a window in asp.net

public void Print()
{
string Script;
Script = "";
Response.Write(Script);
}

MessageBox in AJAX

protected void Button3_Click(object sender, EventArgs e)
{
string jv = "alert('Select exam name');";
ScriptManager.RegisterClientScriptBlock(Button3, Button3.GetType(), "msg", jv, true);
}

Export To Excel in ASP.NET

public void ExportToExcel()
{
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
String savefile = "Consolidate Report";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + savefile + ".xls");
HttpContext.Current.Response.Charset = "";
gv.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}

public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}