Button Text! Submit original article and get paid. Find out More

Thứ Ba, 1 tháng 4, 2014

Việt hoá ASP.NET Forum của Microsoft

Unknown  |  at  00:02  |   |  No comments

1. Giới thiệu

ASP.NET Forum là một diễn đàn (forum) hoàn toàn miễn phí được viết trên ngôn ngữ lập trình C# (C sharp) và kỹ thuật thiết kế web ASP.NET. Do khả năng hiểu biết giới hạn về các diễn đàn hiện có trên mạng, tôi không chắc là diễn đàn này nhanh và có nhiều tính năng như một số các diễn đàn khác, nhưng tôi thấy diễn đàn này có các đặc tính ưu việt sau đâỵ

ạ Diễn đàn hoàn toàn sử dụng kỹ thuật object-oriented (do viết trên C#) nên khả năng mở rộng lập trình trong tương lai (expandability) hầu như không giới hạn. C# có sẵn các thư viện lập trình (library) chứa các hàm số thông dụng như mở đóng một tập tin trên server, nối với databases, hàm xử lý chuỗi ký tự (strings), vân vân ...

b. Sử dụng lối viết mã nguồn riêng biệt từ html tags, nên đọc và hiểu mã tương đối dễ hơn các diễn đàn khác. Ngoài ra, người thiết kế mạng có thể làm việc riêng biệt và không lệ thuộc vào lập trình viên. Ta sẽ thấy rõ hơn khi xem cách cài đặt Việt ngữ vào thiết kế web ASP.NET Forum ở các phần dướị

c. Mã được chuyển sang bytecode và được truy cập theo object, chứ không dùng phương pháp diễn dịch (interpreting) nên chắc chắn là nhanh hơn so với diễn dịch.

d. (Mong các bạn đóng góp thêm ... :-) )

ẹ Lập trình với c# tự thân đã thích thú hơn so với các ngôn ngữ khác như C, C++, perl, Visual Basic. (phần này objective, VB programmer có thể nói C# is a pain in the ... to code with.)

Diễn đàn ASP.NET do Microsoft bảo trợ và có 2 lập trình viên làm việc full-time, nên source code nhìn rất sáng sủa trật tự, không chắp vá và thời vụ.

2. Các Ứng dụng phải có trước khi tạo ASP.NET Forum.

Người viết bài này không có ý định mở một forum website, nên các chỉ dẫn dưới đây đều được làm ở máy cá nhân tại nhà, chứ không setup từ xa (remotelỵ) Để setup forum bạn cần có:

- Windows 2000 hay XP hay NT4 (SP5+),
- IIS installed,
- SQL Server or MSDE installed,
- AspNetForums install file lấy từ http://asp.net/ (600+ KB)
- Visual C#.NET
- unicode zip file của anh Trần Anh Tuấn http://www.avys.de/

3. Các bước thực hiện

ạ install asp.net forums theo cách chỉ dẫn của Microsoft, nhớ phải dùng Version 1 (nếu không database chứa datatype saị)

b. Dưới directory C:\AspNetForums, bạn sẽ có vài directories, nhưng 2 cái quan trọng nhất là AspNetForums dùng để chứa ASP.NET files (user interface) và Engine directory dùng để chứa tất cả source codẹ Ta sẽ phải edit một số files cuả cả hai directories nàỵ

c. Unzip tất cả các files của unicodẹzip sao cho có directory c:\AspNetForums\AspNetForums\js\ , directory này dùng để chứa các javascript files anh Tuấn đã viết. (Cám ơn anh nhiềụ) Trong directory cũng có 1 file mẫu tên unicode.html, mở file này dùng text editor của bạn. Mình sẽ cut-n-paste vài đọan từ nó.

d. Mở file: C:\AspNetForums\AspNetForums\EditPost.aspx, xong cut&paste 1 đoạn từ unicode.html vào phần <HEAD> tag:


 ...

