2010-03-16 4 views
1

어떻게 자동으로 생성 된 명령 단추를 사용자 정의 할 수 있습니까? Delete?사용자 지정 GridView 삭제 단추

삭제시 클라이언트 확인을 추가하고이 버튼이 AutoGenerateDeleteButton="true"에서 생성되도록하고 싶습니다. 그것은 가능한가 ??

<asp:TemplateField> 
    <ItemTemplate> 
     <asp:LinkButton runat="server" CommandName="Delete" OnClientClick="return confirm('Delete?')">Delete</asp:LinkButton> 
    </ItemTemplate> 
</asp:TemplateField> 

를하지만 자동으로 지역화되지되며 AutoGenerateDeleteButton="true" 설정에없는 생성됩니다

나는 사용자 정의 버튼이 방법을 추가 할 수 있습니다!

답변

0

아마도 그리드에 PreRender 이벤트를 구현하면됩니다. 또한

protected void yourGrid_PreRender(object sender, EventArgs e) 
{ 
    GridView grd = (GridView)(sender); 

    // iterate through all your rows and look for the button 
    // make sure to add code to verify your rows, columns, and control bounds are valid 
    for (int rowIndex = 0; rowIndex < grd.Rows.Count; rowIndex++) 
    { 
     LinkButton btn = grd.Rows[rowIndex].Cells[deleteButtonColumnIndex].Controls[0] as LinkButton; 

     // Here you have access to the button so change it to do what you need. 
     btn.OnClientClick = string.Format("return confirm('{0}?')", btn.Text); 
    } 
} 

당신은 당신이 아마의 GridView를 확장하고 자신의 코드를 구현해야합니다에 구운하려는 경우 : 여기

몇 가지 기본 사이비 코드입니다.

http://forums.asp.net/p/1396268/3011988.aspx#3011988

+0

@abatishchev 바로 다음 작업을 수행 했습니까? – Kelsey

+0

아니요, 포기했습니다. 내 맞춤 삭제 버튼은 항상 하드 코딩 된 영어 라벨과 동일합니다. – abatishchev

2

차라리 대신 미리 렌더링 이벤트의 RowDataBound 이벤트를 사용하는 것이 좋습니다 : 다음 스레드를 참조하십시오.

여기에서 특정 행의 요소에 쉽게 액세스 할 수 있습니다. (나는 Kelsey가 게시 한 솔루션이 페이징에 문제가있을 수 있다고 생각합니다.)

Linkbutton에 ID를 부여하고 RowDataBound 이벤트에 적용하십시오. 먼저

void gv_RowDataBound(Object sender, GridViewRowEventArgs e) 
    { 
    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
     LinkButton _foo = e.Row.FindControl("LINKBUTTONID") as LinkButton; 
     if(_foo != null) 
     { 
     _foo.OnClientClick = "insert localized text here"; 
     } 
    } 
    } 
0

, 당신은 솔루션 탐색기 탭 (내가 VWD를 사용)에서 루트 파일에 rightclicking하여 된 .vb 파일/클래스를 만들어야합니다. 새로 추가를 선택하고 클래스 페이지를 선택하십시오. 공유 클래스가 상주 할 App_Code 폴더를 만들 것을 제안합니다. 파일/클래스 이름을 "DeleteButtonField.vb"로 지정하고 확인을 클릭하십시오.

그런 다음 DeleteButtonField라는 새 .vb 파일을 열고 다음 코드를 복사하여 붙여 넣거나 입력 할 수 있습니다.()는 재정의 하위 InitializeCell (........ 보호 정의 코드의 정말 긴 비트를 완료하기 위해 인텔리을 사용할 수 없습니다.)

Imports Microsoft.VisualBasic 
Imports System 
Imports System.Web.UI.WebControls 

Namespace myControls 
Public Class DeleteButtonField 
    Inherits ButtonField 
    Private _confirmText As String = "Delete This Record?" 
    Public Property ConfirmText() As String 
    Get 
     Return _confirmText 
    End Get 
    Set(ByVal value As String) 
     _confirmText = value 
    End Set 
    End Property 
    Public Sub New() 
    Me.CommandName = "Delete" 
    Me.Text = "Delete" 
    End Sub 

    Public Overrides Sub InitializeCell(ByVal cell As System.Web.UI.WebControls.DataControlFieldCell, ByVal cellType As System.Web.UI.WebControl.DataControlCellType, ByVal rowState As System.Web.UI.WebControl.DataControlRowState, ByVal rowIndex As Integer) 
    MyBase.InitializeCell(cell, cellType, rowState, rowIndex) 
    If cellType = DataControlCellType.DataCell Then 
     Dim button As WebControl = CType(cell.Controls(0), WebControl) 
     button.Attributes("onclick") = String.Format("return confirm('{0}');", _confirmText) 
    End If 
End Sub 
End Class 
End Namespace 

저장 된 .vb 파일을. 그런 다음 .aspx 페이지에서 소스 모드로 페이지를 열고 GridView 정의 (예 : 태그)를 찾습니다. 삭제 버튼을 표시 할 위치 (첫 번째 위치, 두 번째 위치 등)를 선택할 수 있습니다. 텍스트 위치가 당신이 정의를 변경하고, 추가하지 않도록 다음과 같은

<custom:DeleteButtonField ConfirmText="Are you sure that you want to delete this record?"></custom:DeleteButtonField> 
당신은 또한 <퍼센트의 @ 페이지 후 페이지 상단에 한 줄을 추가해야

...>로 다음은

<%@ Register TagPrefix="custom" Namespace="myControls" %> GridView의 새로운 삭제 버튼을 사용하려는 모든 페이지에 추가해야합니다.이 설정을 web.config의 기본값으로 설정할 수있는 방법이있을 수 있습니다. 내 학습의이 단계에서.

.aspx 페이지를 저장하고 테스트하십시오.이제 응용 프로그램의 모든 GridView에 연결할 수있는 공통적 인 Sub (표준 삭제 버튼과 그 동작을 정의)를 정의했습니다.

관련 문제