SLE said:
A web user control exposes a RadioButtonList (with AutoPostBack = true)
called "Question". This control also has an embedded RequiredFieldValidator
control.
1. Main ASPX page is populated with several "Questions".
2. Within the code of the main page, there is a SelectedIndexChanged handler
for the RadioButtonList exposed by Question. The function loops through a
number of questions on the pages and hides/shows questions (control.visible
property) depending on answers of other questions.
All works fine until I press the submit button which forces a
Me.Validate() - if there are any errors because of unanswered questions, the
above system no longer works.
If I disable "SmartNavigation" everyhting works fine but I need it because
of the postbacks which cause unacceptable scrolling (d)effects...
This is a repro. The following simple example creates some questions (via a
web user control) on the main ASPX page. It demonstrates what I want to do,
i.e. if the answer to question 5 equals "1", question 6 should disappear.
Everything works fine until the submit button is invoked. From then on, I
cannot hide questions (though .Visible = False is stepped into). When
turning off smartnavigation, it works. Having smartnavigation without the
required field validator control works fine too...
1) WebForm1.aspx.vb:
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents ValidationSummary1 As
System.Web.UI.WebControls.ValidationSummary
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents PlaceHolder1 As System.Web.UI.WebControls.PlaceHolder
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
Me.SmartNavigation = True
Dim i As Integer
Dim c As UserControl
For i = 1 To 10
c = New UserControl()
c = CType(LoadControl("WebUserControl1.ascx"), WebUserControl1)
CType(c, WebUserControl1).InitQuestion(i)
FindControl("PlaceHolder1").Controls.Add(c)
Next
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If IsPostBack Then
Dim c As UserControl
For Each c In FindControl("PlaceHolder1").Controls
If CType(c, WebUserControl1).QID = 5 Then
If CType(c, WebUserControl1).RadioButtonList.SelectedIndex = 0 Then
GetUserControl(6).Visible = False
Else
GetUserControl(6).Visible = True
End If
End If
Next
End If
End Sub
Private Function GetUserControl(ByVal questionID As Integer) As
WebUserControl1
Dim c As Control
For Each c In Me.FindControl("PlaceHolder1").Controls
If CType(c, WebUserControl1).QID = questionID Then
Return CType(c, WebUserControl1)
End If
Next
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.Validate()
If Me.IsValid Then
Response.Redirect("about:blank")
End If
End Sub
End Class
2) WebUserControl1.ascx.vb:
Public MustInherit Class WebUserControl1
Inherits System.Web.UI.UserControl
Protected WithEvents Panel1 As System.Web.UI.WebControls.Panel
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents RequiredFieldValidator1 As
System.Web.UI.WebControls.RequiredFieldValidator
Protected WithEvents RadioButtonList1 As
System.Web.UI.WebControls.RadioButtonList
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
Me.RadioButtonList1.AutoPostBack = True
End Sub
#End Region
Private mintQID As Integer
Public ReadOnly Property RadioButtonList() As RadioButtonList
Get
Return RadioButtonList1
End Get
End Property
Public ReadOnly Property QID() As Integer
Get
Return mintQID
End Get
End Property
Public Sub InitQuestion(ByVal QID As Integer)
mintQID = QID
Label1.Text = "Question " & mintQID.ToString()
With RadioButtonList1.Items
.Add("1")
.Add("2")
.Add("3")
End With
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
End Class