-----RandomPasswordCrate.cs
public class RandomPasswordCrate
{
public static string GeneratePassword(int PasswordLength)
{
string _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789_";
Random randNum = new Random();
char[] chars = new char[PasswordLength];
int allowedCharCount = _allowedChars.Length;
for (int i = 0; i < PasswordLength; i++)
{
chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
}
return new string(chars);
}
}
------In cs Page
protected void Page_Load(object sender, EventArgs e)
{
generatePassword();
}
private void generatePassword()
{
string password = "Your password is: " + RandomPasswordCrate.GeneratePassword(8);
}
Monday, 9 January 2012
Monday, 26 December 2011
Thursday, 1 December 2011
Remove whitespace from HTML
private static readonly Regex RegexBetweenTags = new Regex(@">(?! )\s+", RegexOptions.Compiled);
private static readonly Regex RegexLineBreaks = new Regex(@"([\n\s])+?(?<= {2,})<", RegexOptions.Compiled);
-----------------------------
protected override void Render(HtmlTextWriter writer)
{
using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new System.IO.StringWriter()))
{
base.Render(htmlwriter);
string html = htmlwriter.InnerWriter.ToString();
writer.Write(RemoveWhitespaceFromHtml(html));
}
}
public static string RemoveWhitespaceFromHtml(string html)
{
html = RegexBetweenTags.Replace(html, ">");
html = RegexLineBreaks.Replace(html, "<");
return html.Trim();
}
Friday, 25 November 2011
Compress ViewState
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.IO.Compression;
/// <summary>
/// Summary description for ViewStateCompressor
/// </summary>
public class ViewStateCompressor
{
public ViewStateCompressor()
{
//
// TODO: Add constructor logic here
//
}
public static byte[] CompressViewState(byte[] uncompData)
{
using (MemoryStream mem = new MemoryStream())
{
CompressionMode mode = CompressionMode.Compress;
// Use the newly created memory stream for the compressed data.
using (GZipStream gzip = new GZipStream(mem, mode, true))
{
//Writes compressed byte to the underlying
//stream from the specified byte array.
gzip.Write(uncompData, 0, uncompData.Length);
}
return mem.ToArray();
}
}
public static byte[] DecompressViewState(byte[] compData)
{
GZipStream gzip;
using (MemoryStream inputMem = new MemoryStream())
{
inputMem.Write(compData, 0, compData.Length);
// Reset the memory stream position to begin decompression.
inputMem.Position = 0;
CompressionMode mode = CompressionMode.Decompress;
gzip = new GZipStream(inputMem, mode, true);
using (MemoryStream outputMem = new MemoryStream())
{
// Read 1024 bytes at a time
byte[] buf = new byte[1024];
int byteRead = -1;
byteRead = gzip.Read(buf, 0, buf.Length);
while (byteRead > 0)
{
//write to memory stream
outputMem.Write(buf, 0, byteRead);
byteRead = gzip.Read(buf, 0, buf.Length);
}
gzip.Close();
return outputMem.ToArray();
}
}
}
}
-----------------------------in page
pages smartNavigation="true" enableEventValidation="false" maxPageStateFieldLength="5000" viewStateEncryptionMode="Never"
-----------------------------in page cs
protected override void SavePageStateToPersistenceMedium(object pageViewState)
{
LosFormatter losformatter = new LosFormatter();
StringWriter sw = new StringWriter();
losformatter.Serialize(sw, pageViewState);
string viewStateString = sw.ToString();
byte[] b = Convert.FromBase64String(viewStateString);
b = ViewStateCompressor.CompressViewState(b);
ClientScript.RegisterHiddenField("__CUSTOMVIEWSTATE", Convert.ToBase64String(b));
}
// Deserialize view state
protected override object LoadPageStateFromPersistenceMedium()
{
string custState = Request.Form["__CUSTOMVIEWSTATE"];
byte[] b = Convert.FromBase64String(custState);
b = ViewStateCompressor.DecompressViewState(b);
LosFormatter losformatter = new LosFormatter();
return losformatter.Deserialize(Convert.ToBase64String(b));
}
Wednesday, 26 October 2011
Checking child nodes when checking the parent node
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
<script language="javascript" type="text/javascript">
function OnTreeClick(evt)
{
var src = window.event != window.undefined ? window.event.srcElement : evt.target;
var isChkBoxClick = (src.tagName.toLowerCase() == "input" && src.type == "checkbox");
if(isChkBoxClick)
{
var parentTable = GetParentByTagName("table", src);
var nxtSibling = parentTable.nextSibling;
if(nxtSibling && nxtSibling.nodeType == 1)
{
if(nxtSibling.tagName.toLowerCase() == "div") //if node has children
{
CheckUncheckChildren(parentTable.nextSibling, src.checked);
}
}
CheckUncheckParents(src, src.checked);
}
}
function CheckUncheckChildren(childContainer, check)
{
var childChkBoxes = childContainer.getElementsByTagName("input");
var childChkBoxCount = childChkBoxes.length;
for(var i = 0; i<childChkBoxCount; i++)
{
childChkBoxes[i].checked = check;
}
}
function CheckUncheckParents(srcChild, check)
{
var parentDiv = GetParentByTagName("div", srcChild);
var parentNodeTable = parentDiv.previousSibling;
if(parentNodeTable)
{
var checkUncheckSwitch;
if(check)
{
var isAllSiblingsChecked = AreAllSiblingsChecked(srcChild);
if(isAllSiblingsChecked)
checkUncheckSwitch = true;
else
return;
}
else
{
checkUncheckSwitch = false;
}
var inpElemsInParentTable = parentNodeTable.getElementsByTagName("input");
if(inpElemsInParentTable.length > 0)
{
var parentNodeChkBox = inpElemsInParentTable[0];
parentNodeChkBox.checked = checkUncheckSwitch;
CheckUncheckParents(parentNodeChkBox, checkUncheckSwitch);
}
}
}
function AreAllSiblingsChecked(chkBox)
{
var parentDiv = GetParentByTagName("div", chkBox);
var childCount = parentDiv.childNodes.length;
for(var i=0; i<childCount; i++)
{
if(parentDiv.childNodes[i].nodeType == 1)
{
if(parentDiv.childNodes[i].tagName.toLowerCase() =="table")
{
var prevChkBox = parentDiv.childNodes[i].getElementsByTagName("input")[0];
if(!prevChkBox.checked)
{
return false;
}
}
}
}
return true;
}
function GetParentByTagName(parentTagName, childElementObj)
{
var parent = childElementObj.parentNode;
while(parent.tagName.toLowerCase() != parentTagName.toLowerCase())
{
parent = parent.parentNode;
}
return parent;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeViewDemo" runat="server" ExpandDepth="0" ShowCheckBoxes="All">
<Nodes>
<asp:TreeNode NavigateUrl="" Text="P" Value="P">
<asp:TreeNode NavigateUrl="" Text="P:C1" Value="C1"></asp:TreeNode>
<asp:TreeNode NavigateUrl="" Text="P:C2" Value="C2"></asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode NavigateUrl="" Text="P1" Value="P1">
<asp:TreeNode NavigateUrl="" Text="P1:C1" Value="P1:C1"></asp:TreeNode>
<asp:TreeNode NavigateUrl="" Text="P1:C2" Value="P1:C2"></asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode NavigateUrl="" Text="P2" Value="P2">
<asp:TreeNode NavigateUrl="" Text="P2:C1" Value="P2:C1"></asp:TreeNode>
<asp:TreeNode NavigateUrl="" Text="P2:C2" Value="P2:C2"></asp:TreeNode>
</asp:TreeNode>
</Nodes>
</asp:TreeView>
</div>
</form>
</body>
</html>
------------------------------------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
TreeViewDemo.Attributes.Add("onclick", "OnTreeClick(event)");
}
Monday, 24 October 2011
Find Latitude And Longitude From C# Asp.net
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Net;
using System.Web.UI;
namespace GoogleGeocoder
{
public interface ISpatialCoordinate
{
decimal Latitude {get; set; }
decimal Longitude {get; set; }
}
public struct Coordinate : ISpatialCoordinate
{
private decimal _latitude;
private decimal _longitude;
public Coordinate(decimal latitude, decimal longitude)
{
_latitude = latitude;
_longitude = longitude;
}
#region ISpatialCoordinate Members
public decimal Latitude
{
get
{
return _latitude;
}
set
{
this._latitude = value;
}
}
public decimal Longitude
{
get
{
return _longitude;
}
set
{
this._longitude = value;
}
}
#endregion
}
public class Geocode
{
private const string _googleUri = "http://maps.google.com/maps/geo?q=";
private const string _googleKey = "ABQIAAAAAbmewSRic45gmeA3SqPU2xTN7OoKn4WqNHLTV0WVkMCPFkG3kxRiF6l_Jlmlap7r6S592BJ_XDSJtg";
private const string _outputType = "csv"; // Available options: csv, xml, kml, json
private static Uri GetGeocodeUri(string address)
{
address = HttpUtility.UrlEncode(address);
return new Uri(String.Format("{0}{1}&output={2}&key={3}", _googleUri, address, _outputType, _googleKey));
}
public static Coordinate GetCoordinates(string address)
{
WebClient client = new WebClient();
Uri uri = GetGeocodeUri(address);
string[] geocodeInfo = client.DownloadString(uri).Split(',');
return new Coordinate(Convert.ToDecimal(geocodeInfo[2]), Convert.ToDecimal(geocodeInfo[3]));
}
}
}
------------------------------------
using GoogleGeocoder;
Coordinate coordinate = Geocode.GetCoordinates("limkon");
decimal latitude = coordinate.Latitude;
decimal longitude = coordinate.Longitude;
Convert dd/MM/yyyy to mm/dd/yyyy in c# asp.net
string UrDate = "YourDate in dd/mm/yyyy"
System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
dateInfo.ShortDatePattern = "dd/MM/yyyy";
DateTime validDate = Convert.ToDateTime(UrDate, dateInfo);
Subscribe to:
Posts (Atom)
Get all non-clustered indexes
DECLARE cIX CURSOR FOR SELECT OBJECT_NAME(SI.Object_ID), SI.Object_ID, SI.Name, SI.Index_ID FROM Sys.Indexes SI ...
-
In C# using System; using System.Collections.Generic; using System.Linq; using System.Web; public class encode_decode { public ...
-
Create one class named RandomSNKGenerator using System; using System.Collections.Generic; using System.Linq; using System.Web; ...
-
Design page ------------ <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inheri...