Hi
I am doing one thing in my website ,actually i want to add controls dynamically to a web form and want to access the
elements but when i postback the form,and access that control, it gives error Object reference not set to an instance of an object.
*************** *************** *************** *************** ************
*************** *************** *************** *************** **************
Public Class index2
Inherits System.Web.UI.P age
Dim ini As Org.Mentalis.Fi les.IniReader, dirname As String
Dim nxtBtn As Button, prevBtn As Button
Protected WithEvents cmdPrev As System.Web.UI.W ebControls.Butt on
Protected WithEvents cmdNext As System.Web.UI.W ebControls.Butt on
Protected WithEvents plh As System.Web.UI.W ebControls.Plac eHolder
'Added by hand
Protected Form1 As System.Web.UI.H tmlControls.Htm lForm
Protected WithEvents cCheck As System.Web.UI.W ebControls.Chec kBoxList
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
End Sub
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceho lderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeCompo nent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
If IsPostBack = False Then
Dim aPath As String, temp As String, strsub0 As String, cnt As Integer
Dim commaPos As Integer, examTitle As String, usrName As String
Dim fileName As String, subname As String, cCheck As CheckBoxList
Dim cTable As New System.Web.UI.H tmlControls.Htm lTable
Dim row As System.Web.UI.H tmlControls.Htm lTableRow = New HtmlTableRow
Dim btnRow As System.Web.UI.H tmlControls.Htm lTableRow = New HtmlTableRow
Dim cell As HtmlTableCell = New HtmlTableCell
Dim cell2 As HtmlTableCell = New HtmlTableCell
Dim btncell1 As HtmlTableCell = New HtmlTableCell
Dim btncell2 As HtmlTableCell = New HtmlTableCell
cCheck = New CheckBoxList
cCheck.ID = "subname"
cCheck.Font.Nam e = "Verdana"
cCheck.Font.Siz e = FontUnit.Parse( 12)
cCheck.Font.Bol d = True
nxtBtn = New Button
nxtBtn.ID = "cmdNext"
nxtBtn.Text = "Next"
nxtBtn.CommandN ame = "next"
prevBtn = New Button
prevBtn.Text = "Previous"
prevBtn.Command Name = "prev"
prevBtn.ID = "cmdPreviou s"
aPath = Request.ServerV ariables("PATH_ TRANSLATED")
aPath = Left(aPath, InStrRev(aPath, "\"))
examTitle = Session("ExamTi tle")
usrName = Session("userNa me")
'strLoad="windo w.opener.close( )"
Session.Content s.RemoveAll()
'************** *************** ************
'For Testing locally on my system
'Session("Title ")=Request.FORM ("subname")
'************** *************** **************
'Actual code
'Session("Title ")=examTitl e
Session("Title" ) = "IIT"
Session("begin" ) = 0
Session("result page") = "result1.as px"
Session("userid ") = usrName
'************** *************** ************
Dim fileEntries As String() = System.IO.Direc tory.GetFiles(a Path & Session("Title" ))
If System.IO.Direc tory.Exists(aPa th & Session("Title" )) = False Then
Response.Redire ct("Error.aspx" )
End If
For Each fileName In fileEntries
ini = New Org.Mentalis.Fi les.IniReader(f ileName)
Exit For
Next fileName
Session("SHOW_E NT") = ini.ReadString( "USAGE_CONFIGUR ATION", "SHOW_ENT")
Session("SHOW_M OD") = ini.ReadString( "USAGE_CONFIGUR ATION", "SHOW_MOD")
Session("SHOW_S UB") = ini.ReadString( "USAGE_CONFIGUR ATION", "SHOW_SUB")
Session("SHOW_S TO") = ini.ReadString( "USAGE_CONFIGUR ATION", "SHOW_STO")
Session("SHOW_I NS") = ini.ReadString( "USAGE_CONFIGUR ATION", "SHOW_INS")
Session("SHOW_P FM") = ini.ReadString( "USAGE_CONFIGUR ATION", "SHOW_PFM")
Session("SHOW_U SR") = ini.ReadString( "USAGE_CONFIGUR ATION", "SHOW_USR")
cnt = 0
If Session("SHOW_S UB") = 1 Then
strsub0 = ini.ReadString( "SUBJECTS", "SUB0", "SS")
commaPos = InStr(1, strsub0, ",", CompareMethod.B inary)
subname = Mid(strsub0, 1, commaPos - 1)
cCheck.Items.Ad d(New ListItem(subnam e, "sub" & cnt))
strsub0 = ini.ReadString( "SUBJECTS", "SUB1", "SS")
cnt = cnt + 1
Do While strsub0 <> "SS"
commaPos = InStr(1, strsub0, ",", CompareMethod.B inary)
subname = Mid(strsub0, 1, commaPos - 1)
cCheck.Items.Ad d(New ListItem(subnam e, "sub" & cnt))
cnt = cnt + 1
strsub0 = ini.ReadString( "SUBJECTS", "SUB" & cnt, "SS")
Loop
End If
Session("begin" ) = Session("begin" ) + 1
'cmdPrev.Enable d = False
cell2.InnerText = "Avaliable Subjects"
cell.Align = "left"
cell2.Controls. Add(cCheck)
'row.Cells.Add( cell)
btncell1.Contro ls.Add(prevBtn)
btncell2.Contro ls.Add(nxtBtn)
row.Cells.Add(c ell2)
btnRow.Cells.Ad d(btncell1)
btnRow.Cells.Ad d(btncell2)
cTable.Rows.Add (row)
'cTable.Rows.Ad d(btnRow)
plh.Controls.Ad d(cTable)
'AddHandler nxtBtn.Click, AddressOf nxtbtn_click
End If
End Sub
Private Sub cmdNext_Click(B yVal sender As Object, ByVal e As System.EventArg s) Handles cmdNext.Click
Response.Write( "Hi You have Clicked Me")
'Error Coming at this line
Response.Write( cCheck.Selected Item.Text)
End Sub
End Class