2013-07-02 28 views
0

我有一個綁定到DataGrid的DataTable。 CheckBox在數據行中創建。當我檢查複選框後面的代碼被擊中時,但我不知道如何獲取DataRow。如何將DataRow從UI控件獲取到ASP.NET中的ascx.cs?

<asp:DataGrid ID="dgCaseStatusTypes" 
         runat="server" 
         AutoGenerateColumns="False" 
         CellPadding="5" 
         DataKeyField="InmateCaseStatusID" 
         OnItemCommand="dgCaseStatusTypes_ItemCommand"> 
     <Columns> 
      <asp:BoundColumn DataField="Code" HeaderText="Code"></asp:BoundColumn> 
      <asp:BoundColumn DataField="Text" HeaderText="Text"></asp:BoundColumn> 

      <asp:TemplateColumn HeaderText="Prebook Visible" > 
       <ItemTemplate> 
        <asp:CheckBox id="chkBox1" 
            runat="server" 
            AutoPostBack="true" 
            checked= '<%# Eval("IsPreBookVisibleBool") %>' 
            OnCheckedChanged="OnCheckedChanged_Event" 
            ></asp:CheckBox> 
       </ItemTemplate> 
      </asp:TemplateColumn> 
    </Columns> 
</asp:DataGrid> 

protected void OnCheckedChanged_Event(object sender, System.EventArgs e) 
{ 
    CheckBox cb = sender as CheckBox; 
    //how to get the DataRow that created this control? 
} 
+3

如果您還處於開發早期,請考慮從'DataGrid'切換到'GridView'。 'GridView'是建立在ASP.NET 2.0中以取代'DataGrid'。 –

回答

1

我真的只需要點擊的ID和CheckBox狀態。以下爲我工作。

<asp:DataGrid ID="dgCaseStatusTypes" 
        runat="server" 
        AutoGenerateColumns="False" 
        CellPadding="5" 
        DataKeyField="InmateCaseStatusID" 
        OnItemCommand="dgCaseStatusTypes_ItemCommand"> 
    <Columns> 
     <asp:TemplateColumn HeaderText="ID Label" Visible="false"> 
      <ItemTemplate> 
       <asp:Label id="IDLabel" 
          runat="server" 
          AutoPostBack="true" 
          Text='<%# Eval("InmateCaseStatusID") %>' 
          ></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateColumn> 

     <asp:BoundColumn DataField="Code" HeaderText="Code"></asp:BoundColumn> 
     <asp:BoundColumn DataField="Text" HeaderText="Text"></asp:BoundColumn> 

     <asp:TemplateColumn HeaderText="Prebook Visible" > 
      <ItemTemplate> 
       <asp:CheckBox id="chkBox1" 
           runat="server" 
           AutoPostBack="true" 
           checked= '<%# Eval("IsPreBookVisibleBool") %>' 
           OnCheckedChanged="OnCheckedChanged_Event" 
           ></asp:CheckBox> 
      </ItemTemplate> 
     </asp:TemplateColumn> 
    </Columns> 
</asp:DataGrid> 


    protected void OnCheckedChanged_Event(object sender, System.EventArgs e) 
     { 
      CheckBox cb = sender as CheckBox; 
      bool isChecked = cb.Checked; 
      DataGridItem dgi = cb.NamingContainer as DataGridItem; 
      Label lbl = dgi.FindControl("IDLabel") as Label; 
      string Id = lbl.Text as string; 
} 
-1

你將需要保持你的datagrid的數據源在session或viewstate。一旦你這樣做,你將需要知道你選中複選框時點擊的行。然後您可以獲取綁定到該行的對象。

相關問題