2014-01-24 99 views
1

我有調用功能的單選按鈕列表。停止單選按鈕更改

如果該函數返回true,那麼我想更改該值。

但是,如果該函數返回false,那麼我不想更改該值並保留原始選擇值。

目前它即使在語句返回false時也會更改該值。

有什麼建議嗎?

ASP頁

<asp:RadioButtonList ID="rblType" runat="server" AutoPostBack="True" 
    DataSourceID="SqlData" DataTextField="Type" 
    DataValueField="TypeID"> 
</asp:RadioButtonList> 

VB文件

Private selectionvalue As Integer 

Protected Sub rblType_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles rblType.SelectedIndexChanged 

    Dim CheckListValidation As Boolean = CheckListBox() 

    If CheckListValidation = True Then 
      selectionvalue = rblType.SelectedItem.Value 
     Else 
      rblType.SelectedItem.Value = selectionvalue 
    End If 

End Sub 

Function CheckListBox() As Boolean 

    If lstbox.Items.Count <> "0" Then 
     If MsgBox("Are you sure you want to change option?", MsgBoxStyle.YesNo, " Change Type") = MsgBoxResult.Yes Then 
      Return True 
     Else 
      Return False 
     End If 
    Else 
     Return True 
    End If 

End Function 
+0

你已經把一個斷點?不確定CheckListBox()在做什麼,但是這個事件是否會被多次調用,並且您的支票在後續的調用中返回true? CheckListBox()是否更改了值,而不是你粘貼的代碼? –

+0

好點 - 更新代碼! – user3191666

回答

3

的問題是在執行rblType_SelectedIndexChanged,所選擇的項目已經改變,RadioButtonList不 「記住」先前選定的值。您需要在回發之間保留之前選定的值以實現此目的。

我會建議使用ViewState。創建代碼隱藏的屬性來表示的ViewState值:

Private Property PreviousSelectedValue() As String 
    Get 
     If (ViewState("PreviousSelectedValue") Is Nothing) Then 
      Return String.Empty 
     Else 
      Return ViewState("PreviousSelectedValue").ToString() 
     End If 
    End Get 
    Set(ByVal value As String) 
     ViewState("PreviousSelectedValue") = value 
    End Set 
End Property 

rblType_SelectedIndexChanged

Protected Sub rblType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rblType.SelectedIndexChanged 

    Dim CheckListValidation As Boolean = CheckListBox() 

    If CheckListValidation = True Then 
     'save the currently selected value to ViewState 
     Me.PreviousSelectedValue = rblType.SelectedValue 
    Else 
     'get the previously selected value from ViewState 
     'and change the selected radio button back to the previously selected value 
     If (Me.PreviousSelectedValue = String.Empty) Then 
      rblType.ClearSelection() 
     Else 
      rblType.SelectedValue = Me.PreviousSelectedValue 
     End If 
    End If 

End Sub 
+0

請告訴我這不是它......我認爲它不可能是這麼簡單......基本的故障排除發生了。 –

+0

對不起,即使語句返回true,它也不會改變值。 – user3191666

+0

@ user3191666如果是這樣的話,那麼請編輯你的問題並添加'CheckListBox()'函數的代碼,問題可能在那裏。 – ekad