2013-01-04 5 views
1

ASP.Net 페이지를 처음 만들었습니다. GridView 개체가있는 기본 asp.net 페이지가 있고 조건을 기반으로 행 색을 변경하려면 RowDataBound 이벤트를 작성했습니다. 내 함수/이벤트를 acutal GridView 객체에 연결하는 방법에 대한 도움이 필요합니다. 함수/이벤트를 클라이언트 측 또는 서버 측에 배치해야합니까?GridView 개체에 GridView 함수를 연결하는 방법

ps. Visual Studio 2010을 사용하여 도구 모음 옵션을 사용하여 개체에 기능을 추가하는 방법이 있다면 멋지 겠지요.

답변

0

RowDataBound 이벤트는 서버 측에서 처리됩니다. "코드 숨김"파일에 이벤트 코드/논리가 있거나 HTML 파일에 인라인 스크립트를 추가 할 수 있습니다.

<%@ Page Language="C#" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<script runat="server"> 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     // logic here 
    } 
</script> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
    <title>GridView RowDataBound Event</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <h2 style="color:Navy">GridView OnRowDataBound</h2> 
     <asp:SqlDataSource 
      ID="SqlDataSource1" 
      runat="server" 
      ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
      SelectCommand="SELECT ProductID, ProductName, UnitPrice FROM Products" 
      > 
     </asp:SqlDataSource> 
     <asp:GridView 
      ID="GridView1" 
      runat="server" 
      DataSourceID="SqlDataSource1" 
      ForeColor="AliceBlue" 
      BackColor="DarkSalmon" 
      BorderColor="Salmon" 
      HeaderStyle-BackColor="Crimson" 
      AllowPaging="true" 
      AutoGenerateColumns="true" 
      DataKeyNames="ProductID" 
      OnRowDataBound="GridView1_RowDataBound" 
      > 
     </asp:GridView>   
    </div> 
    </form> 
</body> 

OnRowDataBound 속성은 even 메서드와 동일한 이름이어야합니다. 예를 들어 OnRowDataBound="GridView1_RowDataBound"의 이름은 이벤트 처리기 서명 GridView1_RowDataBound(object sender, GridViewRowEventArgs e)과 같습니다.

자세한 내용은 here을 참조하십시오.

+0

감사합니다. 또한 나는 서버 측에서 다른 모든 C# 파일이있는 디렉토리에 "App_Code"라는 폴더가 있다고 언급하고 싶습니다. 또한 새 C# 파일을 만들고 행 이벤트/함수를 배치하고 default.aspx에 GridView를 링크 할 수 있습니까? – charlie

+0

네가 그렇게 할 수 있다고 믿는다. .aspx 파일 내에서 src 경로를 업데이트하려고합니다. <% @ Page .. src = "CodeBehind.cs"%> –