2014-10-06 95 views
0

我有一個DataGrid,它具有用於選擇特定用戶的複選框。點擊Ok按鈕後,我應該得到複選框的狀態,如果選中,則獲取該行的ID。我到達複選框,但值總是Checked = false,當然,即使當我檢查我的複選框。代碼如下:無法獲取數據網格中的複選框值ASP.NET

的.aspx

 <Columns> 

      <asp:TemplateColumn HeaderText="Odabir"> 
       <ItemTemplate> 
        <asp:CheckBox ID="chkVjezba" runat="server" Enabled="true"/> 
       </ItemTemplate> 
      </asp:TemplateColumn> 

      <asp:BoundColumn HeaderText="Vježba" DataField="Naziv"></asp:BoundColumn> 

      <asp:TemplateColumn HeaderText="Broj serija"> 
       <ItemTemplate> 
        <asp:TextBox ID="brojSerija" runat="server" Width="20px"></asp:TextBox> 
       </ItemTemplate> 
      </asp:TemplateColumn> 

      <asp:TemplateColumn HeaderText="Ocjena 6"> 
       <ItemTemplate> 
        <asp:TextBox ID="ocjena6" runat="server" Width="20px"></asp:TextBox> 
       </ItemTemplate> 
      </asp:TemplateColumn> 

      <asp:TemplateColumn HeaderText="Ocjena 7"> 
       <ItemTemplate> 
        <asp:TextBox ID="ocjena7" runat="server" Width="20px"></asp:TextBox> 
       </ItemTemplate> 
      </asp:TemplateColumn> 

      <asp:TemplateColumn HeaderText="Ocjena 8"> 
       <ItemTemplate> 
        <asp:TextBox ID="ocjena8" runat="server" Width="20px"></asp:TextBox> 
       </ItemTemplate> 
      </asp:TemplateColumn> 

      <asp:TemplateColumn HeaderText="Ocjena 9"> 
       <ItemTemplate> 
        <asp:TextBox ID="ocjena9" runat="server" Width="20px"></asp:TextBox> 
       </ItemTemplate> 
      </asp:TemplateColumn> 

      <asp:TemplateColumn HeaderText="Ocjena 10"> 
       <ItemTemplate> 
        <asp:TextBox ID="ocjena10" runat="server" Width="20px"></asp:TextBox> 
       </ItemTemplate> 
      </asp:TemplateColumn> 
     </Columns> 

    </asp:DataGrid> 

我的.cs代碼文件:

foreach (DataGridItem row in vjezbeGrid.Items) 
      { 
      if (((CheckBox)row.Cells[0].FindControl("chkVjezba")).Checked) 
      { 
       //something 
      } 

     } 

感謝。

回答

1

你可能會在每次回傳數據綁定。確保你的數據綁定只有在!Page.IsPostBack。

+0

就是這樣!大。我剛剛在頁面加載事件中添加if(!IsPostBack) – rukfash 2014-10-06 14:31:14

1

嘗試這樣

foreach (DataGridItem row in vjezbeGrid.Items) 
     { 
      if (row.ItemType == ListItemType.Item) 
      { 
       CheckBox chkVjezba = (CheckBox)row.FindControl("chkVjezba"); 

       if (chkVjezba.Checked) 
       { 
        //something 
       } 
      } 

     } 
+0

謝謝,添加if(!isPostBack)到pageLoad事件解決了這個問題。 – rukfash 2014-10-06 14:32:06

+0

@rukfash不要忘記標記爲answer.whose的答案對你更有幫助 – 2014-10-06 16:00:55