November 28, 2011

Coding in server side for uploading Photo


protected void imgbtnAddPhoto_Click(object sender, ImageClickEventArgs e)

{

try

{

if (flpAddPhoto.PostedFile != null && flpAddPhoto.HasFile)

{





System.IO.FileInfo inf = new FileInfo(flpAddPhoto.FileName);

string Extension = inf.Extension.ToString().ToLower();

if (Extension == ".jpg" || Extension == ".jpeg" || Extension == ".png" || Extension == ".bmp" || Extension == ".gif")

{

double length = (flpAddPhoto.PostedFile.InputStream.Length) / 1024;

if (length <= 51200)

{

string fileName = System.IO.Path.GetFileName(flpAddPhoto.PostedFile.FileName);

if (fileName != "")

{

SPSecurity.RunWithElevatedPrivileges(delegate()

{

string _fileTime = DateTime.Now.ToFileTime().ToString();

string _fileorgPath = System.IO.Path.GetFullPath

(flpAddPhoto.PostedFile.FileName);

string _newfilePath = _fileTime + "~" + fileName;

string tempFolder = Environment.GetEnvironmentVariable("TEMP");

string _filepath = tempFolder + _newfilePath;

flpAddPhoto.PostedFile.SaveAs(_filepath);

Stream fStream = flpAddPhoto.PostedFile.InputStream;

byte[] Contents = new byte[fStream.Length];

fStream.Read(Contents, 0, (int)fStream.Length);

imgPhoto.ImageUrl = _filepath;

if (insert(fileName, _filepath))

{

}

ViewState["PhotoContent"] = Contents;

ViewState["FilePath"] = _filepath;

ViewState["Filename"] = fileName;

fStream.Close();

fStream.Dispose();

});

}

}

}

}

}

catch (Exception ex)

{

ErrorLog.Add(Convert.ToString(ex.Message), Convert.ToString(ex.StackTrace));

Response.Redirect("/_catatlogs/masterpage/Error.aspx");

}

}

Expiry date validation






function ExpDateChk(sender, args)
{

       
var Today = new Date();

var expDate = new Date(document.getElementById("<%# txtLicenceExpiryDate.ClientID %>").value)

if (Today.setDate(Today.getDate()+6) > expDate) {

args.IsValid = false;

}

else {

args.IsValid = true;

}

}

Combining more than one Checboxes using string builder



public string GetMuscleGravity()

{
StringBuilder sb = new StringBuilder();


            if (chkbxLU.Checked)

{

sb.Append((int)Helper.MuscleGravityOT.chkbxLU).ToString();

sb.Append(",");

}

if (chkbxRU.Checked)

{

sb.Append((int)Helper.MuscleGravityOT.chkbxRU).ToString();

sb.Append(",");

}

if (chkLH.Checked)

{

sb.Append((int)Helper.MuscleGravityOT.chkLH).ToString();

sb.Append(",");

}

if (chkbxRH.Checked)

{

sb.Append((int)Helper.MuscleGravityOT.chkbxRH).ToString();

sb.Append(",");

}

if (sb == null)

{

sb.Append(0).ToString();

}

return sb.ToString();

}

Enumeration

public enum Discipline

{
OT=1,
OTA,
PT,
PTA,
ST,
STA,
SP,
SPA,
}
public enum PatientStatus


{
New=1,
Assigned,
Inprogress,
Completed,
Terminate,
}
public enum HurtRating


{
Nohurt = 1,
HurtsLittleBit,
HurtsLittleMore,
HurtsEvenMore,
HurtsWholeLot,
HurtsWorst,
}

Conversion of url to PDF

#region

{

Convert URLtoPDFpublic static void ConvertURLToPDF(string Url, string downloadName)string urlToConvert = Url;// Create the PDF converter. Optionally you can specify the virtual browser // width as parameter. 1024 pixels is default, 0 means autodetect
PdfConverter pdfConverter = new PdfConverter();// set the license key - requiredpdfConverter.LicenseKey =
ConfigurationManager.AppSettings["LicenseKey"].ToString();// set the converter options - optionalpdfConverter.PdfDocumentOptions.PdfPageSize =
pdfConverter.PdfDocumentOptions.PdfCompressionLevel =
pdfConverter.PdfDocumentOptions.PdfPageOrientation =
 
PdfPageSize.A4;PdfCompressionLevel.Normal;PDFPageOrientation.Portrait;// set if header and footer are shown in the PDF - optional - default is false pdfConverter.PdfDocumentOptions.ShowHeader =
pdfConverter.PdfDocumentOptions.ShowFooter =
false;false;// set to generate a pdf with selectable text or a pdf with embedded image - optional - default is truepdfConverter.PdfDocumentOptions.GenerateSelectablePdf =
pdfConverter.ScriptsEnabled =
true;true;// set if the ActiveX controls (like Flash player) are enabled during conversion // to a PDF with selectable text - optional - default is falsepdfConverter.ActiveXEnabled =
 
true;// set PDF security options - optionalpdfConverter.PdfSecurityOptions.CanPrint =
true;//pdfConverter.PdfSecurityOptions.CanEditContent = true;//pdfConverter.PdfSecurityOptions.UserPassword = "";//set PDF document description - optionalpdfConverter.PdfDocumentInfo.AuthorName =
"Winnovative HTML to PDF Converter";// Performs the conversion and get the pdf document bytes that you can further // save to a file or send as a browser response

byte[] pdfBytes = pdfConverter.GetPdfBytesFromUrl(urlToConvert);// send the PDF document as a response to the browser for downloadSystem.Web.
response.Clear();
response.AddHeader(
response.AddHeader(

response.Flush();
response.BinaryWrite(pdfBytes);
response.Flush();
response.End();
}
HttpResponse response = System.Web.HttpContext.Current.Response;"Content-Type", "binary/octet-stream");"Content-Disposition","attachment; filename=" + downloadName + ".pdf; size=" + pdfBytes.Length.ToString());#endregion