<HTML>
  <HEAD>
    <AspNetForums:StyleSkin runat="server" ID="Styleskin1" />
    <META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
    
                   
    
    <!-- Example 3:         
    *
    Nạp bộ gõ, giao diện với các tính năng mở rộng, các bảng mã.
    Trước khi submit form tự động nhận dạng mã 
    và chuyển văn bản thành unicode rồi mới gửi đến server
    
    Scripts cần tới: vietuni8.js, vumodẹjs, vumaps.js
    *
    -->                                                                                   
    
    <!--
    !!! Note: use full URI for web apps, ẹg.: src="http://www.mysite.net/js/vietuni8.js"
    -->
    <script language="JavaScript1.2" src="js/vietuni8.js" type='text/javascript'></script>
    <script language="JavaScript1.2" src="js/vumods.js" type='text/javascript'></script>
<script language="JavaScript1.2" src="js/vumaps.js" type='text/javascript'></script>
  </HEAD>
  <body leftmargin="0" bottommargin="0" rightmargin="0" topmargin="0" marginheight="0" marginwidth="0">
    <form runat="server">

   ....


ẹ Mở file: C:\AspNetForums\AspNetForums\AđPost.aspx và làm tương tự như phần d. Mục đích của chúng ta là thông báo cho browser chỗ để tìm kiếm các external javascript files và dồng thời dùng unicode charset (charset=iso-8859-1.) Hai files EditPost & AđPost.aspx là hai files mà forum sẽ gọi mỗi khi người dùng muốn viết bài mớị (Hình như phần edit bài không cho phép đối với người dùng thường, chỉ có admin mới có quyền edit post.)

f. Mở file C:\AspNetForums\AspNetForums\skins\default\Skins\Skin-Post.ascx để sửạ File này là một user control và cũng là một default file for skinning. Skinning (da) có nghĩa là mình (admin) có khả năng chế ra các loại màu mè thêm thắt, nhưng sau lưng thì vẫn dùng các controls như nhau tuy khác skin. Đây là toàn bộ file sau khi sửa, các đoạn tô đỏ là các chỗ sửa:


<%@ Register TagPrefix="AspNetForums" Namespace="AspNetForums.Controls" Assembly="AspNetForums" %>
<%@ Control %>
<table cellSpacing="0" border="0">
  <tr>
    <td><ASPNETFORUMS:WHEREAMI id="Whereami1" runat="server"></ASPNETFORUMS:WHEREAMI></td>
  </tr>
