Category Archives: ASP.NET 2.0

Microsoft Chart Controls For ASP.NET

To begin with, you need to download MSChart.exe, and you will also find life a lot easier if you install the Visual Studio Add on that adds the charting control to the toolbox and offers Intellisense support for the components too, and also download the .chm documentation. There are also a load of samples showing the charts being used against a variety of data ssources, such as xml, csv, Excel and a typed dataset. These can be downloaded from http://code.msdn.microsoft.com/mschart.

Once you have installed the .exe and the VS Add on, you will see a chart icon in the Data section of the Toolbox in Visual Studio, but if you are using Visual Web Developer Express, you will need to add it manually. This can be done by right-clicking on the Data section header, and choosing Add Items.

From there, select the System.Web.UI.DataVisualization.Charting flavour of Chart, and click OK.

That’s it. The Chart icon should appear in your toolbox, ready to be dragged onto the Forms Designer. Once you do, you are presented with a default vertical bar chart.

To change the chart type, you need to find the Series option in the Properties panel for the Chart. If you click next to (Collection) a button with ellipses will show, and clicking on this will reveal the Series Collection Editor. In the Chart section, you will find Chart Type, and a drop down offering you a selection to choose from. I chose Pie.

There are a huge number of options available to format the chart. These are found in the Chart section of the Properties, and are generally members of collections. ChartArea options include the ability to set a 3D appearance, inclination etc of the actual chart; Legends and Title allow you finer control over these areas, and Series holds one of the most important options (in the Data Source section): XValueMember and YValueMembers. These must be set to bind data to the chart, and to prevent a blank image being rendered.

I drag a SqlDataSource onto the designer, and configure it to connect to an old copy of this site’s database, and apply the following SQL to it:

SELECT Categories.Category, COUNT(ArticleCategories.CategoryID) AS TotalInCategory

FROM ArticleCategories

INNER JOIN Categories ON ArticleCategories.CategoryID = Categories.CategoryID

GROUP BY Categories.Category

This produces a resultset that shows how many articles there were in each category on my site during stage 2 of its evolution. Having done that, click the smart tag on the chart, and choose the datasource. At this point you can normally run the page if this was a GridView or similar databound control, but if you do, the result would be a totally blank page. Now you need to go back to the Properties panel, and find the Series section and configure the XValueMember and the YValueMembers as mentioned previously. Fortunately, the two values that are being returned are available as dropdown options.

I select Category for the X value, and TotalInCategory for the Y values, and run the page:

The result is far from satisfactory, so I fiddle with some more of the formatting properties and the size and get something much nicer:

The charts are rendered as images. The default is to use an HttpHandler to stream the image to the browser as a png file – all managed by the Chart library. However, there are options to change the image type to jpg, bmp or emf files and to save them to the filesystem and use a straightforward <img> tag to show them. The documentation is quite comprehensive in its coverage of the library classes etc, but is very disappointing in its lack of walkthroughs or easier explanations or notes as to what each property does, and what effect changing it might have on the resulting graph. However, there is already a forum dedicated to the chart control library at MSDN forums, and no doubt most beginner questions will be answered there as it grows.

Gui Email voi noi dung doc tu file .htm

Nội dung:
Trong bài giới thiệu này mình sẻ cố gắng giải thích cô động và xúc tích, đọc xong bài này các bạn sẻ có thể làm được:

1. Tạo 1 email template mẫu có sẳn.
2. Cấu hình smtp.
3. Lấy địa chỉ email từ file web.config.
4. Bật chức năng bảo mật cho email gửi đi. (enable SSL)

Nếu các bạn muốn thì hiểu rỏ hơn về Mail cũng như các lớp cung cấp phục vụ cho việc gửi mail, xin vui lòng tham khảo thêm MCTS Self-Paced Training Kit (Exam 70-536): Microsoft .NET Framework 2.0 Application Development Foundation, chương 15 (cơ bản về .NET 2.0).

Cách làm:

Bước 1:
Dùng Visual Studio 2005/2008 tạo 1 ứng dụng Web, chọn ngôn ngữ mà bạn thích, ở đây mình code trên 2 ngôn ngữ VB và C#, đầu tiên mình sẻ tạo 1 template mẫu phục vụ cho việc gửi mail.