Gridview - RowDataBound



RowDatabound
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#94a8b7'");

// This will be the back ground color of the GridView Control

if (e.Row.RowIndex % 2 == 0)

{

e.Row.Attributes.Add(
"onmouseout", "this.style.backgroundColor='#d0d9e2'");

}

else

{

e.Row.Attributes.Add(
"onmouseout", "this.style.backgroundColor='#e7ebf0'");

}

Back Button in JS



window.history.go(-1);
window.location.reload();
}

window.history.go(-1);
}
}
function GoBack() {if ($.browser.msie) {else {

Validate dropdown

function ValidateddlState(sender, args)
 {
Var str = new String();

str = args.Value;

     


if (str == "0") {

args.IsValid =
false;

}



else {

args.IsValid =
true;

}

}

November 25, 2011

Clear controls

 private void ClearControls()
         {
             foreach (Control container in Parent.Controls)
             {
                 foreach (Control ctrl in container.Controls)
                 {
                     if (ctrl is TextBox)
                     {
                         ((TextBox)ctrl).Text = string.Empty;
                     }
                     if (ctrl is CheckBoxList)
                     {
                         ((CheckBoxList)ctrl).ClearSelection();
                     }
                     if (ctrl is RadioButton)
                     {
                         ((RadioButton)ctrl).Checked = false;
                     }
                 }
             }
         }

Zipcode validation


function Patientzipcode(sender, args)
 {


var zipcode = document.getElementById('<%= txtZipCode.ClientID%>').value;
if (zipcode == "00000"
{
document.getElementById('<%= CustmZipcode.ClientID%>').innerHTML = "Please enter valid zipcode";
return args.IsValid = false;
}
else


{
return args.IsValid = true;
}
}

November 16, 2011

Phone no validation

function ValidatetxtPhone(sender, args)
{

document.getElementById('<%# CustomPhoneNumber.ClientID %>').innerHTML = "";

var txt1 = document.getElementById('<%# txtPhone1.ClientID %>').value;

var txt2 = document.getElementById('<%# txtPhone2.ClientID %>').value;

var txt3 = document.getElementById('<%# txtPhone3.ClientID %>').value;

if ((txt1 == "000") && (txt2 == "000") && (txt3 == "0000")) {

document.getElementById('<%# CustomPhoneNumber.ClientID %>').innerHTML = "Please enter valid phone number";

return args.IsValid = false;

}

else if ((txt1 != "") && (txt1 == 3)) {


if ((txt2 != "") && (txt2 == 3)) {

if ((txt3 != "") && (txt3 == 4)) {

return args.IsValid = true;

}

}

}

else if ((txt1 == "") && (txt2 == "") && (txt3 == "")) {


document.getElementById('<%# CustomPhoneNumber.ClientID %>').innerHTML = "Please enter phone number(### ### ####)";

return args.IsValid = false;

}

else if ((txt1.length < 3) || (txt2.length < 3) || (txt3.length < 4)) {


document.getElementById('<%# CustomPhoneNumber.ClientID %>').innerHTML = "Please enter valid phone number";

return args.IsValid = false;

}

else {


return args.IsValid = true;

}

}

Fileupload validation

function ValidateFileUpload(sender, args)
 {

document.getElementById('<%# cvFlpAttach.ClientID %>').innerHTML = "";

var fl = document.getElementById('<%# FlpAttach.ClientID %>').value;

if (fl != "") {

var ext = document.getElementById('<%# FlpAttach.ClientID %>').value;

if (ext.length > 0) {

var str = ext.substring(ext.lastIndexOf('\\') + 1);

var sInvalidChars = "#%@&*:<>?\/{|}~";

var myIndex = 0;

args.IsValid = false;

var index = 0;

while (index < str.length) {

if (sInvalidChars.indexOf(str.charAt(index)) != -1) {

myIndex = myIndex + 1;

}

index++;

}

ext = ext.substring(ext.lastIndexOf('.') + 1);


ext = ext.toLowerCase();

if (ext != "doc" && ext != "docx" && ext != "pdf" && ext != "xls" && ext != "xlsx" && ext != "msg" && ext != "txt" && ext != "jpeg" && ext != "png" && ext != "rtf" && ext != "gif" && ext != "pptx" && ext != "ppt" && ext != "pps" && ext != "jpg" && ext != "xps" && ext != "eml") {


document.getElementById('<%# cvFlpAttach.ClientID %>').innerHTML = "Please upload valid file format (pdf/ppt/rtf/msg/doc/docx/xls/xlsx/jpeg/gif/png/txt/xps/pps/eml) only"

return args.IsValid = false;

}
else if (myIndex > 0) {


document.getElementById('<%# cvFlpAttach.ClientID %>').innerHTML = "Sorry, Invalid characters in filename(#%@&*:<>?\/{|}~).";

return args.IsValid = false;

}

else {


document.getElementById('<%# cvFlpAttach.ClientID %>').innerHTML = "";

return args.IsValid = true;

}

}

}

}

Maximum length for Multiline




function limit(obj, length)
{

var maxlength = length;

if (obj.value.length >= maxlength) {

alert("Please note maximum characters exceeded, maximum length is 2000");


}

}

function limitlength(obj1, length1) {


var maxlength = length1;

if (obj1.value.length >= maxlength) {


obj1.value = obj1.value.substring(0, maxlength);

}

}