2017-08-28 86 views
8

我複選框在ASPxGridView這樣的:獲取CheckBox.Checked在ASPxGridView不正確沃金

<dxwgv:ASPxGridView ID="Grid_Loading_Plan_Detail" runat="server" >   
... 
<dxwgv:GridViewDataColumn Caption="#" VisibleIndex="1"> 
<DataItemTemplate> 
<dxe:ASPxCheckBox ID="loadingStatus" runat="server" Checked='<%# GetChecked(Eval("loadingStatus").ToString()) %>'></dxe:ASPxCheckBox> 
</DataItemTemplate> 
</dxwgv:GridViewDataColumn> 
... 
</dxwgv:ASPxGridView> 

它很好地結合時,該複選框加載,這裏的功能:

Public Function GetChecked(value As String) As Boolean 
    Select Case value 
     Case "1" 
      Return True 
     Case "0" 
      Return False 
     Case Else 
      Return False 
    End Select 
End Function 

的問題是,當檢查checked狀態時,它總是返回加載的值。如果第一次加載狀態爲checked,然後我做了unchecked CheckBox,它仍然會返回checked。在這裏,我怎麼得到的值,並將其保存到數據庫:

Protected Sub btSimpan_Click(sender As Object, e As EventArgs) Handles btSimpan.Click 
    Try 
     sqlstring = "" 
     For i As Integer = 0 To Grid_Loading_Plan_Detail.VisibleRowCount - 1 
      Dim loadingStatus As DevExpress.Web.ASPxEditors.ASPxCheckBox = Grid_Loading_Plan_Detail.FindRowCellTemplateControl(i, Grid_Loading_Plan_Detail.Columns(1), "loadingStatus") 

      sqlstring = sqlstring & " UPDATE containerTransaction SET loadingStatus = '" & IIf(loadingStatus.Checked, "1", "0") & "' " & _ 
         " WHERE ID = '" & Grid_Loading_Plan_Detail.GetRowValues(i, "ID") & "'; " 
     Next 

     If SQLExecuteNonQuery(sqlstring) > 0 Then 
      Response.Redirect("loading-plan.aspx") 
     End If 
    Catch ex As Exception 
     Response.Write("Error btSimpan_Click <BR> " & ex.ToString) 
    End Try 
End Sub 

ADDED

在這裏,我如何綁定數據:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    Try 
     If Not Page.IsPostBack Then 
      Call Load_menu() 
     End If 
    Catch ex As Exception 
     Response.Write("Page_Load Exception :<br>" & ex.ToString) 
    End Try 

    If Not Session("Grid_Loading_Plan_Detail") Is Nothing Then 
     Grid_Loading_Plan_Detail.DataSource = CType(Session("Grid_Loading_Plan_Detail"), DataTable) 
     Grid_Loading_Plan_Detail.DataBind() 
    End If 
End Sub 

而這裏Load_menu()功能:

Private Sub Load_menu() 
    Try 
     sqlstring = "SELECT ID, code, date, container, seal, sender, receiver, [weight], loadingStatus " & _ 
      "FROM containerTransaction " & _ 
      "WHERE loadingCode = (SELECT TOP 1 code FROM loadingPlan WHERE ID = '" & ID & "') AND [status] = 1 " & _ 
      "ORDER BY [weight] " 
     DS = SQLExecuteQuery(sqlstring) 
     DT = DS.Tables(0) 
     Session("Grid_Loading_Plan_Detail") = DT 
     Grid_Loading_Plan_Detail.DataSource = DT 
     Grid_Loading_Plan_Detail.KeyFieldName = "ID" 
     Grid_Loading_Plan_Detail.DataBind() 
    Catch ex As Exception 
     Response.Write("Load_Menu Exception :<br>" & ex.ToString) 
    End Try 
End Sub 

我在這裏錯過了什麼?任何幫助將被認可。謝謝。

+0

如何以及何時將數據綁定到GridView?如果您未將數據綁定包裝在「IsPostBack」檢查中,每次發生PostBack時,複選框的狀態將被其數據庫值覆蓋。 – VDWWD

+0

您也正在使用loadingStatus作爲參數調用您的checked函數,所以無論何時它被選中或取消選中,它都會使用加載參數調用加載函數 – MaCron

+0

不太確定我是否完全理解該案例,但我認爲這可能是' LoadPostData()'在代碼中缺少。嘗試在這行之後調用'DirectCast(btSimpan,IPostBackDataHandler).LoadPostData(btSimpan.UniqueID,Request.Form)'Dim loadingStatus As DevExpress.Web.ASPxEditors.ASPxCheckBox = ....'並參閱。順便說一句,我沒有真正進入VB.NET,所以我將我的C#代碼片段轉換爲這一片。原來在C#中是((IPostBackDataHandler)btSimpan).LoadPostData(btSimpan.UniqueID,Request.Form);'。希望能幫助到你..如果是這樣,請告訴我,我明天會發布完整答案。祝你好運:) –

回答

2

對不起,

這是會話綁定問題,檢查是否會話中使用,那麼數據源將用綁定。代碼是:

If Not Session("Grid_Loading_Plan_Detail") Is Nothing Then 
    Grid_Loading_Plan_Detail.DataSource = CType(Session("Grid_Loading_Plan_Detail"), DataTable) 
    Grid_Loading_Plan_Detail.DataBind() 
End If 

我刪除該變量,並且現在一切正常。

CNC中

它有搜索,如果我刪除了會話檢查排序問題。我有新的解決方案,通過搜索一些更合適的文章,並且ASPxGridView中的搜索/排序功能仍在工作。

調用到了init load_menu()功能,對於這種情況最好的辦法:

Private Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init 
    Call Load_menu() 
End Sub 

您可以刪除會話變量這種情況。如果此解決方案存在問題,請通過評論或發佈新答案告訴我。

非常感謝您的幫助。