场景:
我在客户端具有ASPxGridview,在服务器端使用了复选框控件。我已经声明了一个aspxbutton(submit)将选中的复选框值(v = 1,empty = 0)插入数据库,并同时计算每一行选中的复选框的数量。在复选框模板中,v表示已选中,空表示未选中,并且列中的数字表示基于月份的日期。我想计算emp no:234的已选中复选框的数量。
YEAR : 2019 MONTH: 3
------------------------------------------------------------------
|EMP NO| 1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|.....|
------------------------------------------------------------------
| 123 | v| | | | | | | | | | | | | | v| | | | | |.....|
-------------------------------------------------------------------
-->| 234 | v| | | | | | | | | | |v | v| v| | v| | | | |.....|
------------------------------------------------------------------
(Submit) (cancel)
aspxgridview(aspx)的代码
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div class="flexs">
<dx:ASPxComboBox ID="year" runat="server" AutoPostBack="true" ClientInstanceName="year" ValueType="System.Int32" Width="100px" CssClass="ddstyle mr10px" OnSelectedIndexChanged="SelectedIndexChanged"></dx:ASPxComboBox>
<dx:ASPxComboBox ID="month" runat="server" AutoPostBack="true" ClientInstanceName="month" ValueType="System.Int32" Width="100px" CssClass="ddstyle mr10px" OnSelectedIndexChanged="SelectedIndexChanged"></dx:ASPxComboBox>
<dx:ASPxComboBox ID="section" runat="server" AutoPostBack="true" ValueType="System.String" Width="100px" CssClass="ddstyle" OnSelectedIndexChanged="SelectedIndexChanged"></dx:ASPxComboBox>
</div>
</div>
<dx:ASPxGridView ID="LeaveSystem" runat="server" AutoGenerateColumns="false" KeyFieldName="EMP_NO;Year;Month" Width="100%">
<SettingsBehavior AllowDragDrop="false" AllowSort="false" />
<SettingsPager Mode="ShowAllRecords" />
</dx:ASPxGridView>
</ContentTemplate>
</asp:UpdatePanel>
<dx:ASPxCallback ID="cb" ClientInstanceName="cb" runat="server" OnCallback="cb_Callback" ></dx:ASPxCallback>
<dx:ASPxLabel ID="HiddenEmpID" runat="server" ClientInstanceName="HiddenEmpID" ClientVisible="false"></dx:ASPxLabel>
<div class="flexs5 mt10px text-center">
<dx:ASPxButton ID="submitbtn" runat="server" Text="Submit" OnClick="submitbtn_Click" AutoPostBack="false" CssClass="mr10px"></dx:ASPxButton>
</div>
代码vb.net
Friend Class CheckBoxTemplate
Implements ITemplate
Public Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
Dim checkbox As New ASPxCheckBox
Dim gridContainer As GridViewDataItemTemplateContainer = CType(container, GridViewDataItemTemplateContainer)
'Dim empid As String = gridContainer.Grid.GetRowValues(gridContainer.VisibleIndex, "EmpID").ToString()
checkbox.AllowGrayed = False
checkbox.CheckedImage.Url = "images/checked_image.png"
checkbox.UncheckedImage.Url = "images/unchecked_image.png"
checkbox.CssClass += "customcheckbox"
checkbox.ID = "DateCheckBox"
checkbox.ClientSideEvents.CheckedChanged = String.Format("function (s, e) {{ cb.PerformCallback(HiddenEmpID.GetValue() + '|' + year.GetValue() + '|' + month.GetValue() + '|' + '{0}|' + s.GetChecked()); }}", gridContainer.Column.FieldName)
checkbox.Value = DataBinder.Eval(gridContainer.DataItem, gridContainer.Column.FieldName)
If checkbox.Value = 1 Then
checkbox.Checked = True
Else
checkbox.Checked = False
End If
checkbox.ValueType = GetType(Int32)
checkbox.ValueUnchecked = 0
checkbox.ValueChecked = 1
container.Controls.Add(checkbox)
End Sub
End Class
Protected Sub cb_Callback(source As Object, e As CallbackEventArgs)
Session("par") = e.Parameter.Split("|"c)
End Sub
问题:
我已经在网上搜索并尝试了下面的方法,但这似乎不起作用。
Protected Sub submitbtn_Click(sender As Object, e As EventArgs)
' Dim fieldname1 As String = ""
If IsPostBack Then
Dim p() As String = Session("par")
'Dim p() As String = e.Parameter.Split("|"c)
'p(0) = empid, p(1) = year, p(2) = month, p(3) = Date, p(4) = Boolean
If p(4) = True Then
p(4) = "1"
Else
p(4) = "0"
End If
Dim query As String = String.Format("UPDATE LeaveSystem SET [{3}] = '{4}', UpdateTime = GETDATE() WHERE EmpID = '{0}' and Year = '{1}' and Month = '{2}' IF @@ROWCOUNT=0 INSERT INTO LeaveSystem (EmpID, Year, Month, [{3}], UpdateTime) values ('{0}', '{1}', '{2}', '{4}', GETDATE())", p(0), p(1), p(2), p(3), p(4))
SetMssql(query)
End If
'' Check exceed 13 days
Dim count1 As Integer = 0
For I As Integer = 0 To LeaveSystem.VisibleRowCount - 1
Dim chkRow As ASPxCheckBox = TryCast(LeaveSystem.FindRowCellTemplateControl(I, Nothing, "cb"), ASPxCheckBox)
For k As Integer = 2 To LeaveSystem.Columns.Count - 1
Dim aheaderCb = TryCast(LeaveSystem.FindHeaderTemplateControl(LeaveSystem.Columns(k), "cb1"), ASPxCheckBox)
If chkRow.IsVisible = True Then
If aheaderCb.Checked = True Then
count1 = count1 + 1
End If
End If
Next k
Next I
End Sub
执行提交按钮时,未设置为对象错误实例的对象引用出现在行上:如果chkRow.IsVisible = True则。我已经尝试了很多次,但是结果还是一样。请指导我。
答案 0 :(得分:0)
尝试
protected void btnGetRecord_Click(object sender, EventArgs e)
{
var checkedRows = (from GridViewRow row in myGrid.Rows
let checkbox = (CheckBox)row.FindControl("chkSelect")
where checkbox != null && checkbox.Checked == true
select row).ToList();
}
您可以将代码转换为VB.NET。