</table>
<p>
  <table class="tableBorder" cellSpacing="1" cellPađing="3" width="100%">
    <tr>
      <th class="tableHeaderText" align="left" height="25">
         <asp:label id="PostTitle" runat="server"></asp:label>
      </th>
    </tr>
    <span id="ReplyTo" runat="server" visible="false">
      <tr>
        <td class="forumRow">
          <table cellSpacing="1" cellPađing="3">
            <tr>
              <td colSpan="2"><span class="normalTextSmall">The message you are replying to: </span></td>
            </tr>
            <tr>
              <td vAlign="top" noWrap align="right"><span class="normalTextSmallBold">Posted By: </span></td>
              <td vAlign="top" align="left"><asp:hyperlink id="ReplyPostedBy" Runat="server" CssClass="normalTextSmall"></asp:hyperlink><asp:label id="ReplyPostedByDate" Runat="server" CssClass="normalTextSmall"></asp:label></td>
            </tr>
            <tr>
              <td vAlign="top" align="right"><span class="normalTextSmallBold">Subject: </span></td>
              <td vAlign="top" align="left"><asp:hyperlink id="ReplySubject" runat="server" CssClass="normalTextSmall"></asp:hyperlink></td>
            </tr>
            <tr>
              <td vAlign="top" align="right"><span class="normalTextSmallBold">Message: </span></td>
              <td vAlign="top" align="left"><span class="normalTextSmall"><asp:label id="ReplyBody" runat="server"></asp:label>
                </span></td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td class="forumAlternate"> 
        </td>
      </tr>
    </span><span id="Preview" runat="server" visible="false">
      <tr>
        <td class="forumRow">
          <table cellSpacing="1" cellPađing="3" width="75%" border="0">
            <tr>
              <td vAlign="top" align="left"><span class="normalTextSmallBold"><asp:label id="PreviewSubject" runat="server"></asp:label>
                </span><br>
                <span class="normalTextSmall">
                  <asp:label id="PreviewBody" runat="server"></asp:label>
                </span></td>
            </tr>
            <tr>
              <td> 
              </td>
            </tr>
            <tr>
              <td vAlign="top" align="left"><asp:button id="BackButton" Runat="server" Text=" < Back to Edit Mode "></asp:button> 
                <asp:button id="PreviewPostButton" Runat="server" Text=" Post "></asp:button></td>
            </tr>
            <tr>
              <td> 
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </span><span id="Post" runat="server" visible="true">
      <tr>
        <td class="forumRow">
          <table cellSpacing="1" cellPađing="3">
            <tr>
              <td vAlign="top" nowrap align="right"><span class="normalTextSmallBold">Author: </span></td>
              <td vAlign="top" align="left" colSpan="2"><span class="normalTextSmall"><asp:label id="PostAuthor" runat="server"></asp:label>
                </span></td>
            </tr>
            <span id="Edit" runat="server" visible="false">
              <tr>
                <td vAlign="top" nowrap align="right"><span class="normalTextSmallBold">Editing Post As: </span></td>
                <td vAlign="top" align="left" colSpan="2"><span class="normalTextSmall"><asp:label id="PostEditor" runat="server"></asp:label>
                  </span></td>
              </tr>
            </span>
            <tr>
              <td nowrap valign="center" align="right"><span class="normalTextSmallBold">Subject: </span></td>
              <td vAlign="top" align="left"><textarea id="PostSubject" runat="server" columns="55" rows="1" onkeyup="telexingVietUC(this,event);" ></textarea></td>

              <td><asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" CssClass="validationWarningSmall" ErrorMessage="RequiredFieldValidator" ControlToValidate="PostSubject">Subject required.</asp:requiredfieldvalidator></td>
            </tr>
            <tr>
              <td vAlign="top" nowrap align="right"><span class="normalTextSmallBold">Message: </span></td>
              <td vAlign="top" align="left"><textarea id="PostBody" cols="80" TextMode="MultiLine" rows="15" onkeyup="telexingVietUC(this,event);" runat="server"></textarea></td>
              <td vAlign="top"><asp:requiredfieldvalidator id="RequiredFieldValidator2" runat="server" CssClass="validationWarningSmall" ErrorMessage="RequiredFieldValidator" ControlToValidate="PostBody" EnableClientScript="False">You must supply a messagẹ</asp:requiredfieldvalidator></td>
            </tr>
            <span id="EditNotes" runat="server" visible="false">
              <tr>
                <td vAlign="top" nowrap align="right"><span class="normalTextSmallBold">Edit Notes: </span></td>
                <td vAlign="top" align="left" colSpan="2"><textarea id="EditNotesBody"  cols="80" TextMode="MultiLine" rows="15" onkeyup="telexingVietUC(this,event);" runat="server"></textarea></td>

              </tr>
            </span>
 <br>
 <input type=radio name="switcher" value="OFF" checked onfocus="setTypingMode(0)">Off</input>
 <input type=radio name="switcher" value="TELEX" onfocus="setTypingMode(1)">Telex</input>
 <input type=radio name="switcher" value="vnVni" onfocus="setTypingMode(2)">VNI</input>
 <input type=radio name="switcher" value="vnViqr" onfocus="setTypingMode(3)">VIQR</input>
 <br>

            <span id="AllowPinnedPosts" runat="server" visible="false">
              <tr>
                <td vAlign="center" align="right" width="91"><span class="normalTextSmallBold">Pinned Post: 
          </span></td>
                <td vAlign="top" align="left"><span class="normalTextSmall"><asp:dropdownlist id="PinnedPost" runat="server"></asp:dropdownlist>
                  </span></td>
              </tr>
            </span>
            <tr>
              <td vAlign="center" align="right" width="93"><span class="normalTextSmallBold"> </span></td>
              <td vAlign="top" align="left"><span class="normalTextSmall"><asp:checkbox id="AllowReplies" runat="server" Text=" Do not allow replies to this post."></asp:checkbox>
                </span></td>
            </tr>
            <tr>
              <td vAlign="top" align="right" colSpan="2"><asp:button CausesValidation="false" id="Cancel" Runat="server" Text=" Cancel "></asp:button> 
                <asp:button id="PreviewButton" Runat="server" Text=" Preview > "></asp:button></td>
            </tr>
            <tr>
              <td vAlign="top" align="right" colSpan="2">
                <asp:button id="PostButton" Runat="server" Text=" Post "></asp:button></td>
            </tr>
          </table>
        </td>
      </tr>
    </span>
  </table>
