如何从aspxgridview复选框dataitemtemplate中将多个check checked / unchecked box值插入数据库?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (61)

我正在尝试通过提交按钮将多个选中/未选中的框值插入数据库。单击提交按钮后,将首先将一个或多个选中的复选框值插入数据库,然后选中/取消选中的图像将显示在单击的复选框上(选中图像表示值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)
提问于
用户回答回答于

我不知道VB.net。我是一名PHP开发人员。

但在这个多个无线电检查按钮值。

<form method="POST" action="submit.php" >
<input type="checkbox" name="gender" value="male" />
<input type="checkbox" name="gender" value="female" />
<input type="submit" name="submit" value="submit">
</form>
$_POST['gender']={male,female};

`````````````````````
after submitting the form you will get an array of this multiple radio check value.
that's it.

扫码关注云+社区

领取腾讯云代金券