2009-06-23 2 views
1

그리드보기가 있고 템플릿 체크 박스가 있습니다 ... 편집 버튼이 있습니다.gridview에서 편집 버튼을 클릭하면 checkbox templete가 자동으로 선택되는 방법 C#

일단 내가 gridview ....에서 편집 버튼을 클릭하면이 템플릿 확인란이 자동으로 체크되어 있어야합니다 == 사실 ... 자동으로 선택됩니다 클릭 grond view.can에서 편집 buton 클릭하면 선택해야합니다 아무도 그 코드를 말해 ... 감사합니다

답변

0

다음은 Person 개체 집합을 채우는 표보기의 샘플 코드입니다.

은의 GridView

<asp:GridView runat="server" ID="grdView" AutoGenerateColumns="False" 
     onrowcancelingedit="grdView_RowCancelingEdit" 
     onrowdatabound="grdView_RowDataBound" onrowediting="grdView_RowEditing"> 
     <Columns> 
      <asp:BoundField DataField="ID" HeaderText="ID" /> 
      <asp:BoundField DataField="Name" HeaderText="Name" /> 
      <asp:TemplateField HeaderText="IsActive Template"> 
       <ItemTemplate> 
        <asp:Label runat="server" ID="lblIsActive"></asp:Label> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:CheckBox ID="chkIsActive" runat="server" /> 
       </EditItemTemplate> 
      </asp:TemplateField> 
      <asp:CommandField ShowEditButton="True" /> 
     </Columns> 
    </asp:GridView> 

의 마크 업 코드하고이 이벤트를 처리 뒤에 당신이 RowDataBound 이벤트가 작성을위한 책임이다 보듯이

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     BindGrid(); 
    } 

    private void BindGrid() 
    { 
     List<Person> persons = new List<Person> 
     { 
      new Person{ID = 1, IsActive = true, Name = "Test 1"}, 
      new Person{ID = 2, IsActive = true, Name = "Test 2"}, 
      new Person{ID = 3, IsActive = true, Name = "Test 3"} 
     }; 

     grdView.DataSource = persons; 
     grdView.DataBind(); 
    } 

    protected void grdView_RowEditing(object sender, GridViewEditEventArgs e) 
    { 
     grdView.EditIndex = e.NewEditIndex; 

     grdView.DataBind(); 
    } 

    protected void grdView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 
    { 
     grdView.EditIndex = -1; 
     grdView.DataBind(); 
    } 

    protected void grdView_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     Person p = e.Row.DataItem as Person; 
     if (p == null) 
      return; 
     var lbl = e.Row.Cells[2].FindControl("lblIsActive") as Label; 
     if (lbl != null) 
     { 
      lbl.Text = p.IsActive ? "Yes" : "No"; 
     } 
     else 
     { 
      var chkIsActive = e.Row.Cells[2].FindControl("chkIsActive") as CheckBox; 
      if (chkIsActive != null) 
      { 
       if (p != null) 
        chkIsActive.Checked = p.IsActive; 
      } 
     } 
    } 


} 

class Person 
{ 
    public int ID { get; set; } 

    public string Name { get; set; } 

    public bool IsActive { get; set; } 
} 

그래서 코드 템플릿 필드에서 올바른 값을 선택합니다.

관련 문제