Home »

Disable Viewstate of a page (No postback) in C# .NET

No comments

Simply add this function in the code behind of that page
protected override void Render(HtmlTextWriter output)
        {
            StringWriter stringWriter = new StringWriter();
            HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter);
            base.Render(textWriter);
            textWriter.Close();
            string strOutput = stringWriter.GetStringBuilder().ToString();
            strOutput = Regex.Replace(strOutput, "<input[^>]*id=\"__VIEWSTATE\"[^>]*>", "", RegexOptions.Singleline);
            output.Write(strOutput);
        }

Generate Random String in C#

No comments
Sometimes when I want to generate a random string and I forget this function, so I have to search on google or in my old project. It's quiet annoyed. So I post it here to make everything more convenient xD.
    private readonly Random random = new Random();
    private const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private string RandomString(int size)
    {
        StringBuilder strBuilder = new StringBuilder();

        for (int i = 0; i < size; i++)
        {
            strBuilder.Append(chars[random.Next(chars.Length)]);
        }
        return strBuilder.ToString();
    }
Because the random object is seeded from the system clock, so if you instantiate it inside the RandomString(), you will get the same result (string) when you call your method several times in quick succession. So you can put your Random instance outside the method as above code or you can do as follow for your Random object:
private static Random random = new Random((int)DateTime.Now.Ticks);
Hope this entry help you! If you found any mistake or error in this entry, please let me know. I'll try to fix that a.s.a.p. Any solution is highly appreciated! -Share2Learn-

Convert Dataset to JSON String and vice versa using Json.NET

18 comments
Sometimes it's more convenient to use a string rather a dataset. So if you handle a dataset, you must convert it to string. The best way to do that is to convert it to json string.
And then, after you finish everything, you'll need to convert that string back to dataset so that you can save the result into the database or display it.
In this entry I'll show you the easiest way to do that using Json.NET
In this project I use .NET 2.0. If you want to use .NET 3.5 all you have to do is remove reference Newtonsoft.Json.Net20 and add the Newtonsoft.Json.Net35 in bin folder.

Dataset to Json String

StringWriter sw = new StringWriter();
ds.WriteXml(sw, XmlWriteMode.IgnoreSchema);
XmlDocument xd = new XmlDocument();
xd.LoadXml(sw.ToString());

string jsonText = JsonConvert.SerializeXmlNode(xd);
txtResult.Text = jsonText;
First, if you want to convert DataSet to Json String you have to convert DataSet to XMLDocument (Here I use StringWritter).
When you've already added Newtonsoft.Json.Net to you References, you can use JsonConvert to convert between JSON and XML. If you want to convert o JSON String, use SerializeXmlNode. This method takes an XmlNode and serializes it to JSON text.

Javascript and Cookie

No comments
Here is the way to create, retrieve and delete cookies using JavaScript.

Overview of Cookies

Cookie is a small text file used to store information about a user visiting your site. This file is stored on their computer by the browser. In this file you can store user's customized data, login information or something like that.

To view cookies with IE: Run -> shell:cookies
To view cookies with FF: Tools/Options/ On the Privacy Tab, select Show Cookie
...

Set Cookie

Here is the javascript function to create cookie.
function setCookie(name, value, expires) {
 document.cookie = name + "=" + escape(value) 
 + ((expires == null) ? "" : "; expires=" + expires) 
 + "; path=/";
}
name: name of the cookie, you will use this name to get the cookie.
value: data, information that you want to save.
expires: time when cookie will expire (it will be deleted from the user's computer).

Validate email address using Javascript

2 comments
The function below is used to validate email address through javascript.
Hope this entry help you!
If you found any mistake or error in this entry, please let me know. I'll try to fix that a.s.a.p.
Any solution is highly appreciated!

-Share2Learn-