Asp.net ile ilgili kod örnekleri
Delegates kullanımının farklı yazılım örnekleri.
<script runat=”server”>
void Page_Init()
{
btn.Click += new EventHandler(btn_Click);
}
void btn_Click(object sender, EventArgs e)
{
lblResult.Text = DateTime.Now.ToString();
}
</script>
<script runat=”server”>
void Page_Init()
{
btn.Click += delegate(object sender, EventArgs e)
{
lblResult.Text = DateTime.Now.ToString();
};
}
</script>
<script runat=”server”>
void Page_Init()
{
btn.Click += (sender, e) => lblResult.Text = DateTime.Now.ToString();
}
</script>
Linq ile sayfalama kod örneği
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
public partial class Movie
{
public static IEnumerable<Movie>
{
MyDatabaseDataContext db = n
return db.Movies;
}
public static IEnumerable<Movie>
SelectPaged(
int startRowIndex,
int maximumRows
)
{
return Select().Skip(startRowIndex).Take(maximumRows);
}
public static int SelectCount()
{
return Select().Count();
}
}
Asp.net ön yüz kodu
<asp:GridView
id=”grdMovies”
DataSourceID=”srcMovies”
AllowPaging=”true”
PageSize=”5”
Runat=”server” />
<asp:ObjectDataSource
id=”srcMovies”
TypeName=”Movie”
SelectMethod=”SelectPaged”
SelectCountMethod=”SelectCount”
EnablePaging=”true”
Runat=”server” />
LightWeight Cacheleme
Önyüz kodu
<asp:ObjectDataSource
id=”srcMovies”
TypeName=”Movie”
SelectMethod=”Select”
EnableCaching=”true”
CacheDuration=”9999”
Runat=”server” />
Class Dosyamız
using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
public partial class Movie
{
public static IEnumerable<Movie> Select()
{
MyDatabaseDataContext db = new MyDatabaseDataContext();
return db.Movies;
}
public static IEnumerable<Movie> SelectCached()
{
HttpContext context = HttpContext.Current;
List<Movie> movies = (List<Movie>)context.Cache[“Movies”];
if (movies == null)
{
movies = Select().ToList();
context.Cache[“Movies”] = movies;
context.Trace.Warn(“Retrieving movies from database”);
}
return movies;
}
}
UrlRewriting yaparken gelen sayfanının bulunamamasında yapılacak yönlendirme
void Page_Load()
{
if (String.Compare(Request.Path, Request.RawUrl, true) != 0)
lblMessage.Text = “The URL to this page has changed, “ +
“please update your bookmarks.”;
}
Asp.net ile online ziyaretçiyi gösterme
Global.asax
<%@ Application Language=”C#” %>
<script runat=”server”>
void Application_Start(object sender, EventArgs e)
{
Application[“SessionCount”] = 0;
}
void Session_Start(object sender, EventArgs e)
{
Application.Lock();
int count = (int)Application[“SessionCount”];
Application[“SessionCount”] = count + 1;
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
Application.Lock();
int count = (int)Application[“SessionCount”];
Application[“SessionCount”] = count - 1;
Application.UnLock();
}
</script>
Ön yüz kodu
<script runat=”server”>
void Page_Load()
{
lblSessionCount.Text = Application[“SessionCount”].ToString();
}
</script>
Asp.net Jquery ile webservis sorgularken, webservisinin hata döndürdüğünü anlamak için eklenecek error attribute özelliğinini kullanımı
$(document).ready(function () {
$(“#btnGet”).click(function () {
$.ajax({
type: “POST”,
dataType: “json”,
contentType: “application/json”,
url: “QuotationService.asmx/GetQuote”,
success: function (data) {
$(“#spanQuote”).html(data.d);
},
error: function () {
alert(“The call to the web service failed.”);
}
});
});
Yorumlar
Yorum Gönder