2011-12-08 65 views
0

我正在開發一個使用ASP.NET的Intranet Web應用程序。現在,我需要爲我的部門開發培訓記錄。該部門由四個小組組成。爲此,我開發了這個記錄如下:我使用了一個Repeator,並在裏面放置了GridView,因爲我有三種不同類型的培訓課程。我使用HiddenField來確定每個培訓課程類型的ID。爲了從數據庫中檢索數據,我使用了StoredProcedure。一切工作正常,很好,經過這麼長的任務。如何檢查GridView字段內的某些複選框後更新GridView?

現在,我需要設置這些GridViews更新由管理員。因此,由於我在每個GridView中都有很多員工和很多課程,所以我認爲最好的 更新GridView的方式是將空白字段顯示爲複選框,並且管理員想要更新其中一個記錄的時間爲 員工,他所需要做的只是檢查複選框並單擊更新按鈕。 我可以 能夠顯示空字段作爲複選框,但現在我不知道如何 張貼檢查複選框到數據庫的值。

,我尋找它是使系統檢查每個 複選框中的每個GridView控件,如果已經檢查之前,離開它,因爲它是 的情景。如果它是空的並且現在只檢查它,則應將其添加到員工記錄中的 數據庫中。

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"> 
      <ItemTemplate> 

       <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("GroupID")%>' /> 

       <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
            SelectCommandType="StoredProcedure" SelectCommand="kbiReport"> 
            <%--FilterExpression="[Division] like '{0}%' and [Organization] like '{1}%'">--%> 

        <%--<FilterParameters> 
         <asp:ControlParameter ControlID="ddlDivision" Name="Division" 
               PropertyName="SelectedValue" Type="String" /> 
         <asp:ControlParameter ControlID="ddlOrganization" Name="Organization" 
               PropertyName="SelectedValue" Type="String" /> 
        </FilterParameters>--%> 

        <SelectParameters> 
         <%--ControlParameter is linked to the HiddenField above to generate different GridView based on different values 
          of GroupID--%> 
         <asp:ControlParameter ControlID="HiddenField1" Name="GroupID" PropertyName="Value" /> 
        </SelectParameters> 
       </asp:SqlDataSource> 

       <asp:GridView ID="GridView1" runat="server" 
           AllowSorting="True" 
           CellPadding="3" 
           DataSourceID="SqlDataSource1" 
           CssClass="mGrid" 
           AlternatingRowStyle-CssClass="alt" 
           RowStyle-HorizontalAlign="Center" 
           OnRowDataBound="GridView1_RowDataBound" OnDataBound="GridView1_DataBound"> 
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
        <HeaderStyle Font-Bold = "true" ForeColor="Black"/> 
        <Columns> 
         <asp:CommandField ShowSelectButton="True" /> 

         <%--<asp:TemplateField HeaderText="Select"> 
         <ItemTemplate> 
         <asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelect_CheckedChanged"/> 
         </ItemTemplate> 
         </asp:TemplateField>--%> 
        </Columns> 
        <EditRowStyle BackColor="#999999" /> 
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
        <SortedAscendingCellStyle BackColor="#E9E7E2" /> 
        <SortedAscendingHeaderStyle BackColor="#506C8C" /> 
        <SortedDescendingCellStyle BackColor="#FFFDF8" /> 
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> 
       </asp:GridView> 

      </ItemTemplate> 
     </asp:Repeater> 

     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
          ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
          SelectCommand="SELECT DISTINCT GroupID FROM courses"> 
     </asp:SqlDataSource> 

     <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" /> 
     <br /><br /> 

