2011-09-09 3 views
4

확인란이 비활성화 된 gridview가 있습니다. 나는 gridview에있는 편집 버튼을 클릭 할 때 활성화 할 수 있습니다. 여기에 마크 업을CheckBox Gridview 활성화 및 비활성화

<asp:GridView ID="grd_Bookcode" runat="server" DataSourceID="sqldatasource1" 
autogeneratecolumns="False" onrowcommand="grd_Bookcode_RowCommand1" 
onrowdatabound="grd_Bookcode_RowDataBound"> 
<Columns> 
    <asp:BoundField DataField="BookCode" HeaderText="Book Code"/> 
    <asp:BoundField DataField="mag_name" HeaderText="Name"/> 
    <asp:BoundField DataField="display_date" HeaderText="Display Date"/> 
    <asp:TemplateField HeaderText = "PC"> 
     <ItemTemplate> 
      <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("82_PC").ToString() == "1" ? true:false %>' Enabled="false" /> 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField HeaderText="eReader"> 
     <ItemTemplate> 
      <asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# Eval("83_eReader").ToString() == "1" ? true:false %>' Enabled="false" /> 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField HeaderText="Tablet"> 
     <ItemTemplate> 
      <asp:CheckBox ID="CheckBox3" runat="server" Checked='<%# Eval("84_Tablet").ToString() == "1" ? true:false %>' Enabled="false"/> 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField HeaderText="Mobile"> 
     <ItemTemplate> 
      <asp:CheckBox ID="CheckBox4" runat="server" Checked='<%# Eval("85_Mobile").ToString() == "1" ? true:false %>' Enabled="false" /> 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField HeaderText="None"> 
     <ItemTemplate> 
      <asp:CheckBox ID="CheckBox5" runat="server" Checked='<%# Eval("86_None").ToString() == "1" ? true:false %>' Enabled="false" /> 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:CommandField ShowEditButton="True" /> 
</Columns> 

있어 그리고 여기 내가 사용하려고 해요 코드입니다. 기본적으로 편집 버튼을 누르면 체크 박스 자체를 활성화해야합니다. 어떤 이유로 든 페이지가 다시로드 될 때 확인란이 전혀 사용되지 않습니다. 편집 버튼을 클릭 한 후에 "체크 박스 1"을 활성화하려고했지만 결국 5 개의 체크 박스를 모두 활성화하려고합니다.

protected void grd_Bookcode_RowCommand1(object sender, GridViewCommandEventArgs e) 
    { 
     if (e.CommandName == "Edit") 
     { 
      int index = Convert.ToInt32(e.CommandArgument); 

      GridViewRow row = grd_Bookcode.Rows[index]; 

      CheckBox chk = (CheckBox)row.FindControl("CheckBox1"); 
      chk.Enabled = true; 


     } 
    } 
+0

당신이 사용하고있는 코드와 무슨 일이에요? –

+0

이 코드가 체크 박스를 활성화하지 않습니까? 이 질문에 대해 더 명확히 해 주실 수 있습니까? – Praveen

+0

무엇이 문제입니까? 무엇이 작동하지 않습니까? 디버그하는 경우이 코드 줄에 도달 할 수 있습니까? CheckBox chk = (CheckBox) row.FindControl ("CheckBox1"); ? –

답변

3

편집 컨트롤을 표준 컨트롤과 다르게하려면 "EditItemTemplate"을 사용해야합니다. 이렇게하면 행의 모드가 변경 될 때 편집 행이 다른 컨트롤, 값 등을 가질 수 있습니다.

예 :

 <Columns> 
      <asp:TemplateField HeaderText="PC"> 
       <ItemTemplate> 
        <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("82_PC").ToString() == "1" ? true:false %>' Enabled="false" /> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:CheckBox ID="CheckBox1" runat="server" Checked="true" Enabled="false" /> 
       </EditItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
+0

정확히 정확히 내가 필요로하는 것, 덕분에 – Jeff

+0

당신의 문제를 해결한다면 대답으로 표시하는 것을 잊지 마라! – Zachary

+0

@ Zachary 정말 고마워요 !! 나는 지금 이것을위한 시대를 찾고 있었으며, 멋지고 단순하게 유지했다! –

1

는 내가의 GridView의 모든 행을 통해 당신은 할 수 루프를 추측하고 아래와 같이 체크 박스 뭔가를 활성화 :이 도움이

protected void grd_Bookcode_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     if (e.CommandName == "Edit") 
     { 
      for (int index = 0; index < GridView1.Rows.Count; index++) 
      { 
       CheckBox chk = grd_Bookcode.Rows[index].FindControl("CheckBox" + index + 1) as CheckBox; 
       chk.Enabled = true; 
      } 
     } 
    } 

희망을!

+0

맞아, 그게 내가 할 줄 알았지 만, edititemtemplate를 사용하는 것이 훨씬 쉬운 방법이었다. – Jeff

관련 문제