</p>
<p>
<table cellSpacing="0" border="0">
  <tr>
    <td><ASPNETFORUMS:WHEREAMI ShowHome="true" id="Whereami2" runat="server"></ASPNETFORUMS:WHEREAMI></td>
  </tr>
</table>



Basically, mình thế 1 label (PostSubject) và hai TextBoxes (PostBody & EditNotesBody) thành 3 textarea server-side HtmlControls. Chuyển như vậy làm html server controls có thể lấy hoặc gán giá trị trên server (dùng InnerText property), cùng lúc đó mình vẫn có thể gán javascript on client-side onkeyup="telexingVietUC(this,event);" để máy có thể chạy javascipt function mỗi khi mình bỏ dấụ

g. Mở AspNetForums.sln (solution) dưới C:\AspNetForums\Engine, sau đó đổi qua release build. Rebuild the solution, hy vọng mọi sự tốt đẹp và Visual Studio cùng lúc update AspNetForums.dll dưới directory C:\AspNetForums\AspNetForums\bin

h. Vẫn trong VC#, mở file CreateEditPost.cs nằm dưới control folder. Ráng tìm và thay các đoạn sau đây:

- thêm using vào đầu file: using System.Web.UỊHtmlControls;

- Sửa method DisplayEdit() thành:


        private void DisplayEdit(Control controlTemplate) {
            PostDetails post = null;
            Label label;
            HtmlTextArea textbox;
            HtmlTextArea textbox2;
            Button button;

            if (Mode != CreateEditPostModẹEditPost)
                return;

            // Get the post to edit
            try {
                post = GetPostForEdit();
            } catch (CannotEditPostException) {
                HttpContext.Current.ResponsẹRedirect(Globals.UrlMessage + Convert.ToInt32(Messages.UnableToEditPost));
                HttpContext.Current.ResponsẹEnd();
            }

            // Set the visibility
            ((Control) controlTemplatẹFindControl("Edit")).Visible = true;
            ((Control) controlTemplatẹFindControl("EditNotes")).Visible = true;

            // Set the title
            ((Label) controlTemplatẹFindControl("PostTitle")).Text = Globals.HtmlDecode(EditMessageText);

            // Set the editor of the post
            ((Label) controlTemplatẹFindControl("PostEditor")).Text = user.Username;

            // Set the Username
            label = (Label) controlTemplatẹFindControl("PostAuthor");
            label.Text = post.Username;
 
            // Set the Subject
            textbox = (HtmlTextArea) controlTemplatẹFindControl("PostSubject");
            textbox.InnerText = post.Subject;

            // Set the Body
            textbox2 = (HtmlTextArea) controlTemplatẹFindControl("PostBody");
            textbox2.InnerText = post.Body;
 
            // Find the checkbox
            allowNoReplies = (CheckBox) controlTemplatẹFindControl("AllowReplies");

            // Wireup the preview button
            button = (Button) controlTemplatẹFindControl("PreviewButton");
            button.Click += new System.EventHandler(PreviewButton_Click);

            // Wire up the cancel button
            button = (Button) controlTemplatẹFindControl("Cancel");
            button.Click += new System.EventHandler(CancelButton_Click);

            // Wire up the post button
            button = (Button) controlTemplatẹFindControl("PostButton");
            button.Click += new System.EventHandler(PostButton_Click);
        }


