Archive for the ‘.Net’ 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

h1

Set Document mode standard/Edge in IE

September 2, 2015

set Document mode standard/Edge in IE
Code :

Add meta tag in web page
<meta http-equiv=”X-UA-Compatible” content=”IE=edge” />

If you are using .net you can add this to the web.config

<system.webServer>
    <httpProtocol>
     <customHeaders>
        <add name="X-UA-Compatible" value="IE=edge" />
      </customHeaders>
    </httpProtocol>
</system.webServer>
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

Numeric only text box in .net Winforms

July 18, 2011

We can create Numeric only text boxes in .Net Windows forms application with adding below code in Key Press Event.

Numeric only text boxes

private void txtBox_KeyPress(object sender, KeyPressEventArgs e)

{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != ‘.’)
{
e.Handled = true;
}

// only allow one decimal point
if (e.KeyChar == ‘.’ && (sender as TextBox).Text.IndexOf(‘.’) > -1)
{
e.Handled = true;
}
}

 

Integer only text boxes

private void txtBox_KeyPress(object sender, KeyPressEventArgs e)

{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) )
{
e.Handled = true;
}

}

h1

Confirm Dialog in .Net WinForms

April 26, 2011

We can use MessageBox as Confirm Dialog box

Coding

if(MessageBox.Show(“Confirm this?”, “Test”, MessageBoxButtons.YesNo) == DialogResult.Yes)
{
/// Code for ‘Yes’
}
else
{
/// Code for ‘No’
}
h1

Making Non-selectable options and option groups in select

December 10, 2010

Making Non-selectable options and indented option groups are pretty easy in select control. disabled the options is not a correct way to do, use optgroup option.

Making Non-selectable options :

<select >
<option >groups</option>
<optgroup label=”group1″> </optgroup>
<option> group 1 item 1</option>
<option> group 1 item 2</option>
<optgroup label=”group2″> </optgroup>
<option> group 2 item 1</option>
<option> group 2 item 2</option>
</select>

Making indented option groups:

<select >
<option >groups</option>
<optgroup label=”group1″>
<option> group 1 item 1</option>
<option> group 1 item 2</option>
</optgroup>
<optgroup label=”group2″>
<option> group 2 item 1</option>
<option> group 2 item 2</option
>/optgroup>
</select>

 

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

AutoGenerateColumn property – Winforms

September 13, 2010

In datagridview properties can not find AutoGenerateColumn property, but can find pragmatically.

Here can set false to hide columns, need to do before set the datasource.

Datagridview1.AutoGenerateColumn = false; 
Datagridview1.DataSource = dt;