Loop through a Plain Radio Button in ASP.NET

B

Brian Ciarcia

Ok.. It seems that when I added the button control and the label
control, the code that I used to enable the view state of the panels
stopped working.. As soon as I take them away, it works... Why is that
happening?
 
S

Scott M.

I'm not sure I'm following what is happening at this point, but you want to
make sure that the first panel has its visibility set to True right from the
start so that the user will get the first set of questions right away.
 
B

Brian Ciarcia

I created my panels and set them to be hidden by default. When the page
loads, it checks to see what panels they should see and makes them
visible. Everything worked great with the code you gave me.. I then
changed the regular submit button to an asp:button and added an
asp:label. As soon as I added that, the code that makes the panels
visible, stopped working.. If I change the asp:button back and get rid
of the lable... it works fine..
 
B

Brian Ciarcia

URGHHH... I give up... Now I can't even get it to work no matter what I
try.. Here is my codebehind.. Please tell me what is wrong....

----------------------------------------------------------



Public Class Questions2 : Inherits Page


Protected WithEvents CAD As System.Web.UI.WebControls.panel
Protected WithEvents CHF As System.Web.UI.WebControls.panel
Protected WithEvents HB As System.Web.UI.WebControls.panel
Protected WithEvents DBTS As System.Web.UI.WebControls.panel





Public Sub Page_Load(Source As Object, E As System.EventArgs)

if NOT Page.IsPostback Then

GetQuestions()
end if





End Sub





'///////////////////////////////////////////////////////////////////////
///////////
'////////////////////////////// QUESTIONNAIRE QUERY
//////////////////////////////
'///////////////////////////////////////////////////////////////////////
//////////

public Sub GetQuestions()


Dim strbarConn as string =
System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_ST
RING_cardmemberoledb")
Dim strbarSQL as String = "SELECT ProgramID, QuestionHTML from
SponsorQuestions where SponsorID = '" & session("SponsorCode") & "'
order by QuestionCode"
Dim objBarConnection as New OledbConnection(strbarConn)
Dim objBarCommand as New OledbCommand(strBarSQL, objBarConnection)
Dim objBarDataReader as OledbDatareader
objBarConnection.open()
objBarDataReader = objBarCommand.ExecuteReader()
Do While objBarDataReader.Read()=True
' response.write(strbarSQL)


Dim ctl as Control

For Each ctl In Page.Controls

Response.write(ctl)
If TypeOf (ctl) Is System.Web.UI.WebControls.Panel Then

If ctl.ID = objBarDataReader("ProgramID") Then
ctl.visible = True

End If
End If
Next

Loop
objBarDataReader.Close()
objBarConnection.Close()
End Sub



End Class
 
S

Scott M.

Well, I see that in your Page_Load event you aren't doing anything when it
IS a postback. So it would seem that on the first page call you'd get the
page and after that, you'd get absolutely nothing. Make sure to either turn
off the ViewState (EnableViewState property) for each panel so that they
always start out being invisible or add code to your IsPostBack that shuts
off the last panel.

