Adding Client-Side Script Blocks with RegisterStartupScript() and RegisterClientScriptBlock()

The System.Web.UI.Page class contains two methods for emitting client-side script code into the HTML rendered by the ASP.NET Web page:

  • RegisterStartupScript(key, script)
  • RegisterClientScriptBlock(key, script)

Both of these methods take two strings as input. The second parameter, script, is the client-side script—including the opening and closing <script> tags—to insert into the page. The first parameter, key, serves as a unique identifier for the inserted client-side script.

The only difference between these two methods is where each one emits the script block. RegisterClientScriptBlock() emits the script block at the beginning of the Web Form (right after the <form runat="server"> tag), while RegisterStartupScript() emits the script block at the end of the Web Form (right before the </form> tag).

From http://msdn.microsoft.com/en-us/library/aa478975.aspx

EPiServer tuyển người – Hà Nội

EPiServer is the world’s fastest growing provider of Web Content Management (WCM) and online social community platforms. Over 3000 customers worldwide use EPiServer CMS to create collaborative, engaging and attractive websites. The platform EPiServer CMS is the foundation of more than 9000 websites and is used on a daily basis by over 130,000 web editors. It enables true web engagement which allows customers to turn passive web visits into dynamic, personalized web experiences that drive new revenues. EPiServer delivers its WCM platform through a network of more than 340 competent partner companies in 25 countries.

Founded in 1994, the company has offices in the USA, Sweden Denmark, Norway, Finland, the Netherlands, Australia, South Africa and the UK.

http://www.episerver.com

Attached to this mail you will find five different profiles,

  • Profile Software Test Automation Engineer
  • Profile Software Tester
  • Profile Web Designer, User Interaction Designer
  • Profile Web User Interface Developer
  • Profile Product Support Developer

As the Development Manager coming to Hanoi on the January the 22 it is important for candidates to apply ASAP.

Send your CV to pelle[dot]niklasson[at]episerver.com

dùng Cache trong ASP.NET

Bài này đề cập tới DataCaching của ASP.NET. Các vấn đề outputCaching (cache cả trang aspx) và fragmentCaching (cache một ascx, một phần của trang Aspx) là khá dễ, chỉ cần setup vài thẻ trong mã aspx là xong nên không được đề cập trong bài.

Xem thêm tại http://aspnet.4guysfromrolla.com/articles/022802-1.aspx

Cache về bản chất giống như một HashTable, lưu key là một String, value là đối tượng .NET. Tuy nhiên Cache thì có tính năng đặc biệt là tự động gỡ bỏ các entry theo thời gian hoặc khi hết bộ nhớ để lưu.

Truy xuất Cache:

dùng đối tượng tĩnh HttpContext.Current.Cache
nếu trong Page aspx, dùng Server.Cache
nếu trong ashx, dùng context.Cache

tất cả đều truy xuất đến cùng một chỗ lưu cache của ASP.NET.

Có hai kiểu chính sách hết hiệu lực bạn có thể sử dụng khi lưu giữ dữ liệu trong cache.

  • Absolute expiration: hết hiệu lực vào một THỜI ĐIỂM xác định trong tương lai (VD: 10 phút nữa là huỷ đổi tượng).

Cache.Insert("cacheKey", ObjectToCache, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero);

  • Sliding expiration: gỡ bỏ các đối tượng sau một khoảng thời gian không dùng đến. Mỗi khi đối tượng được truy xuất, thời gian sống của nó sẽ được reset. (VD: loại bỏ các đối tượng mà đã lâu (10 phút) không dùng tới)

Cache.Insert("cacheKey", ObjectToCache, null,DateTime.MaxValue, TimeSpan.FromMinutes(10));

Mẫu code chung để sử dụng Cache là:

T GetValue(){
string CACHE_KEY = "cacheKey";
object oValue = cache[CACHE_KEY];
if (oValue == null) {
oValue = GetValueFromSomeWhere();
// Lưu giữ đối tượng theo kiểu nào tuỳ bạn chọn
cache.Insert(CACHE_KEY, oValue, null, DateTime.MaxValue, TimeSpan.FromSeconds(60));
}
return oValue as T;
}

Hàm GetValueFromSomeWhere() là chỗ bạn lấy dữ liệu thực ở đâu đó (VD lấy từ Database, đọc từ file …)

.

Một  số bonus:
Hàm Cache.Insert() có đối số cuối cùng là CallBack. Tính năng này khá tiện, cho phép developer kiểm soát xem nên làm gì mỗi khi Cache entry bị gỡ bỏ (có thể kiểm tra, tạo lại mới luôn, hoặc thực hiện một số tác vụ …) .  Tips: Dùng cái này có thể tránh được việc pooling để kiểm tra xem Cache entry hết hạn chưa.

Callback này không được khởi chạy nếu cache entry bị gỡ bỏ có chủ ý (bằng cách gọi Remove(), hay gọi Insert() mới đè lên)

Hàm Cache.Insert() nếu cung cấp key trùng với key đã có rồi sẽ gỡ bỏ entry cũ, ấn entry mới vào với key tương ứng ấy

Sự khác nhau giữa các inline output tag , và trong ASP.NET

Khi viết ASP.NET, nhất là với MVC, các bạn chú ý mấy điểm khác biệt sau để phun (write output) dữ liệu ra đúng ý mình nhé:

  • <%= expressions %> được thực hiện tại thời gian render (pha OnRender) trang ASPX
  • <%# expressions %> được thực hiện khi ta gọi phương thức DataBind(), và thẻ này sẽ không được thực hiện nếu ta không gọi phương thức DataBind()
  • <%# expressions %> có thể được sử dụng trong thuộc tính của các server controls, còn <%= %> không thể làm giá trị trong các thuộc tính cho các server controls

Cập nhật:

Với ASP.NET MVC và ASP.NET 4  trong Visual Studio 2010, có thêm một tính năng mới nữa là <%: expression %> giúp viết code markup ngắn gọn dễ đọc hơn.

<%: ViewData[“Message”] %> tương tự như viết  <%= Html.Encode(ViewData[“Message”]) %>