隱藏代碼:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      foreach (TableCell c in e.Row.Cells) 
      { 
       // Check if the cell vlaue = Yes 
       // if it is Yes, the cell will be colored with Light Green 
       if (c.Text.Equals("Yes")) 
       { 
        c.BackColor = System.Drawing.Color.LightGreen; 
       } 
      }  
     } 

     // The following is for changing the color of headers in each GridView based on the value of the HiddenFild 
     // BTW, the value of the HiddenField is the value of the GroupID in Group Table in the Database 
     else if(e.Row.RowType == DataControlRowType.Header){ 
      switch (((HiddenField)((GridView)sender).Parent.FindControl("HiddenField1")).Value) 
      { 
       case "1": 
        for (int i = 5; i &lt; e.Row.Cells.Count; i++) 
         e.Row.Cells[i].BackColor = System.Drawing.Color.LightBlue; 
        break; 

       case "2": 
        for (int i = 5; i &lt; e.Row.Cells.Count; i++) 
         e.Row.Cells[i].BackColor = System.Drawing.Color.LightYellow; 
        break; 

       case "3": 
        for (int i = 5; i &lt; e.Row.Cells.Count; i++) 
         e.Row.Cells[i].BackColor = System.Drawing.Color.Orange; 
        break; 
      } 
     }} 


     //For inserting checkboxes inside all courses buttons 
     protected void GridView1_DataBound(object sender, EventArgs e){ 
      GridView GridView1 = (GridView)sender; 
      foreach (GridViewRow objRow in GridView1.Rows) 
      { 
       for (int i = 5; i &lt; objRow.Cells.Count; i++){ 
        CheckBox chkCheckBox = new CheckBox(); 
        objRow.Cells[i].Controls.Add(chkCheckBox); 
        if (objRow.Cells[i].BackColor == System.Drawing.Color.LightGreen) 
         chkCheckBox.Checked = true; 
       } 

      } 
     } 


     //For updating the GridView (Save Button) 
     protected void btnSave_Click(object sender, EventArgs e) 
     { 

     } 

UPDATE: 我修改了btnSave_Click按鈕是如下:

//For updating the GridView (Save Button) 
     protected void btnSave_Click(object sender, EventArgs e) 
     { 
      GridView GridView1 = (GridView)sender; 
      // Are there checked boxes? 
      List<int> CheckBoxList = new List<int>(); 
      for (int i = 0; i < GridView1.Rows.Count; i++) 
      { 
       int courseid = (int)GridView1.DataKeys[i][0]; 
       CheckBox checkBox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox"); 
       if (checkBox.Checked) 
       { 
        CheckBoxList.Add(courseid); 
       } 
      } 
     } 

但我得到了以下錯誤: Sys系統.WebForms.PageRequestManagerServerErrorException:無法轉換類型爲「System.Web.UI.WebControls.Button」的對象以鍵入「System.Web .UI.WebControls.GridView'

我不知道爲什麼我得到這個錯誤。任何人都可以幫助我嗎?

+0

你能不能動態地添加一個字段到GridView這將顯示更新管理 – MethodMan

+0

沒有必要的。所有我想要的是識別檢查複選框,並更新數據庫中的記錄 –

+0

哦..好吧,那麼你可以做一個foreach循環,並檢查複選框,並從那裏做一些額外的編碼,如果有複選框被檢查讓我寄給你一個樣本 – MethodMan

回答

0

您將需要更改爲您所需要的工作,但如果你想檢查是否有GridView控件檢查嘗試的項目是:

List<int> CheckBoxList = new List<int>(); 
for (int i = 0; i < GridView.Rows.Count; i++) 
{ 
    int yourColumnId = (int)GridView.DataKeys[i][0]; 
    CheckBox cb = (CheckBox)GridView.Rows[i].FindControl("The name of your CheckBox"); 
    if (cb.Checked) 
    { 
     CheckBoxList.Add(productId); 
    } 
} 
// Do something with CheckBoxList 
+0

對不起,我不明白你的答案。你可以把它簡化爲我上面的代碼。我是新的ASP.NET開發人員,所以請原諒我,如果我問很多 –

+0

何時你想檢查按鈕是否被選中..?這個相同的代碼將在asp.net中工作,您只需要將其替換或放入正確的事件處理程序中即可。對於那部分我不確定你想要檢查複選框的位置 – MethodMan

相關問題