Check/unCheck all comments in blogengine using jquery

by Neon Quach 1. May 2010 18:14

By default there is no select all comments for deleting or approving these comments, in this blog i will show you the way to do this with jquery.

BlogEngine v1.6 don’t use jquery so we must get jquery library form here and reference it to admin’s master page if we are going to use this javascript framework in another admin page, or admin/Comment/DataGrid.ascx user control.

    <script src="<%= ResolveUrl("~/Admin/jquery-1.3.2.js")%>" type="text/javascript"></script>


Now in DataGrid.ascx usercontrol, we will add new html checkbox in the header for selecting all checkboxes by adding either design view or source view. Here is markup:

<HeaderTemplate>

    <input id="chkAll" type="checkbox" />

</HeaderTemplate>

In order up and running jquery i call “document ready” handler:

    $(document).ready(function () {

    });


and fire click event of chkAll checkbox input control
    $("input[type=checkbox]").each(function () {
    });


When the user click on the chkAll input control, i will loop for all checkboxes and check whether chkAll is checked or not, if chkAll is checked then all checkboxes have checked and vice versa, here is the code:

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

    $(document).ready(function () {

        $('#chkAll').click(function () {

            $("input[type=checkbox]").each(function () {

                if (this.checked == true) {

                    $("input[type=checkbox]").attr('checked', 'checked');

                }

                else {

                    $("input[type=checkbox]").attr('checked', false);

                }

            });

        });

    });

</script>


Hope this help!

 

Tags: ,


Categories: blogengine | javascript | jquery

compress javascript code using .net

by Neon Quach 15. April 2010 02:55

Compress your JavaScript file by removing UN use code, or comment, whitespace, trim… to make it load faster in your web page is one of good work.

My last post which shows you how to compress css code to decrease file size
http://code2code.info/post/Efficient-stylesheet-minification-in-Net.aspx

and now, I will show all of you how to apply this technique for JavaScript.

 For the demo purpose, I will create console application using both C# and VB.NET

here is c# function:

/// <summary>
        /// Strips the whitespace from any .js file.
        /// </summary>
        private static string StripWhitespace(string content)
        {
            string[] lines = content.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            StringBuilder emptyLines = new StringBuilder();
            foreach (string line in lines)
            {
                string s = line.Trim();
                if (s.Length > 0 && !s.StartsWith("//"))
                    emptyLines.AppendLine(s.Trim());
            }
            content = emptyLines.ToString();
            // remove C styles comments
            content = Regex.Replace(content, "/\\*.*?\\*/", String.Empty, RegexOptions.Compiled | RegexOptions.Singleline);
            //// trim left
            content = Regex.Replace(content, "^\\s*", String.Empty, RegexOptions.Compiled | RegexOptions.Multiline);
            //// trim right
            content = Regex.Replace(content, "\\s*[\\r\\n]", "\r\n", RegexOptions.Compiled | RegexOptions.ECMAScript);
            // remove whitespace beside of left curly braced
            content = Regex.Replace(content, "\\s*{\\s*", "{", RegexOptions.Compiled | RegexOptions.ECMAScript);
            // remove whitespace beside of coma
            content = Regex.Replace(content, "\\s*,\\s*", ",", RegexOptions.Compiled | RegexOptions.ECMAScript);
            // remove whitespace beside of semicolon
            content = Regex.Replace(content, "\\s*;\\s*", ";", RegexOptions.Compiled | RegexOptions.ECMAScript);
            // remove newline after keywords
            content = Regex.Replace(content, "\\r\\n(?<=\\b(abstract|boolean|break|byte|case|catch|char|class|const|continue|default|delete|do|double|else|extends|false|
final|finally|float|for|function|goto|if|implements|import|in|instanceof|int|interface|long|native|new|null|package|
private|protected|public|return|short|static|super|switch|synchronized|this|throw|throws|transient|true|try|typeof|
var|void|while|with)\\r\\n)"
, " ", RegexOptions.Compiled | RegexOptions.ECMAScript);
            content = Regex.Replace(content, @"[\n\r]+\s*", string.Empty); // space
            return content;
        }

