停止单选按钮更改

时间:2014-01-24 12:15:26

标签: asp.net vb.net vb.net-2010 radiobuttonlist

我有一个调用函数的单选按钮列表。

如果函数返回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

1 个答案:

答案 0 :(得分: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