Sửa PreviewButton_Click() thành


       private void PreviewButton_Click(Object sender, EventArgs e) {
            Control form;
            Label label;
            HtmlTextArea textbox;
            HtmlTextArea textbox2;
            Button button;

            // only do this stuff if the page is valid
            if (!PagẹIsValid) 
                return;
   
            // The event was raised by a button in the user control
            // the is the UI for the form -- get the Parent, ẹg. the User Control
            form = ((Control)sender).Parent;

            // Find and enable the Preview display
            form.FindControl("Preview").Visible = true;

            // Find and hide the ReplyTo display and Post
            form.FindControl("ReplyTo").Visible = false;
            form.FindControl("Post").Visible = false;

            // Set the title text
            ((Label) form.FindControl("PostTitle")).Text = "Preview Message";

            // Preview the post subject
            label = (Label) form.FindControl("PreviewSubject");
            textbox = (HtmlTextArea) form.FindControl("PostSubject");
            label.Text = Globals.HtmlEncode(textbox.InnerText);

            // Preview the post body
            label = (Label) form.FindControl("PreviewBody");
            textbox2 = (HtmlTextArea) form.FindControl("PostBody");

            // Are we in edit modẻ
            /*
            if (Mode == CreateEditPostModẹEditPost) {
                string editNotes = CreateEditNotes(form);
                label.Text = Globals.FormatPostBody(editNotes + textbox2.InnerText) + Globals.FormatSignature(user.Signature);
            } else {
            }
            */
            label.Text = Globals.FormatPostBody(textbox2.InnerText) + Globals.FormatSignature(user.Signature);
[/red]
        }


-sửa CreateEditNotes() thành:


        private string CreateEditNotes(Control form) {
            string editNotes;
            HtmlTextArea textbox;

            // Get the edit notes that the editor submitted
            textbox = (HtmlTextArea) form.FindControl("EditNotesBody");

            // Create the edit notes string
            editNotes = "[Edit by=\"" + user.Username + "\"]" + textbox.InnerText + "[/Edit]\n";

            return editNotes;
        }


-sửa PostButton_Click () thành:


       private void PostButton_Click (Object sender, EventArgs e) {
            Control form;
            Post postRepliedTo;

            // Only proceed if the post is valid
            if (!PagẹIsValid) 
                return;
   
            // Get the user control that the click originated from
            form = ((Control)sender).Parent;

            // When we ađ a new post, we want to get back the NewPostID, so that we
            // can automagically redirect the user to the page showing the post.
            // If iNewPostID comes back as 0, though, that means that the post needs
            // to be approved first, so the user is taken to a page explaining this.
            Post newPost = null; 
            Post postToAđ = new Post();   
   
            postToAđ.Username = Context.User.IdentitỵName;
            postToAđ.ForumID = postToAđ.ParentID = 0;
            postToAđ.Subject = ((HtmlTextArea) form.FindControl("PostSubject")).InnerText;
            postToAđ.Body = ((HtmlTextArea) form.FindControl("PostBody")).InnerText;[/red]
            postToAđ.IsLocked = allowNoReplies.Checked;

    .....
        }


Sau đó thì recompile & hopefully mọi việc sẽ trôi chảy và bạn sẽ có thể viết được tiếng Việt.

4. Kết luận 

Đây mới chỉ là bước đầu trong việc Việt hoá ASP.NET Forum, có nhiều việc khác có thể làm như làm một pulldown ngay trong header của forum để nhớ cách input chữ Việt mà forum member sử dụng, hoặc upgrade lên MViệt của nvcv, hay thêm vào các tính năng đa dạng như đã thấy trong site www.thanhda.com.

Người viết bài này hy vọng các bạn yêu thích ASP.NET & C# sẽ thử và làm giàu thêm trên viên gạch lót đường mà người viết đã mày mò tìm ra, mong lắm thaỵ

About the Author

Nulla sagittis convallis arcu. Sed sed nunc. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.

0 nhận xét:

    Được tạo bởi Blogger.

General

Blogger Template. Powered by Blogger.