VB.NET

Public Function StripWhitespace(ByVal content As String) As String
        Dim lines() = content.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
        Dim emptyLines As New StringBuilder()
        For Each line As String In lines
            Dim s = line.Trim()
            If s.Length > 0 AndAlso Not s.StartsWith("//") Then
                emptyLines.Append(s.Trim())
            End If
        Next
        content = emptyLines.ToString()
        ' remove C styles comments
        content = Regex.Replace(content, "/\*.*?\*/", [String].Empty, RegexOptions.Compiled Or RegexOptions.Singleline)
        content = Regex.Replace(content, "^\s*", [String].Empty, RegexOptions.Compiled Or RegexOptions.Multiline)
        content = Regex.Replace(content, "\s*[\r\n]", vbCr & vbLf, RegexOptions.Compiled Or RegexOptions.ECMAScript)
        ' remove whitespace beside of left curly braced
        content = Regex.Replace(content, "\s*{\s*", "{", RegexOptions.Compiled Or RegexOptions.ECMAScript)
        ' remove whitespace beside of coma
        content = Regex.Replace(content, "\s*,\s*", ",", RegexOptions.Compiled Or RegexOptions.ECMAScript)
        ' remove whitespace beside of semicolon
        content = Regex.Replace(content, "\s*;\s*", ";", RegexOptions.Compiled Or RegexOptions.ECMAScript)
        ' remove newline after keywords
        content = Regex.Replace(content, "\\r\\n(?<=\\b(abstract|boolean|break|byte|case|catch|char|class|const|continue|default|delete|do|double|else|extends|false|final|finally|
float|for|function|goto|if|implements|import|in|instanceof|int|interface|long|native|new|null|package|private|protected|public|
return|short|static|super|switch|synchronized|this|throw|throws|transient|true|try|typeof|var|void|while|with)\\r\\n)"
, " ", RegexOptions.Compiled Or RegexOptions.ECMAScript)
        content = Regex.Replace(content, "[\n\r]+\s*", String.Empty) ' space
        Return content
    End Function

Then I call this function in the Main

C#
static void Main(string[] args)
{
    string content = File.ReadAllText("../../blog.js");
    File.WriteAllText("../../blog-min.js", StripWhitespace(content));
    Console.Write("Press any key to continue!");
    Console.Read();
}

VB.NET

Sub Main()
        Dim content As String = File.ReadAllText("../../blog.js")
        File.WriteAllText("../../blog-min.js", StripWhitespace(content))
        Console.WriteLine("Press any key to continue!`")
        Console.Read()
End Sub


Done, now when you run this code, it will create a new file with compress content.

Tags: ,


Categories: javascript | c# | vb.net

jQuery 1.4 Released

by Neon Quach 14. January 2010 17:51

jQuery 1.4 Released

In celebration of jQuery’s 4th birthday, the jQuery team is pleased to release the latest major release of the jQuery JavaScript library! A lot of coding, testing, and documenting has gone into this release, and we’re really quite proud of it.

I want to personally thank Brandon Aaron, Ben Alman, Louis-Rémi Babe, Ariel Flesler, Paul Irish, Robert Katić, Yehuda Katz, Dave Methvin, Justin Meyer, Karl Swedberg, and Aaron Quint who put a lot of work into fixing bugs and getting the release out the door.

http://jquery14.com/day-01/jquery-14

Tags:


Categories: javascript | jquery

Tự động refresh trang aspx sử dụng javascript

by Neon Quach 26. May 2009 20:04



Chú ý: 3000 là thời gian cho mổi lần refresh (mili giây). Như vậy với đoạn code này thì cứ sau 3 giây trang aspx sẻ tự động refresh 1 lần.

Happy coding!

Tags:


Categories: javascript

Powered by BlogEngine.NET 1.6.0.0 - Eco Theme by n3o Web Designers