Archive for the ‘C#’ Category

h1

requiredfieldvalidator not working onclientclick

May 24, 2016

 

Requiredfieldvalidator not working onclientclick added in button.

Code  : This code will not execute required field validator

 <asp:Button ID="btnSubmit" runat="server" Text="Check" 
OnClick="btnSubmit_Click" 
OnClientClick="javascript:return confirm('Are you sure to Submit?')"
 ValidationGroup="Submit" />

use Page_ClientValidate() predefined function to avoid issue.

Solution :

 <asp:Button ID="btnSubmit" runat="server" Text="Check" 
OnClick="btnSubmit_Click" 
OnClientClick="return CheckToProceed();"
 ValidationGroup="Submit" />

Javascript ::

function CheckToProceed() {
 if (Page_ClientValidate()) {
return confirm('Are you sure to Submit?')
}
}

Reference : stackoverflow

Advertisements
h1

Remove invalid characters from file name and path

February 4, 2015

Code :

string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());

foreach (char c in invalid)
{
strFileName = strFileName.Replace(c.ToString(), “”);
}

h1

ignore case in replace C#

July 3, 2014

we can ingnore case in replacing test using Regex

to use Regex include System.Text.RegularExpressions header file

Coding :

Regex.Replace(string,TextToRemove,string.Empty,RegexOptions.IgnoreCase);

h1

Convert Date to any format from any other format

June 30, 2012

Convert a one date format to any other date format.
Date format must not be a regular Date format like ‘MM/dd/yyyy’. it can be a any format like ‘yyyyMMdd’

Coding :

public string DateFormatter(string Date,string RevFormat,string ReqFormat)
{
DateTime MyDateConverted = DateTime.ParseExact(Date, RevFormat, System.Globalization.CultureInfo.InvariantCulture);
string ReqDate = MyDateConverted.ToString(@ReqFormat);
return ReqDate;
}
h1

Find a ‘Value’ or ‘Text’ is Exist in ASP DropDownList

October 21, 2010

If the ‘Value’ or ‘Text’ is not Exist it throws the error. We can find that item is exist in that DropDownList or not.

Code:

ListItem Item1 = drpLst.Items.FindByValue(“value”);
//or
ListItem Item1 = drpLst.Items.FindByText(“text”); 

if (Item1 == null)
{
//Not Exist
}
else
{
//Exist
}

h1

Convert XML to Datatable

July 19, 2010

DataSet ds= new DataSet();
DataTable dtComp = new DataTable();
System.IO.StringReader rdr;
string sXML =  xmldoc.ToString();
rdr = new System.IO.StringReader(sXML);
ds.ReadXml(rdr);
if ( ds.Tables.Count > 0)
{
dtComp = ds.Tables[0];
}

Convert XML to Datatable with XML Schema : Function

private DataSet getDataSet(string sXML, string xSchema)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
System.IO.StringReader rdr;
rdr = new System.IO.StringReader(sXML);
System.IO.StringReader rdrSchema;
rdrSchema = new System.IO.StringReader(xSchema);
ds.ReadXmlSchema(rdrSchema);
ds.ReadXml(rdr);
if (ds.Tables.Count > 0)
{
return ds;
}
return ds;
}