본문 바로가기

.NET/ASP.NET

ASP.NET의 10가지 유용한 프로퍼트들(6가지만 언급 4가지는 링크를 통해 확인 바람)

http://www.codeproject.com/KB/aspnet/usefulpropertes.aspx

Introduction

개발자들은 매번 컨트롤들과 씨름을한다. 여기서는 10가지 유용한 프로퍼티 팁들을 소개한다.

1、ClientIDMode

서버 컨트롤이 랜더링하는 동안 컨트롤들의 아이디는 자동으로 생성됩니다. 자동으로 생성된 아이디 때문에 스크립트를 작성할때 개발자들은 엘러먼트  접근에 많은 어려움을 겪게 되었는데 이러한 아이디 생성을 4가지의 모드를 노출하여 개발하기 편하게 생성할수 있습니다.  4가지 모드는 아래와 같습니다. 일괄적인 셋팅은 아래와 같이  web.config에 설정해 주면 됩니다.

  • AutoID: 기존에 자동생성되는 것같 아이디 생성규칙이 똑같음
  • Static: 개발자가 입력한 아이디로 생성됨
  • Predictable: 아이디 attribute와 접미사가 붙어 생성됩니다.
  • Inherit: 부모컨터롤의 셋팅을 상속받음

Note: 디폴트 모드는 첫번째인 AutoID입니다.

  <pages clientIDMode="Predictable"></pages> </system.web>
		

2、Meta Keywords and Meta Description

4.0에는 페이지 클래스에 2가지 새로운 프로퍼티 Meta Keywords와 Meta Description이 추가 되었습니다. 아래와 같이 설정하여 어떠한 페이지 프로세스에 동적으로 삽입하여 의도하는 작업을 할수 있습니다.

<%@ Page Language=

  "C#" AutoEventWireup=
  "true" Keywords=
  "keyword1, keyword2" Description=
  "my description" %>"C#"
  AutoEventWireup=
  "true"
  Keywords=
  "keyword1, keyword2"
  Description=
  "my description"
  %>"C#"
  AutoEventWireup=
  "true"
  Keywords=
  "keyword1, keyword2"
  Description=
  "my description"
  %>

3、Rows in Data-bound Controls Persistent Selection

그리드뷰와같은 데이터바운드 컨트롤들은 Row 선택을 지원합니다. 그러나 지속적인선택은 지원하지 않습니다. 4.0에서는 지속적인 선택이 가능합니다. 아래와 같이 데이터바운드 컨트롤러에 EnablePersiste 프로퍼티를 사용하면 됩니다.

<asp:ListView runat=
  "server" EnablePersistedSelection=
  "True" DataSourceID=
  "dsRanks" DataKeyNames=
  "rankid" >"server"
  EnablePersistedSelection=
  "True"
  DataSourceID=
  "dsRanks"
  DataKeyNames=
  "rankid"
  >"server"
  EnablePersistedSelection=
  "True"
  DataSourceID=
  "dsRanks"
  DataKeyNames=
  "rankid"
  > <ItemTemplate>…
  </ItemTemplate><SelectedItemTemplate> … </SelectedItemTemplate> </asp:ListView>

4、AutoEventWireup

아래와 같이 AutoEventWireup을 true로 주면 page이벤트가 호출됩니다.

  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" …. %>

5、Page Header property

페이지 클래스에 헤더 프로퍼티를 쓰면 동적으로 아래와 같이 값이나 레퍼런스를 삽입하는게 가능합니다.

 this.Header.Title = "My page title";
HtmlLink printLink = new HtmlLink ();
  printLink.Attributes.Add ("type", "text/css");
  printLink.Attributes.Add ( "rel" , "stylesheet" );
  printLink.Attributes.Add ("href", "css/print.css");
  this.Header.Controls.Add (printLink);

6、AssociatedControlID

컨터롤간의 관계설정이 가능합니다. 아래와 예제와 같이 AssociatedControlID에 관계를 맺을 ID를 설정해주면 됩니다. 레이블을 클릭하면 포커스가 해당 텍스트박스에 있음을 확인할수 있다.

  <asp:Label ID="Label1" AssociatedControlID="txtUserName" runat="server" Text="User name:" />
	<asp:TextBox ID="txtUserName" runat="server" />
	<asp:TextBox ID="Tdfdf" runat="server"></asp:TextBox>