首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从aspxgridview复选框dataitemtemplate中插入多个选中/未选中框值到数据库中?

如何从aspxgridview复选框dataitemtemplate中插入多个选中/未选中框值到数据库中?
EN

Stack Overflow用户
提问于 2019-04-07 16:44:03
回答 1查看 313关注 0票数 0

我正在尝试通过提交按钮将多个选中/未选中的框值插入数据库。单击提交按钮后,首先将一个或多个选中的复选框值插入数据库,然后在单击的复选框上显示选中/未选中的图像(其中选中的图像表示值1,未选中的图像表示值0 )。)

问题:

我尝试了下面的方法,但只能在数据库中插入一个复选框值,对于结果,复选框显示为数字而不是选中/未选中的图像,并且需要刷新页面才能显示选中/未选中的图像。

Aspx

代码语言:javascript
复制
 <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

代码语言:javascript
复制
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

预期结果

代码语言:javascript
复制
                                              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)

实际结果

代码语言:javascript
复制
                                              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)
EN

回答 1

Stack Overflow用户

发布于 2019-04-07 17:10:05

我不知道VB.net怎么样。我是一个PHP开发人员。

但在这多个单选按钮中勾选的值。

代码语言:javascript
复制
<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};

提交表单后,您将获得此多个单选检查值的数组。

就这样。

代码语言:javascript
复制
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55557161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档