I think it should be (I've also cleaned up your code a bit):
---------------------------------------------------------------------------
Imports System.Configuration
Imports System.Data.OleDB

Public Class Questions2

Protected WithEvents CAD As System.Web.UI.WebControls.panel
Protected WithEvents CHF As System.Web.UI.WebControls.panel
Protected WithEvents HB As System.Web.UI.WebControls.panel
Protected WithEvents DBTS As System.Web.UI.WebControls.panel
---------------------------------------------------------------------------
Public Sub Page_Load(Source As Object, E As System.EventArgs)
If Not IsPostBack Then
'Page is loading for first time
'Show whatever the user is supposed to see on the first page load

Else
'Page is loading in response to the form being submitted
'Figure out which questions to show them next
GetQuestions()
End If
End Sub
---------------------------------------------------------------------------
Public Sub GetQuestions()
Dim strbarConn as string = _
ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_cardmemberoledb")
Dim strbarSQL as String = "SELECT ProgramID, QuestionHTML " & _
"FROM SponsorQuestions WHERE
SponsorID = '" & _
Session("SponsorCode") & "'
ORDER BY QuestionCode"
Dim objBarConnection as New OledbConnection(strbarConn)
Dim objBarCommand as New OledbCommand(strBarSQL, objBarConnection)
Try
objBarConnection.open()
Dim objBarDataReader as OledbDatareader =
objBarCommand.ExecuteReader()
Do While objBarDataReader.Read
' response.write(strbarSQL)
Dim ctl as Control
For Each ctl In Controls
Response.Write(ctl)
If TypeOf (ctl) Is Panel Then
If ctl.ID = objBarDataReader("ProgramID") Then
ctl.visible = True
End If
End If
Next
Loop
Catch ex As OleDBException
'Handle exceptions here

Finally
objBarDataReader.Close()
objBarConnection.Close
End Try
End Sub
 
B

Brian Ciarcia

Now I get this error...

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Source Error:


Line 399: Finally
Line 400: objBarDataReader.Close()<---------
Line 401: objBarConnection.Close
Line 402: End Try


Line 400 is the problem...
 
B

Brian Ciarcia

Nevermind.. Dumb error on my part... However, It still doesn't seem to
be seeing the panels as controls Here is the code.. I put some
response.writes in their to confirm the query is running right and to
see what ctls are being seen..

---------------------------------------------------------
Dim ctl as Control
For Each ctl In Controls
Response.Write("the control is " )
response.write(ctl)
response.write(" <BR> TheID is " & objBarDataReader("ProgramID") &
"<BR> the sessionID is " & session("SponsorCode"))
If TypeOf (ctl) Is Panel Then
If ctl.ID = objBarDataReader("ProgramID") Then
ctl.visible = True
else
ctl.visible = false
End If
End If
Next

------------------------------------------------------

Here is the result of the response.writes..

-------------------------------------------------

the control is ASP.header_ascx
TheID is CAD
the sessionID is PB

the control is System.Web.UI.HtmlControls.HtmlForm
TheID is CAD
the sessionID is PB

the control is ASP.header_ascx
TheID is CHF
the sessionID is PB

the control is System.Web.UI.HtmlControls.HtmlForm
TheID is CHF
the sessionID is PB

the control is ASP.header_ascx
TheID is DBTS
the sessionID is PB

the control is System.Web.UI.HtmlControls.HtmlForm
TheID is DBTS
the sessionID is PB
 
S

Scott M.

The reason why its not working is that the controls (panels) aren't being
added to the form's conrols collection as Panel controls, they are being
added as HTMLLiteral controls, so the loop is never finding them.

You need to add the panel controls to the form's controls collection maually
so they will be treated as Panel controls when you loop through the controls
collection.

I have changed the code a little and it works for me (this assumes there are
3 panels called Panel1, Panel2 and Panel3).

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Controls.Add(Panel1)
Me.Controls.Add(Panel2)
Me.Controls.Add(Panel3)
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
Dim pnl As Object
For Each pnl In Controls
If pnl.GetType Is GetType(System.Web.UI.WebControls.Panel) Then
Label1.Text += "<BR>" & CType(pnl,
System.Web.UI.WebControls.Panel).ID
End If
Next
End Sub

-Scott
 
B

Brian Ciarcia

Sorry man.. Its still not working.. I dont understand... I created the
label named label1 and its not even seeing that to populate the label
with the information.. the only thing it sees is the form itself and the
header that i have..
 
S

Scott M.

The code I gave you works for me. You must not be following it or you must
have additional code in there that is interfering. Try this:

1. Make a new Web Form in your project.
2. Set it as the Start Page for the project.
3. Add 3 panel controls on the page (don't change their ID's, leave them as
Panel1...Panel3)
4. Add 1 label on the page (don't change it's ID), but clear out its text
property
5. Add this into the Web Form Designer Generated Code Sub Initialize:
Me.Controls.Add(Panel1)
Me.Controls.Add(Panel2)
Me.Controls.Add(Panel3)


6. Make this your Page_Load event:

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
Dim pnl As Object
For Each pnl In Controls
If pnl.GetType Is GetType(System.Web.UI.WebControls.Panel) Then
Label1.Text += "<BR>" & CType(pnl,
System.Web.UI.WebControls.Panel).ID
End If
Next
End Sub

7. Run the project.
8. After you get this working, you can begin to tailor it to your specific
needs.


If you take just the code I gave you in the last post
 
B

Brian Ciarcia

I'm doing this in Dreamweaver, but I will try and create a new page,
using this code.. I didn't change the code you gave me.. I did take that
label and move it outside the form and then it magically showed up. It
has to be something with the form.. I just dont know what it is...
 
S

Scott M.

Ah, well, more information is coming out!

How can you be doing this in DreamWeaver? You can't and you shouldn't. You
need to be using VS.NET.

DreamWeaver is the source of the "now you see it, now you don't" problems
that you are having.
 
S

Scott M.

I know that the latest version of DreamWeaver says it supports ASP.NET, but
take it from me, stay as far away from DreamWeaver for .NET development as
you can.

Why not use the free WebMatrix ASP.NET development tool (http://asp.net) if
you don't have VS.NET. And, if you DO have VS.NET, that is most definitely
where you should be working.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top