【问题标题】:Stop radio button change停止单选按钮更改
【发布时间】:2014-02-07 19:22:17
【问题描述】:

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

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

【问题讨论】:

  • 你放断点了吗?不确定 CheckListBox() 正在做什么,但可以多次调用此事件并且您的检查在后续调用中返回 true 吗? CheckListBox() 是否更改了值,可能而不是您粘贴的代码?
  • 好点 - 更新代码!

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


【解决方案1】:

问题是当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

【讨论】:

  • 请告诉我不是这样的……我以为不可能这么简单……基本的故障排除已经发生了。
  • 对不起,即使语句返回 true,它也不会改变值。
  • @user3191666 如果是这样,那么请编辑您的问题并添加CheckListBox()函数的代码,问题可能就在那里。
猜你喜欢
  • 2014-07-08
  • 2012-12-06
  • 2011-12-07
  • 2013-02-20
  • 1970-01-01
  • 2012-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多