我正在尝试通过提交按钮将多个选中/未选中的框值插入数据库。单击提交按钮后,首先将一个或多个选中的复选框值插入数据库,然后在单击的复选框上显示选中/未选中的图像(其中选中的图像表示值1,未选中的图像表示值0 )。)
问题:
我尝试了下面的方法,但只能在数据库中插入一个复选框值,对于结果,复选框显示为数字而不是选中/未选中的图像,并且需要刷新页面才能显示选中/未选中的图像。
Aspx
<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>
<asp:UpdateProgress ID="GeneralUpdateProgress" ClientIDMode="Static" runat="server">
<ProgressTemplate>
<img id="gupLoading" src="../images/loadingscreen.gif" />
</ProgressTemplate>
</asp:UpdateProgress>
</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>
<dx:ASPxButton ID="submitbtn" runat="server" Text="Submit" Visible="true" OnClick="submitbtn_Click" AutoPostBack="false"></dx:ASPxButton>
VB.net
Sub CustomCheckBoxColumn(ByVal fieldName As String)
Dim c As New GridViewDataColumn
c.DataItemTemplate = New CheckBoxTemplate
c.FieldName = fieldName
Dim dayOfDate As New DateTime
dayOfDate = getDate(fieldName)
c.Caption = c.FieldName + vbNewLine + Replace(dayOfDate.ToString("dddd"), "??", "")
c.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
c.CellStyle.HorizontalAlign = HorizontalAlign.Center
LeaveSystem.Columns.Add(c)
End Sub
Function getDate(ByVal fieldName As String) As DateTime
Dim chosenDate As New DateTime
chosenDate = Convert.ToDateTime(year.Value.ToString() + "-" + month.Value.ToString() + "-" + fieldName)
Return chosenDate
End Function
Protected Sub SelectedIndexChanged(sender As Object, e As EventArgs)
LeaveSystem.DataBind()
End Sub
Private Sub LeaveSystem_DataBinding(sender As Object, e As EventArgs) Handles LeaveSystem.DataBinding
LeaveSystem.Columns.Clear()
Dim sql As String = ""
If section.Value.ToString() = "PT" Then
sql = "select * from tb1"
Else
sql = "select * from tb2"
End If
Dim dt As New DataTable
dt = GetMssql(sql)
LeaveSystem.DataSource = dt
Dim a As New GridViewDataColumn
a.FieldName = "EMP_NO"
a.Caption = "EMPID"
LeaveSystem.Columns.Add(a)
Dim b As New GridViewDataColumn
b.FieldName = "CHI_NAME"
b.Caption = "CNAME"
LeaveSystem.Columns.Add(b)
Dim fieldName As String = ""
Dim lastDay As New Integer
lastDay = DateTime.DaysInMonth(DirectCast(year.Value, Integer), DirectCast(month.Value, Integer))
For i = 1 To lastDay
If i.ToString().Length = 1 Then
fieldName = "0" + i.ToString()
Else
fieldName = i.ToString
End If
CustomCheckBoxColumn(fieldName)
Next
End Sub
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"
Dim list As New ArrayList
list.Add(checkbox.Checked)
Dim x As String
For Each x In list
checkbox.ClientSideEvents.CheckedChanged = String.Format("function (s, e) {{ cb.PerformCallback(HiddenEmpID.GetValue() + '|' + year.GetValue() + '|' + month.GetValue() + '|' + '{0}|' + s.GetChecked(""" & x & """)); }}", 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
Next
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)
If IsPostBack Then
Dim p() As String = Session("par")
p(1) = Session("year")
p(2) = Session("month")
'Dim p() As String = e.Parameter.Split("|"c)
'p(0) = empid, p(1) = year, p(2) = month, p(3) = Date, p(4) = Boolean
Dim list As New ArrayList
list.Add(p(3))
If p(4) = True Then
p(4) = "1"
Else
p(4) = "0"
End If
Dim x As String
For Each x In list
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), x, p(4))
SetMssql(query)
Next
End If
End Sub
预期结果
YEAR : 2019 MONTH: 4
------------------------------------------------------------------
|EMP NO| 1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|.....|
------------------------------------------------------------------
| 234 |v | | | | | | | | | | | | | |v | | | | | |.....|
-------------------------------------------------------------------
-->| 456 | v| | | | | | | | | | |v | v| v| | v| | | | |.....|
------------------------------------------------------------------
(Submit) (cancel)
实际结果
YEAR : 2019 MONTH: 4
------------------------------------------------------------------
|EMP NO| 1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|.....|
------------------------------------------------------------------
| 234 | 1| | | | | | | | | | | | | | 1| | | | | |.....|
-------------------------------------------------------------------
-->| 456 | 1| | | | | | | | | | |1 | | 1| 0| 1| | | | |.....|
------------------------------------------------------------------
(Submit) (cancel)
https://stackoverflow.com/questions/55557161
复制相似问题