sub_title
 ASP.NET
제   목 데이터 그리드 안에 있는 컨트롤 이벤트 만들기
작성자 깐소금 등록일 2007-11-18 04:49:13 조회수 5,403

예전에 이런 질문을 하시던 분이 계셨는데...

데이터 그리드 안에 있는 어떤 컨트롤에 대해서 그 컨트롤의 이벤트만을 실행시키고 싶다고

하더군요. 그때는 저두 잘 몰랐는데... 오늘에서야 알게 되었습니다.

코드 프로젝트라는 외국 사이트에서 찾게 되었는데... 올려드리겠습니다.

영어 해석하기 싫으신 분은 그냥 코드만 보세요.. 그러면 이해하기 쉬울거예요!

출처 : 코드프로젝트

Handling events of child controls inside a DataGrid

 

Introduction

One of the big DataGrid mysteries is (or was, at least for me) the handling of events triggered by controls from within a DataGrid cell and not dealt with by the DataGrid itself. An example is handling of the OnCheckedChanged event of a CheckBox control with enabled AutoPostBack.

 

Background

If you, like me, found that using a DataGrid is one of the most convenient ways to handle any data-driven web application, you might quite often have experienced its limitations. If you need to get things done quickly, you might not want to write an entire column control yourself, but just use a TemplateColumn. Within this TemplateColumn, you would probably want to use some web controls and of course take advantage of all their native features, including events. But this doesn't work out quite as smoothly as it might sound. In the course of experimenting with controls inside a DataGrid, I experienced quite a lot of odd runtime compiler errors till I ran across one of the most interesting and useful DataGrid events: the ItemCreated event.

 

How-To

What we want to do here is just hook a handler routine onto the OnCheckedChanged event of the CheckBox control. The TemplateColumn with the CheckBox might look like the following

example:

<asp:TemplateColumn HeaderText="Checkbox">

    <ItemTemplate>

            <asp:CheckBox ID="cbExample" AutoPostBack="true" runat="server">

            </asp:CheckBox>

    </ItemTemplate>

</asp:TemplateColumn>

 

The function for handling the event in the underlying code will look very similar

to this:

    protected void OnChangeHandler(object sender, System.EventArgs e)

    {

        // Handle the event...

    }

(In this thread, you might find some example code for the handler.)

 

So, where are we going to connect the handler and the control? If we put the registration directly inside the control, such as OnCheckChanged="OnChangeHandler", while using code behind to support it, we will probably end up with a nice runtime compiler error. By applying the idea of delegate registration from Visual Studio, we can just do it at runtime. Using the ItemCreated event of the DataGrid (see code below) works out best for me, especially because I usually do a lot of other tweaking around like merging footer cells and switching sorting symbols.

 

    private void grid_ItemCreated( object sender,

           System.Web.UI.WebControls.DataGridItemEventArgs e)

    {

        ListItemType elemType = e.Item.ItemType;

        if ((elemType == ListItemType.Item)||

           (elemType == ListItemType.AlternatingItem))

        {

            // *** Event Handler for Checkbox ***

            CheckBox cBox = (CheckBox) e.Item.FindControl("cbExample");

            cBox.CheckedChanged += new EventHandler(OnChangeHandler);

        }

    }

 

Points of Interest

Handling DataGrid and related events is sometimes a cumbersome task, but the DataGrid is still one of my favorite controls. I would be happy to hear your solutions to this kind of problems. Particular points of interest: whether you simply placed a function call inside another DataGrid event, and why you preferred this solution in your particular situation.

출처: http://cafe.naver.com/headstudy.cafe(후다닥님 카페)

 

 
0
    
 
0
        list
 
※ 짧은 댓글일수록 예의를 갖추어 작성해 주시기 바랍니다.
line
reply cancel
 
번호 제목 글쓴이 추천 조회 날짜
29  ASP.NET에서 Gridview 내용을 Excel로 export하기   member 아침마당 2 / 0 9967 2010-11-01
28  ASP.NET에서 메일 보내기   member 아침마당 0 / 0 5634 2010-10-26
27  문자열 변수 = 문자열 변수 + 정수형 변수   member 아침마당 0 / 0 3311 2010-10-21
26  출력 매개변수를 갖는 저장 프로시저 ASP.NET에서 사용하기   member 아침마당 0 / 0 8151 2010-10-04
25  ASP.NET에서 저장 프로시저 사용하기   member 아침마당 2 / 0 9813 2010-09-30
24  저장 프로시저 만들기   member 아침마당 1 / 0 4786 2010-09-28
23  Visual Studio 2010 단축키 요약 파일 첨부파일   member 아침마당 0 / 0 5080 2010-09-07
22  ASP.NET에서 예외 처리에 대해서   member 아침마당 0 / 0 4580 2010-08-29
21  답변형 게시판 예제 파일   member 아침마당 1 / 0 3258 2010-08-22
20  리스트 컨트롤과 컬렉션에 대해서   member 아침마당 1 / 0 6119 2010-08-17
19  데이터 바인딩과 배열에 대해서   member 아침마당 0 / 0 3929 2010-08-16
18  Gridview control에 TemplateField를 사용하는 이유   member 아침마당 1 / 1 4363 2010-08-12
17  C#의 제어문   member 아침마당 0 / 0 5009 2010-08-04
16  Windows SharePoint Services 3.0 도구에 대하여   member 아침마당 0 / 0 3328 2010-07-17
15  C#의 기본 규칙   member 아침마당 0 / 0 3682 2010-07-09
14  닷넷에서 DataSet 다루기   member 아침마당 0 / 0 24578 2010-06-21
13  ASP.NET에서 TextBox에 onfocus, onblur 이벤트 적용 예   member 아침마당 0 / 0 5414 2010-06-15
12  ASP.NET에서 TextBox 엔터키 적용과 주민 번호 검증 예   member 아침마당 0 / 0 5530 2010-06-15
11  ASP.NET 정수<-->문자열 형변환, 메시지출력, switch문 사용 예   member 아침마당 0 / 0 8063 2010-06-15
10  닷넷에서 XML 다루기 이미지 첨부파일   member 아침마당 1 / 0 5653 2010-06-07
9  데이터 그리드 안에 있는 컨트롤 이벤트 만들기   member 깐소금 0 / 0 5403 2007-11-18
8  로딩페이지 구현하기   member 깐소금 0 / 0 7007 2007-11-18
7  CDO객체를 이용하여 야후 smtp로 메일 보내기   member 먹깨비 0 / 0 4970 2007-11-15
6  C# 버블소트 알고리즘   member 웹스톤 0 / 0 4463 2007-11-14
5  @OutputCache 디렉티브를 사용한 페이지 성능 향상   member 먹깨비 0 / 0 6511 2007-09-17
write
[2] button