2016-10-19 24 views
0

enter image description here需要從細胞數據或數據庫的數據

人高填充GridView的複選框屬性,

我不是開發人員,但我總是盡我所能獨自困擾任何人之前來管理我的網頁編碼和通過檢查很多例子並將它們應用到我的Web應用程序中,但這次我真的投降了,不得不問。

我有一個Gridview,我將用它來更新一個CHECKBOX字段。 gridview有一個複選框控件,它的checked屬性需要基於數據庫值的True或False。 值爲1和2只

我寫了下面的代碼來捕獲細胞標籤,並改變複選框屬性,但它沒有工作

Protected Sub Refill_checkbox(ByVal sender As Object, ByVal e As System.EventArgs) 


    Dim i As Integer 

    For i = 0 To dg.Rows.Count - 1 
     Dim Test As String = CType(dg.Rows(i).Cells(3).FindControl("Att_Type"), Label).Text 

     If CType(dg.Rows(i).Cells(4).FindControl("Att_Type"), Label).Text = 1 Then 

      CType(dg.Rows(i).Cells(6).FindControl("CheckBox_Attendance"), CheckBox).Checked = True 
     Else 
      CType(dg.Rows(i).Cells(6).FindControl("CheckBox_Attendance"), CheckBox).Checked = False 
     End If 
     Response.Write(Test) 
    Next 

End Sub 

不幸的是,即使沒有寫測試變量找到如果它從gridview捕獲標籤。

我的GridView控件如下:

 <asp:GridView ID="dg" runat="server" BorderColor="#CCCCCC" BorderStyle="None" AutoGenerateColumns="False" 
         BorderWidth="1px" CellPadding="4" 
         EnableModelValidation="True" ForeColor="Black" GridLines="Horizontal" 
         Width="99%" AllowPaging="True" PageSize="500" DataSourceID="SqlDataSource_BCS" > 
      <Columns> 
       <asp:TemplateField HeaderText="#"> 
       <ItemTemplate> 
         <%#Container.DataItemIndex + 1 %> 
        </ItemTemplate> 
       </asp:TemplateField> 

       <asp:BoundField DataField="Att_ID" HeaderText="Att_ID" 
        SortExpression="Att_ID" /> 

       <asp:BoundField DataField="Emp_FullName" HeaderText="Emp_FullName" 
        SortExpression="Emp_FullName" /> 

       <asp:BoundField DataField="Att_Desc" HeaderText="Att_Desc" 
        SortExpression="Att_Desc" /> 

       <asp:BoundField DataField="Att_Type" HeaderText="Att_Type" 
        SortExpression="Att_Type" /> 

       <asp:BoundField DataField="Att_Date" HeaderText="Att_Date" 
        SortExpression="Att_Date" /> 


    <asp:templatefield HeaderText="Attendance" > 

     <itemtemplate > 

     <asp:CheckBox ID="CheckBox_Attendance" runat="server" /> 

     </itemtemplate> 
     <itemstyle horizontalalign="left" /> 

    </asp:templatefield> 

          </Columns> 

      <FooterStyle BackColor="#CCCC99" ForeColor="Black" /> 
      <HeaderStyle BackColor="White" Font-Bold="True" BorderWidth="0px"/> 
      <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" /> 
      <RowStyle BorderStyle="None" /> 
      <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" /> 
     </asp:GridView> 

我不知道什麼是做到這一點的正確方法。

在此先感謝

回答

0

您可以將複選框直接綁定到,像這樣的現場...

<asp:CheckBox ID="CheckBox_Attendance" runat="server" Checked='<%# Eval("Att_type").ToString() = "1" %>' /> 

否則,直接處理它在你的GridView RowDataBound事件:

把下面的代碼放到你的gridview上:

<asp:GridView ... OnRowDatabound="dg_RowDataBound"> 

更改您的模板列,看起來像這樣:

<asp:TemplateField HeaderText="Attendance"> 
    <ItemTemplate> 
     <asp:HiddenField id="hfType" Value='<%# Eval("Att_Type") %>' /> 
     <asp:CheckBox ID="CheckBox_Attendance" runat="server" /> 
    </ItemTemplate> 
    <ItemStyle HorizontalAlign="left" /> 
</asp:TemplateField> 

在後面的代碼

Protected Sub dg_RowDataBound(sender As Object, e As GridViewRowEventArgs) 
     Dim cbAttendance As Checkbox = e.Row.FindControl("CheckBox_Attendance") 
     Dim hfType as Hiddenfield = e.Row.FindControl("hfType") 

     If hfType.Value = "1" Then 
     cbAttendance.Checked = True 

End Sub 
+0

非常感謝,我裝盤了幾個小時:( –