2014-01-24 106 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