2016-04-29 53 views
0

我能夠從數據庫檢索出數據,以顯示哪些是Y和N使用GridView中的複選框。但是現在我想要能夠存儲編輯後將哪個複選框檢入到數據庫中。在數據庫ASP.NET的gridview中存儲選中的複選框C#

我做了什麼至今:

的.aspx

<asp:GridView ID="SiteGridView" runat="server" CssClass="datagrid" 
        GridLines="Vertical" AutoGenerateColumns="False" 
        Width="100%" 
        AllowPaging="True" AllowSorting="True" 
        DataKeyNames="promoID" OnRowCommand="GvPage_RowCommand" 
        OnPageIndexChanging="GvPage_PageIndexChanging" 
        OnSorting="GvPage_Sorting" 
        OnRowDatabound="SiteGridView_RowDataBound" 
        OnRowEditing="SiteGridView_RowEditing"> 
     <Columns>  
     <asp:TemplateField HeaderText="Default"> 
      <ItemTemplate> 
      <asp:CheckBox ID="process_flag" runat="server" 
       Checked='<%# bool.Parse(Eval("PROCESSED").ToString()) %>' 
       Enable='<%# !bool.Parse(Eval("PROCESSED").ToString()) %>' /> 
      </ItemTemplate> 
      <ItemStyle Width="20%" /> 
     </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 

代碼隱藏:

 SqlCommand cmd = new SqlCommand("SELECT * , CAST(CASE defaults WHEN 'Y' THEN 1 ELSE 0 END AS BIT) AS PROCESSED FROM Promotions"); 
       SqlDataAdapter da = new SqlDataAdapter(); 
       DataTable dt = new DataTable(); 
       da.SelectCommand = cmd; 

       // Save results of select statement into a datatable 
       da.Fill(dt); 

       SiteGridView.DataSource = dt; 
       SiteGridView.DataBind(); 

     protected void SiteGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       //check userrole & display accrodingly 
       if (Session["ROLE"].ToString() != "SA") 
       { 
        //ImageButton btnEdit = (ImageButton)e.Row.FindControl("btnDelete"); 
        //btnEdit.Visible = false; 
        SiteGridView.Columns[4].Visible = false; 
       } 
      } 
     } 
+0

是否要在每次點擊時保存複選框狀態?或者只是在用戶在多行上執行後才執行一次? – fnostro

+0

保存複選框狀態每次點擊 – lel

回答

0

由於某些原因複選框不會觸發GridView RowCommand事件。它可能可以與適當的電話ClientScript.GetPostBackEventReference()完成,但這是一個複雜的整體其他級別。因此,爲了作用於一個CheckBox點擊單獨處理事件:

(注:我已經去掉了很多你的編碼,以澄清我的觀點。)

  1. 在你的CheckBox設置AutoPostBack="true"
  2. 添加處理程序OnCheckedChanged
  3. 確保DataKey包含行,你需要具有CheckBox國家

    // Simplified version of your markup 
    <asp:GridView ID="SiteGridView" runat="server" 
           DataKeyNames="promoID" 
           OnRowDataBound="SiteGridView_RowDataBound"> 
        <Columns> 
        <asp:TemplateField HeaderText="Default"> 
         <ItemTemplate> 
         <asp:CheckBox ID="process_flag" runat="server" 
          Checked='<%# bool.Parse(Eval("PROCESSED").ToString()) %>' 
          Enable='<%# !bool.Parse(Eval("PROCESSED").ToString()) %>' 
          OnCheckedChanged="process_flag_CheckedChanged" /> 
         </ItemTemplate> 
        </asp:TemplateField> 
        </Columns> 
    </asp:GridView> 
    
  4. 更新在RowDataBound情況下,找到複選框,並添加行索引作爲一個屬性

    // Merge this with your `RowDataBound` event 
    protected void SiteGridView_RowDataBound(object sender, GridViewRowEventArgs e) { 
        if (e.Row.RowType == DataControlRowType.DataRow) { 
         CheckBox cbx = (CheckBox) e.Row.FindControl("CheckBox1"); 
         cbx.Attributes[ "data-row-idx" ] = e.Row.RowIndex.ToString(); 
        } 
    } 
    
    的PK
  5. 處理的CheckChanged事件:

    // Add this handler to your code 
    protected void process_flag_CheckedChanged(object sender, EventArgs e) 
    { 
        CheckBox cbx = (CheckBox) sender; 
        Int32 rowidx = Convert.ToInt32(cbx.Attributes["data-row-idx"]); 
        Int32 pk = (Int32) GridView4.DataKeys[rowidx].Value; 
    
        // simple way to see event arguments before actually touching the database 
        GridView4.Caption = string.Format(
         "CheckChanged: Row {0}, State: {1}, RowPK: {2} ", rowidx , (cbx.Checked ? "Checked" : "Unchecked"), pk); 
    
        // Save Data and rebind the gridview to reflect changes 
        // Verify your parameters before attempting an actual update 
        // YourDatabaseCallWithAppropriateParamaters(...); 
        GridView1.DataBind(); 
    } 
    
1

你試過: 選中=」 <%#的eval( 「加工」)% >'

如果是這樣,你收到了什麼錯誤消息?

+0

試過!沒有錯誤彈出 – lel

0

首先使用foreach循環找出哪些行被檢查。然後將這些行的ID存儲在列表中。

foreach (GridViewRow row in SiteGridView.Rows) 
     { 
      if (row.RowType == DataControlRowType.DataRow) 
      { 
       CheckBox chkRow = (row.Cells[0].FindControl("process_flag") as CheckBox); 
       if (chkRow.Checked) 
       { 
        string ID = row.Cells[columnID].Text; 
+0

對不起,但columnID是?因爲我的其他列綁定到數據庫。 – lel

+0

向我顯示您的SiteGridView_RowDataBound事件 –

+0

我將代碼隱藏SiteGridView_RowDataBound事件 – lel