Bước 2:
Và mail template có dạng sau:

Và đây là code html của mail mẫu

Giờ khoang hả quan tâm đến các từ trong dấu [], chẳng hạn [Sender], nó chính là các tham số mà ta sẻ thay thế.

Bước 3:
Tiếp đến tạo 1 trang để cho người dùng gửi yêu cầu, nơi mà chúng ta sẻ áp dụng chức năng send mail:

Các này dể thiết kế nên mình không có đi sâu vào vấn đề làm thế nào để layout nó, ở đây mình dùng table.

Bước 4:
Giờ đến phần cấu hình smtp trong web.config, mục đích cho phép người dùng dể dàng chỉnh sửa email gửi đi 1 cách dể dàng mà không cần đụng đến mã nguồn.


Ở đây do mình dùng host của godaddy nên cấu hình smtp yêu cầu là như hình trên, nếu smtp của các bạn là gmail hoặc yahoo thì phải cấu hình cho đúng, ví dụ: của gmail là stmp.gmail.com, port là 587. Ở đây mình cấu hình smtp trên domain của mình đang sử dụng (http://www.vi-vn.com).

Chú ý: tag smtp phải đặt trong tag configuration và được bộc lại bởi tag system.net

Típ: Các bạn nên bỏ nó phía dưới cùng trong tag configuration

Bước 5: tạo 1 thuộc tính để lấy giá trị email trong web.config

Chú ý: thuộc tính này chỉ đọc thôi, không cho phép ghi xuống cho nên chỉ có get C#, và ReaOnly trong VB.

Chú ý: các bạn phải using hoặc import 2 namespace vào là:

C#
using System.Net.Configuration;
using System.Configuration;

VB
Imports System.Configuration
Imports System.Net.Configuration

Với thuộc tính này chúng ta sẻ lấy được địa chi email: admin@vi-vn.com

Bước 6: Viết hàm send mail

Hàm này sẻ trả về kiểu String, dùng để nhận biết kết quả của việc send mail (thành công hay thất bại), bạn cũng có thể trả về kiểu bool nếu bạn thích.

C#
public string SendMail(string subject, string body,
string to, bool isHtml, bool isSSL)

VB
Public Function SendMail(ByVal subject As String, ByVal body As String, _
ByVal toAddress As String, ByVal isHtml As Boolean, ByVal isSSL As Boolean) _
As String
C#

VB.NET

Phương thức send mail này sẻ nhận vào các tham số như: chủ đề, nội dụng, người nhận, gửi mail dạng html, mã hoá SSL (Secure Socket Layer).

Đầu tiên hàm này sẻ tạo 1 đối tượng MailMessage, và được bộc lại bởi block using, để đảm bảo rằng đối tượng này sẻ được Dispose khi ra khỏi hàm và trước khi Collect Garbage thực hiện việc này. Như những dòng lệnh của đả nói lên rằng là 1 mail bao gồm người gửi (From), người nhận (To), chủ đề (Subject), nội dụng (Body)…

Bước 7: Gọi hàm SendMail.

C#

VB.NET

Đầu tiên chúng ta tạo 1 đối tượng StreamReader và đọc nội dụng của file html tempate, sau đó đọc tất cả nội dụng của file template vào biến content. Sau đó thay thế các tag [Sender], [Content]…. bằng những gì mà người dùng nhập vào.


Kết quả:

Kết luân:

Đây là cách làm thông thường khi lằm việc với mail, ngoài ra khi send mail, chúng ta cần phải nhúng vào đó hình hoặc tập tin đính kèm chắng hạn. Điều này cũng rất dể dàng vì ASP.NET 2.0 đả hổ trợ chúng ta. Tương tự với cách làm này thì khi người dùng đăng ký thành viên thì rất dể dàng cho việc send mail active tài khoản, cũng dùng cách này trong các bài viết sau mình sẻ hướng dẩn send mail active tài khoản khi user đăng ký thành viên.

Mã nguồn:

http://www.vi-vn.com/pubs/DynamicEmail.rar


Hope this help,
QuachNguyen

Follow

Get every new post delivered to your Inbox.