Friday, January 7, 2011

Button Click event not firing in the Update Panel

Hi,

This time i did little bit research on, why Button Click event not firing in the Update Panel?

I found some issues in code behind for button click event

Go through the below instructions carefully:

1) Have you used any Response objects in code behind in page load event or button click event

if yes, Remove the code, write alternate for it.

2) Have you used any control ID which will not under updatepanel contenttempplate for button click event?

if yes, put those controls under updatepanel content Template.

might your problem will solve.

Friday, November 19, 2010

Get radiobuttonlist Selected value and Text using javascript

To get RadioButtonList Selected values and Text on Button Click using Javascript in asp.net page (.aspx page).


See the below example:-

Write the below javascript under header section of .aspx page

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

function GetSelectedItem()
{
var RB1 = document.getElementById("<%=RadioButtonList1.ClientID%>");
var radio = RB1.getElementsByTagName("input");
var label = RB1.getElementsByTagName("label");
for (var i = 0; i < radio.length; i++) {
if (radio[i].checked)
{
alert("SelectedText = " + label[i].innerHTML);
alert("SelectedValue = " + radio[i].value);
}
}
return false;
}

</script>

Call the above javascript function in asp button, to get selected radiobuttonlist Text,value:

<asp:Button ID="btnSubmit" Text="Submit" runat="server" CausesValidation="true"
ValidationGroup="General" OnClientClick="return GetSelectedItem();" />

Hope this is help full

Validate Radiobuttonlist using Javascript

This time i want to explain, How to validate RadioButtonList using javascript in asp.net.

In this below example i am going to explain to validate radiobuttonlist, to select at least one radio button.

Technology : asp.net ,HTML ,JAVASCRIPT

Please follow the below example :

Write the below javascript in asp.net page (.aspx) page header section.

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

function Validate() {
var RB1 = document.getElementById("<%=RadioButtonList1.ClientID%>");
var radio = RB1.getElementsByTagName("input");
var isChecked = false;
for (var i = 0; i < radio.length; i++) {
if (radio[i].checked) {
isChecked = true;
break;
}
}
if (!isChecked) {
alert("Please select an item");
}
return isChecked;
}
</script>

call the above javascript function on button click event as follows:

<asp:Button ID="btnValidateRadioList" runat="server" Text="Validate"
OnClientClick = "return Validate();"/>

Hope this is Help full

Tuesday, November 16, 2010

File Upload control validation using Javascript

We often used to validate Fileupload control by file extension validation at server side.It is little bit time taking process but secure. If we want to validate file upload control with some known file extensions at client side, we need to use javascript.

See the below example to validate file upload control using javascript :


In aspx page header section just write the following javascript code:


<script type ="text/javascript">

// Declare known file extensions in an array
var validFilesTypes=["bmp","gif","png","jpg","jpeg","doc","xls"];

function ValidateFile()
{
var file = document.getElementById("<%=FileUpload1.ClientID%>");
var label = document.getElementById("<%=Label1.ClientID%>");
var path = file.value;
var ext=path.substring(path.lastIndexOf(".")+1,path.length).toLowerCase();
var isValidFile = false;
for (var i=0; i<validFilesTypes.length; i++)
{
if (ext==validFilesTypes[i])
{
isValidFile=true;
break;
}
}
if (!isValidFile)
{
label.style.color="red";
label.innerHTML="Invalid File. Please upload a File with" +
" extension:\n\n"+validFilesTypes.join(", ");
}
return isValidFile;
}
</script>


In aspx page body section :

<asp:fileupload id="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload"
OnClientClick = "return ValidateFile();" />
<asp:Label ID="Label1" runat="server" Text="" />

Hope this is helpfull..!

Saturday, November 13, 2010

unable to copy file access to the path is denied

In visual studio while building on application you might get the following error.

unable to copy file ' c://xxx/xxx...' access to the 'D://xx/xxx..' path is denied.

The reason behind this is the current file is in read only mode.

You just do the following steps, then the problem will resolve.

  • Go to the file copying file location or pasted location(In case if it is pasted and giving error) .
  • Right click on that file a context menu will be open.
  • Select properties.
  • A new window will open with General tab.
  • Under the general tab if Read only Permission is checked, just uncheck it.
  • Now build the application. The build will succeed.

Wednesday, October 27, 2010

Generating PDFs in C#.Net using iTextSharp


//For PDF
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.rtf;

using System.Collections;

//Method to generate PDF in C#
private static void CreateStandardNdaPDF(string pdfFilePath, StandardNDAConfig ndaConfigData, NDA ndaUserData)
{

//Define memory stream
System.IO.MemoryStream PDFStream = new System.IO.MemoryStream();

//Create pdf document object
Document document = new Document();

try
{

//Create a document at physical location
PdfWriter pdfwr = PdfWriter.GetInstance(document, PDFStream);

#region Method to add current date on footer of the PDF document
Phrase phrCurrentDate = new Phrase(System.DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt"));
//RtfHeaderFooter footer = new RtfHeaderFooter();
HeaderFooter footer = new HeaderFooter(phrCurrentDate, false);
footer.Border = Rectangle.NO_BORDER;
footer.Alignment = HeaderFooter.ALIGN_RIGHT;

document.Footer = footer;

#endregion

// open the document
document.Open();

//Define Fonts
float SMALL_FONT = 8.0f;
float BIG_FONT = 9.5f;
float LEADING = 12.0f;

//Define fonts
Font fontNormal = FontFactory.GetFont(FontFactory.TIMES_ROMAN, SMALL_FONT, Font.NORMAL);
Font fontBold = FontFactory.GetFont(FontFactory.TIMES_ROMAN, SMALL_FONT, Font.BOLD);
Font fontUnderLine = FontFactory.GetFont(FontFactory.TIMES_ROMAN, SMALL_FONT, Font.UNDERLINE);
Font fontBoldUnderLine = FontFactory.GetFont(FontFactory.TIMES_ROMAN, SMALL_FONT, Font.BOLD Font.UNDERLINE);
Font fontNormalBigger = FontFactory.GetFont(FontFactory.TIMES_ROMAN, BIG_FONT, Font.NORMAL);
Font fontBoldBigger = FontFactory.GetFont(FontFactory.TIMES_ROMAN, BIG_FONT, Font.BOLD);
Font fontUnderLineBigger = FontFactory.GetFont(FontFactory.TIMES_ROMAN, BIG_FONT, Font.UNDERLINE);
Font fontBoldUnderLineBigger = FontFactory.GetFont(FontFactory.TIMES_ROMAN, BIG_FONT, Font.BOLD Font.UNDERLINE);

//To add New line to the PDF document
Phrase phrNewLine = new Phrase("\n");
Phrase phrTab = new Phrase("\t");
Phrase phrSpace = new Phrase(" ",fontNormal);

//Header Table for NDA Logo and NDA Address
Table ndaTable = new Table(2, 1); // 2 columns,1 row
ndaTable.BorderWidth = 0;
ndaTable.Border = Rectangle.NO_BORDER;
ndaTable.DefaultCellBorder = Rectangle.NO_BORDER;
ndaTable.WidthPercentage = 100;
ndaTable.Cellspacing = 2.0f;

//Define and Add NDA Logo

//Image ndaLogo = Image.GetInstance(new Uri(Server.MapPath("../homepagetopzoneimage.jpg")));
//Image ndaLogo = Image.GetInstance(new Uri("http://idc-dk-is:801/sites/nda/PictureLibrary/lg_mchp.gif"));
Image ndaLogo = Image.GetInstance(ndaConfigData.NDALogo);
ndaLogo.Alignment = Image.ALIGN_LEFT;

Cell cellNDALogo = new Cell(ndaLogo);
Phrase ndaLogoText = new Phrase(ndaConfigData.NDALogoText, fontNormal);
cellNDALogo.AddElement(ndaLogoText);
cellNDALogo.VerticalAlignment = Cell.ALIGN_LEFT;
cellNDALogo.HorizontalAlignment = Cell.ALIGN_LEFT;
cellNDALogo.Leading = LEADING;
ndaTable.AddCell(cellNDALogo);

//Define and Add NDA Address
Phrase ndaSerialNumber = new Phrase(ndaUserData.SerialNumber, fontBoldBigger);
Phrase phrParticipant = new Phrase("PARTICIPANT ", fontBoldBigger);
Phrase phrParticipantLegalNameOfCompany = new Phrase(ndaUserData.ParticipantLegalNameOfCompany, fontBoldBigger);
Phrase phrParticipantDetails = new Phrase();
phrParticipantDetails.Add(phrParticipant);
phrParticipantDetails.Add(phrParticipantLegalNameOfCompany);

//Phrase phrParticipantName = new Phrase(ndaUserData.ParticipantLegalNameOfCompany, fontUnderLine);
Phrase ndaCompanyName = new Phrase(ndaConfigData.CompanyName, fontBoldBigger);
Phrase ndaAddress = new Phrase(ndaConfigData.Address, fontNormalBigger);
Phrase ndaCityStateZip = new Phrase(ndaConfigData.CityStateZip, fontNormalBigger);
Phrase ndaPhone = new Phrase(ndaConfigData.Phone, fontNormalBigger);
Phrase ndaFax = new Phrase(ndaConfigData.FAX, fontNormalBigger);

Cell cellNDAAddress = new Cell();
Phrase phrNDAAddress = new Phrase();

phrNDAAddress.Leading = LEADING;
phrNDAAddress.Add(ndaSerialNumber);
phrNDAAddress.Add(phrNewLine);
phrNDAAddress.Add(phrParticipantDetails);
phrNDAAddress.Add(phrNewLine);
phrNDAAddress.Add(ndaCompanyName);
phrNDAAddress.Add(phrNewLine);
phrNDAAddress.Add(ndaAddress);
phrNDAAddress.Add(phrNewLine);
phrNDAAddress.Add(ndaCityStateZip);
phrNDAAddress.Add(phrNewLine);
phrNDAAddress.Add(ndaPhone);
phrNDAAddress.Add(phrNewLine);
phrNDAAddress.Add(ndaFax);
phrNDAAddress.Add(phrNewLine);

cellNDAAddress.Add(phrNDAAddress);

ndaTable.AddCell(cellNDAAddress);
cellNDAAddress.VerticalAlignment = Cell.ALIGN_LEFT;
cellNDAAddress.HorizontalAlignment = Cell.ALIGN_LEFT;
cellNDAAddress.UseAscender = true;
document.Add(ndaTable);
document.Add(phrNewLine);
document.Add(phrNewLine);

//Define and Add NDA Document Title
Paragraph parNDADocTitle = new Paragraph(ndaConfigData.NDADocTitle, fontBoldBigger);
parNDADocTitle.Alignment = 1;
parNDADocTitle.Add(phrNewLine);
parNDADocTitle.Add(phrNewLine);

document.Add(parNDADocTitle);
//document.Add(phrNewLine);
//document.Add(phrNewLine);

//Define and Add NDA Doc Effective Date
Paragraph parNDAEffectiveDate = new Paragraph();
Chunk chnEffectiveDate = new Chunk("EFFECTIVE DATE: ", fontBold);
parNDAEffectiveDate.Add(chnEffectiveDate);
DateTime dtEffective = Convert.ToDateTime(ndaUserData.EffectiveDate);
Chunk chnDateEffective = new Chunk(dtEffective.ToString("MMMM dd, yyyy"), fontBoldUnderLine);
parNDAEffectiveDate.Add(chnDateEffective);
parNDAEffectiveDate.Add(phrNewLine);
parNDAEffectiveDate.Add(phrNewLine);
document.Add(parNDAEffectiveDate);
//document.Add(phrNewLine);
//document.Add(phrNewLine);

//Define and Add NDA Header
Phrase phrNDAHeader = ReplaceHtmlText(ndaConfigData.Header, SMALL_FONT);
phrNDAHeader.Font = fontNormal;
phrNDAHeader.Leading = LEADING;
phrNDAHeader.Add(phrNewLine);
phrNDAHeader.Add(phrNewLine);
document.Add(phrNDAHeader);

Table ndaSecTable = new Table(2, 3); // 2 columns,3 rows
ndaSecTable.TableFitsPage = true;
ndaSecTable.Border = Table.NO_BORDER;
ndaSecTable.BorderColor = iTextSharp.text.Color.WHITE;
ndaSecTable.WidthPercentage = 100;

#region Sections

#region Amendment Section1
Phrase phrSecTitle1 = new Phrase(ndaConfigData.SecTitle1, fontBoldUnderLine);

Phrase phrSec1 = new Phrase(ReplaceHtmlText(ndaConfigData.Sec1, SMALL_FONT));
Phrase phrSec1Details = new Phrase();
phrSec1Details.Leading = LEADING;
Phrase phr1 = new Phrase("1. ", fontBold);
phrSec1Details.Add(phr1);
phrSec1Details.Add(phrSecTitle1);
phrSec1Details.Add(phrTab);
phrSec1Details.Add(phrSec1);
phrSec1Details.Add(phrNewLine);
//Display Note only if the section note is not empty
if (ndaConfigData.SecNote1 != "")
{
Phrase phrSecNote1 = new Phrase(ReplaceHtmlText(ndaConfigData.SecNote1, SMALL_FONT));
phrSec1Details.Add(phrSecNote1);
phrSec1Details.Add(phrNewLine);
phrSec1Details.Add(phrSecNote1);
}
phrSec1Details.Add(phrNewLine);

Cell cellNDASec1 = new Cell();
cellNDASec1.AddElement(phrSec1Details);
cellNDASec1.Border = Table.NO_BORDER;
cellNDASec1.VerticalAlignment = Cell.ALIGN_JUSTIFIED;
cellNDASec1.HorizontalAlignment = Cell.ALIGN_JUSTIFIED;

ndaSecTable.AddCell(cellNDASec1);

#endregion

#region Amendment Section2
Phrase phrSecTitle2 = new Phrase(ndaConfigData.SecTitle2, fontBoldUnderLine);
Phrase phrSec2 = new Phrase(ReplaceHtmlText(ndaConfigData.Sec2+ " ", SMALL_FONT));
Phrase phrDisclosingParty = new Phrase(ndaUserData.DisclosingParties,fontUnderLine);
Phrase phr2 = new Phrase("2. ", fontBold);
Phrase phrSec2Details = new Phrase();
phrSec2Details.Leading = LEADING;
phrSec2Details.Add(phr2);
phrSec2Details.Add(phrSecTitle2);
phrSec2Details.Add(phrTab);
phrSec2Details.Add(phrSec2);
phrSec2Details.Add(phrSpace);
phrSec2Details.Add(phrSpace);
phrSec2Details.Add(phrSpace);
phrSec2Details.Add(phrDisclosingParty);
phrSec2Details.Add(phrNewLine);

//Display Note only if the section note is not empty
if (ndaConfigData.SecNote2 != "")
{
Phrase phrSecNote2 = new Phrase(ReplaceHtmlText(ndaConfigData.SecNote2, SMALL_FONT));
phrSec2Details.Add(phrSecNote2);
phrSec2Details.Add(phrNewLine);
}
phrSec2Details.Add(phrNewLine);


Cell cellNDASec2 = new Cell();
cellNDASec2.AddElement(phrSec2Details);
cellNDASec2.Border = Table.NO_BORDER;
cellNDASec2.VerticalAlignment = Cell.ALIGN_JUSTIFIED;
cellNDASec2.HorizontalAlignment = Cell.ALIGN_JUSTIFIED;

ndaSecTable.AddCell(cellNDASec2);

#endregion

#region Amendment Section3
Phrase phrSecTitle3 = new Phrase(ndaConfigData.SecTitle3, fontBoldUnderLine);
Phrase phrSec3 = new Phrase(ReplaceHtmlText(ndaConfigData.Sec3, SMALL_FONT));

Phrase phrSec3Details = new Phrase();
phrSec3Details.Leading = LEADING;
Phrase phr3 = new Phrase("3. ", fontBold);
phrSec3Details.Add(phr3);
phrSec3Details.Add(phrSecTitle3);
phrSec3Details.Add(phrTab);
phrSec3Details.Add(phrSec3);

phrSec3Details.Add(phrNewLine);

Phrase phrSecLabelA3 = new Phrase(ReplaceHtmlText(ndaConfigData.SecLabelA3 + " ", SMALL_FONT));
Phrase phrMCHPrepresentative = new Phrase(ndaUserData.MCHPRepersentative, fontUnderLine);
//Phrase phrMCHPConfidentialInfo = new Phrase(ndaUserData.DescriptionOfConfidentialInformationMCHP, fontUnderLine);

Phrase phrSecLabelB3 = new Phrase(ReplaceHtmlText(ndaConfigData.SecLabelB3 + " ", SMALL_FONT));
Phrase phrParticipantRepresentative = new Phrase(ndaUserData.ParticipantRepersentative, fontUnderLine);
//Phrase phrParticipantConfidentialInfo = new Phrase(ndaUserData.DescriptionOfConfidentialInformationParticipant, fontUnderLine);

phrSec3Details.Add(phrSecLabelA3);
phrSec3Details.Add(phrMCHPrepresentative);
phrSec3Details.Add(phrNewLine);
phrSec3Details.Add(phrSecLabelB3);
phrSec3Details.Add(phrParticipantRepresentative);
phrSec3Details.Add(phrNewLine);
//Display Note only if the section note is not empty
if (ndaConfigData.SecNote3 != "")
{
Phrase phrSecNote3 = new Phrase(ReplaceHtmlText(ndaConfigData.SecNote3, SMALL_FONT));

phrSec3Details.Add(phrSecNote3);
phrSec3Details.Add(phrNewLine);
}
phrSec3Details.Add(phrNewLine);



Cell cellNDASec3 = new Cell();
cellNDASec3.AddElement(phrSec3Details);
cellNDASec3.Border = Table.NO_BORDER;
cellNDASec3.VerticalAlignment = Cell.ALIGN_JUSTIFIED;
cellNDASec3.HorizontalAlignment = Cell.ALIGN_JUSTIFIED;

ndaSecTable.AddCell(cellNDASec3);

#endregion

#endregion

document.Add(ndaSecTable);

//Table for Footer for MCHP and Participant
Table tblNDASignature = new Table(2, 1); // 2 columns,1 row
tblNDASignature.BorderWidth = 0;
tblNDASignature.Border = Rectangle.NO_BORDER;
tblNDASignature.DefaultCellBorder = Rectangle.NO_BORDER;
tblNDASignature.WidthPercentage = 100;
tblNDASignature.TableFitsPage = true;

//Define and Add MCHP Signature Details
Phrase phrMCHPSignatureTitle = new Phrase(ReplaceHtmlText(ndaConfigData.SignatureTitle1, BIG_FONT));
phrMCHPSignatureTitle.Font = fontBoldBigger;
Phrase phrAddress = new Phrase("Address: ", fontNormal);
Phrase phrMCHPBy = new Phrase("By: ", fontNormal);
Phrase phrMCHPPrintName = new Phrase("Print Name: ", fontNormal);
Phrase phrTitle = new Phrase("Title: ", fontNormal);
Phrase phrLine1 = new Phrase("_________________________________________________", fontNormal);
Phrase phrLine2 = new Phrase("__________________________________________", fontNormal);
Phrase phrLine3 = new Phrase("_______________________________________________", fontNormal);

Cell cellMCHPSignature = new Cell();
cellMCHPSignature.Leading = LEADING;

cellMCHPSignature.Add(phrMCHPSignatureTitle);
cellMCHPSignature.Add(phrNewLine);
cellMCHPSignature.Add(phrNewLine);
cellMCHPSignature.Add(phrNewLine);
cellMCHPSignature.Add(phrMCHPBy);
cellMCHPSignature.Add(phrLine1);
cellMCHPSignature.Add(phrNewLine);
cellMCHPSignature.Add(phrNewLine);
cellMCHPSignature.Add(phrMCHPPrintName);
cellMCHPSignature.Add(phrLine2);
cellMCHPSignature.Add(phrNewLine);
cellMCHPSignature.Add(phrNewLine);
cellMCHPSignature.Add(phrTitle);
cellMCHPSignature.Add(phrLine3);
cellMCHPSignature.Add(phrNewLine);
cellMCHPSignature.VerticalAlignment = Cell.ALIGN_LEFT;
cellMCHPSignature.HorizontalAlignment = Cell.ALIGN_LEFT;
tblNDASignature.AddCell(cellMCHPSignature);

//Define and Add Participant Signature Details
Phrase strParticipant = new Phrase(ReplaceHtmlText(ndaConfigData.Participant + " ", BIG_FONT));
strParticipant.Font = fontBoldBigger;
Phrase phrParticipantName = new Phrase(ndaUserData.ParticipantLegalNameOfCompany, fontUnderLine);
Phrase phrParticipantStreet = new Phrase(ndaUserData.ParticipantStreet + ", ", fontUnderLine);
Phrase phrParticipantCity = new Phrase(ndaUserData.ParticipantCity + ", ", fontUnderLine);
Phrase phrParticipantState = new Phrase(ndaUserData.ParticipantState + ", ", fontUnderLine);
Phrase phrParticipantCountry = new Phrase(ndaUserData.ParticipantCountry + ", ", fontUnderLine);
Phrase phrParticipantZip = new Phrase(ndaUserData.ParticipantPinCode, fontUnderLine);
Phrase phrParticipantAddress = new Phrase();
phrParticipantAddress.Add(phrParticipantStreet);
phrParticipantAddress.Add(phrParticipantCity);
phrParticipantAddress.Add(phrParticipantState);
phrParticipantAddress.Add(phrParticipantCountry);
phrParticipantAddress.Add(phrParticipantZip);
phrParticipantAddress.Font = fontUnderLine;

Cell cellParticipantSignature = new Cell();
cellParticipantSignature.Leading = LEADING;

cellParticipantSignature.Add(strParticipant);
cellParticipantSignature.Add(phrParticipantName);
cellParticipantSignature.Add(phrNewLine);
cellParticipantSignature.Add(phrAddress);
cellParticipantSignature.Add(phrParticipantAddress);
cellParticipantSignature.Add(phrNewLine);
cellParticipantSignature.Add(phrNewLine);
cellParticipantSignature.Add(phrMCHPBy);
cellParticipantSignature.Add(phrLine1);
cellParticipantSignature.Add(phrNewLine);
cellParticipantSignature.Add(phrNewLine);
cellParticipantSignature.Add(phrMCHPPrintName);
cellParticipantSignature.Add(phrLine2);
cellParticipantSignature.Add(phrNewLine);
cellParticipantSignature.Add(phrNewLine);
cellParticipantSignature.Add(phrTitle);
cellParticipantSignature.Add(phrLine3);
cellParticipantSignature.Add(phrNewLine);
cellParticipantSignature.VerticalAlignment = Cell.ALIGN_LEFT;
cellParticipantSignature.HorizontalAlignment = Cell.ALIGN_LEFT;
tblNDASignature.AddCell(cellParticipantSignature);


document.Add(tblNDASignature);
}
catch (DocumentException de)
{

}
finally
{
document.Close();
}


byte[] PdfByte = PDFStream.GetBuffer();
PDFStream.Flush();
PDFStream.Close();
NDACommonFunctions.UploadFile(PdfByte, pdfFilePath);
}

Tuesday, October 26, 2010

To Get DataColumn Name in DataTable in c#.net

To get DataColumnName in dataTable By the following code:

In C#.net

using System.Data;

//objDataTable is a DataTable object which is having Data

objDataTable.Columns[columnIndex].ColumnName="New ColumnName";


(OR)


objDataTable.Columns["OldColumnName"].ColumnName="New ColumnName";