Dynamically Adding Controls

S

Sandeep

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.Page
Dim ini As Org.Mentalis.Files.IniReader, dirname As String
Dim nxtBtn As Button, prevBtn As Button
Protected WithEvents cmdPrev As System.Web.UI.WebControls.Button
Protected WithEvents cmdNext As System.Web.UI.WebControls.Button
Protected WithEvents plh As System.Web.UI.WebControls.PlaceHolder
'Added by hand
Protected Form1 As System.Web.UI.HtmlControls.HtmlForm
Protected WithEvents cCheck As System.Web.UI.WebControls.CheckBoxList
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
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()
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 = 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.HtmlControls.HtmlTable
Dim row As System.Web.UI.HtmlControls.HtmlTableRow = New HtmlTableRow
Dim btnRow As System.Web.UI.HtmlControls.HtmlTableRow = 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.Name = "Verdana"
cCheck.Font.Size = FontUnit.Parse(12)
cCheck.Font.Bold = True
nxtBtn = New Button
nxtBtn.ID = "cmdNext"
nxtBtn.Text = "Next"
nxtBtn.CommandName = "next"
prevBtn = New Button
prevBtn.Text = "Previous"
prevBtn.CommandName = "prev"
prevBtn.ID = "cmdPrevious"
aPath = Request.ServerVariables("PATH_TRANSLATED")
aPath = Left(aPath, InStrRev(aPath, "\"))
examTitle = Session("ExamTitle")
usrName = Session("userName")
'strLoad="window.opener.close()"
Session.Contents.RemoveAll()
'*****************************************
'For Testing locally on my system
'Session("Title")=Request.FORM("subname")
'*******************************************
'Actual code
'Session("Title")=examTitle
Session("Title") = "IIT"
Session("begin") = 0
Session("resultpage") = "result1.aspx"
Session("userid") = usrName
'*****************************************
Dim fileEntries As String() = System.IO.Directory.GetFiles(aPath & Session("Title"))
If System.IO.Directory.Exists(aPath & Session("Title")) = False Then
Response.Redirect("Error.aspx")
End If
For Each fileName In fileEntries
ini = New Org.Mentalis.Files.IniReader(fileName)
Exit For
Next fileName
Session("SHOW_ENT") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_ENT")
Session("SHOW_MOD") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_MOD")
Session("SHOW_SUB") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_SUB")
Session("SHOW_STO") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_STO")
Session("SHOW_INS") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_INS")
Session("SHOW_PFM") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_PFM")
Session("SHOW_USR") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_USR")
cnt = 0
If Session("SHOW_SUB") = 1 Then
strsub0 = ini.ReadString("SUBJECTS", "SUB0", "SS")
commaPos = InStr(1, strsub0, ",", CompareMethod.Binary)
subname = Mid(strsub0, 1, commaPos - 1)
cCheck.Items.Add(New ListItem(subname, "sub" & cnt))
strsub0 = ini.ReadString("SUBJECTS", "SUB1", "SS")
cnt = cnt + 1
Do While strsub0 <> "SS"
commaPos = InStr(1, strsub0, ",", CompareMethod.Binary)
subname = Mid(strsub0, 1, commaPos - 1)
cCheck.Items.Add(New ListItem(subname, "sub" & cnt))
cnt = cnt + 1
strsub0 = ini.ReadString("SUBJECTS", "SUB" & cnt, "SS")
Loop
End If
Session("begin") = Session("begin") + 1
'cmdPrev.Enabled = False
cell2.InnerText = "Avaliable Subjects"
cell.Align = "left"
cell2.Controls.Add(cCheck)
'row.Cells.Add(cell)
btncell1.Controls.Add(prevBtn)
btncell2.Controls.Add(nxtBtn)
row.Cells.Add(cell2)
btnRow.Cells.Add(btncell1)
btnRow.Cells.Add(btncell2)
cTable.Rows.Add(row)
'cTable.Rows.Add(btnRow)
plh.Controls.Add(cTable)
'AddHandler nxtBtn.Click, AddressOf nxtbtn_click
End If


End Sub
Private Sub cmdNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdNext.Click
Response.Write("Hi You have Clicked Me")
'Error Coming at this line
Response.Write(cCheck.SelectedItem.Text)
End Sub

End Class
 
S

Shock

Sandeep said:
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.Page
Dim ini As Org.Mentalis.Files.IniReader, dirname As String
Dim nxtBtn As Button, prevBtn As Button
Protected WithEvents cmdPrev As System.Web.UI.WebControls.Button
Protected WithEvents cmdNext As System.Web.UI.WebControls.Button
Protected WithEvents plh As System.Web.UI.WebControls.PlaceHolder
'Added by hand
Protected Form1 As System.Web.UI.HtmlControls.HtmlForm
Protected WithEvents cCheck As System.Web.UI.WebControls.CheckBoxList
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
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()
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 = 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.HtmlControls.HtmlTable
Dim row As System.Web.UI.HtmlControls.HtmlTableRow = New HtmlTableRow
Dim btnRow As System.Web.UI.HtmlControls.HtmlTableRow = 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.Name = "Verdana"
cCheck.Font.Size = FontUnit.Parse(12)
cCheck.Font.Bold = True
nxtBtn = New Button
nxtBtn.ID = "cmdNext"
nxtBtn.Text = "Next"
nxtBtn.CommandName = "next"
prevBtn = New Button
prevBtn.Text = "Previous"
prevBtn.CommandName = "prev"
prevBtn.ID = "cmdPrevious"
aPath = Request.ServerVariables("PATH_TRANSLATED")
aPath = Left(aPath, InStrRev(aPath, "\"))
examTitle = Session("ExamTitle")
usrName = Session("userName")
'strLoad="window.opener.close()"
Session.Contents.RemoveAll()
'*****************************************
'For Testing locally on my system
'Session("Title")=Request.FORM("subname")
'*******************************************
'Actual code
'Session("Title")=examTitle
Session("Title") = "IIT"
Session("begin") = 0
Session("resultpage") = "result1.aspx"
Session("userid") = usrName
'*****************************************
Dim fileEntries As String() = System.IO.Directory.GetFiles(aPath &
Session("Title"))
If System.IO.Directory.Exists(aPath & Session("Title")) = False Then
Response.Redirect("Error.aspx")
End If
For Each fileName In fileEntries
ini = New Org.Mentalis.Files.IniReader(fileName)
Exit For
Next fileName
Session("SHOW_ENT") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_ENT")
Session("SHOW_MOD") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_MOD")
Session("SHOW_SUB") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_SUB")
Session("SHOW_STO") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_STO")
Session("SHOW_INS") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_INS")
Session("SHOW_PFM") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_PFM")
Session("SHOW_USR") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_USR")
cnt = 0
If Session("SHOW_SUB") = 1 Then
strsub0 = ini.ReadString("SUBJECTS", "SUB0", "SS")
commaPos = InStr(1, strsub0, ",", CompareMethod.Binary)
subname = Mid(strsub0, 1, commaPos - 1)
cCheck.Items.Add(New ListItem(subname, "sub" & cnt))
strsub0 = ini.ReadString("SUBJECTS", "SUB1", "SS")
cnt = cnt + 1
Do While strsub0 <> "SS"
commaPos = InStr(1, strsub0, ",", CompareMethod.Binary)
subname = Mid(strsub0, 1, commaPos - 1)
cCheck.Items.Add(New ListItem(subname, "sub" & cnt))
cnt = cnt + 1
strsub0 = ini.ReadString("SUBJECTS", "SUB" & cnt, "SS")
Loop
End If
Session("begin") = Session("begin") + 1
'cmdPrev.Enabled = False
cell2.InnerText = "Avaliable Subjects"
cell.Align = "left"
cell2.Controls.Add(cCheck)
'row.Cells.Add(cell)
btncell1.Controls.Add(prevBtn)
btncell2.Controls.Add(nxtBtn)
row.Cells.Add(cell2)
btnRow.Cells.Add(btncell1)
btnRow.Cells.Add(btncell2)
cTable.Rows.Add(row)
'cTable.Rows.Add(btnRow)
plh.Controls.Add(cTable)
'AddHandler nxtBtn.Click, AddressOf nxtbtn_click
End If


End Sub
Private Sub cmdNext_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdNext.Click
Response.Write("Hi You have Clicked Me")
*'Error Coming at this line*
*Response.Write(cCheck.SelectedItem.Text)*
End Sub

End Class

When you do the postback you need to reinitialize your controls or else
they are empty. There are several different ways you can do this.

#1 if (isPostBack)
#2 go recursive and pass the value in the querystring.
#3 use a session variable.
#4 use the viewstate

Shock
 
M

Martin Marinov

When you add dynamically controls in your page you have to recreate them
every time you visit the page ( no matter it is postback or not ). Take a
look at this article of Microsoft , that describes how to add controls
dynamically

http://support.microsoft.com/default.aspx?scid=kb;EN-US;317515


Regards
Martin


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.Page
Dim ini As Org.Mentalis.Files.IniReader, dirname As String
Dim nxtBtn As Button, prevBtn As Button
Protected WithEvents cmdPrev As System.Web.UI.WebControls.Button
Protected WithEvents cmdNext As System.Web.UI.WebControls.Button
Protected WithEvents plh As System.Web.UI.WebControls.PlaceHolder
'Added by hand
Protected Form1 As System.Web.UI.HtmlControls.HtmlForm
Protected WithEvents cCheck As System.Web.UI.WebControls.CheckBoxList
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
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()
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 = 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.HtmlControls.HtmlTable
Dim row As System.Web.UI.HtmlControls.HtmlTableRow = New HtmlTableRow
Dim btnRow As System.Web.UI.HtmlControls.HtmlTableRow = 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.Name = "Verdana"
cCheck.Font.Size = FontUnit.Parse(12)
cCheck.Font.Bold = True
nxtBtn = New Button
nxtBtn.ID = "cmdNext"
nxtBtn.Text = "Next"
nxtBtn.CommandName = "next"
prevBtn = New Button
prevBtn.Text = "Previous"
prevBtn.CommandName = "prev"
prevBtn.ID = "cmdPrevious"
aPath = Request.ServerVariables("PATH_TRANSLATED")
aPath = Left(aPath, InStrRev(aPath, "\"))
examTitle = Session("ExamTitle")
usrName = Session("userName")
'strLoad="window.opener.close()"
Session.Contents.RemoveAll()
'*****************************************
'For Testing locally on my system
'Session("Title")=Request.FORM("subname")
'*******************************************
'Actual code
'Session("Title")=examTitle
Session("Title") = "IIT"
Session("begin") = 0
Session("resultpage") = "result1.aspx"
Session("userid") = usrName
'*****************************************
Dim fileEntries As String() = System.IO.Directory.GetFiles(aPath &
Session("Title"))
If System.IO.Directory.Exists(aPath & Session("Title")) = False Then
Response.Redirect("Error.aspx")
End If
For Each fileName In fileEntries
ini = New Org.Mentalis.Files.IniReader(fileName)
Exit For
Next fileName
Session("SHOW_ENT") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_ENT")
Session("SHOW_MOD") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_MOD")
Session("SHOW_SUB") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_SUB")
Session("SHOW_STO") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_STO")
Session("SHOW_INS") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_INS")
Session("SHOW_PFM") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_PFM")
Session("SHOW_USR") = ini.ReadString("USAGE_CONFIGURATION", "SHOW_USR")
cnt = 0
If Session("SHOW_SUB") = 1 Then
strsub0 = ini.ReadString("SUBJECTS", "SUB0", "SS")
commaPos = InStr(1, strsub0, ",", CompareMethod.Binary)
subname = Mid(strsub0, 1, commaPos - 1)
cCheck.Items.Add(New ListItem(subname, "sub" & cnt))
strsub0 = ini.ReadString("SUBJECTS", "SUB1", "SS")
cnt = cnt + 1
Do While strsub0 <> "SS"
commaPos = InStr(1, strsub0, ",", CompareMethod.Binary)
subname = Mid(strsub0, 1, commaPos - 1)
cCheck.Items.Add(New ListItem(subname, "sub" & cnt))
cnt = cnt + 1
strsub0 = ini.ReadString("SUBJECTS", "SUB" & cnt, "SS")
Loop
End If
Session("begin") = Session("begin") + 1
'cmdPrev.Enabled = False
cell2.InnerText = "Avaliable Subjects"
cell.Align = "left"
cell2.Controls.Add(cCheck)
'row.Cells.Add(cell)
btncell1.Controls.Add(prevBtn)
btncell2.Controls.Add(nxtBtn)
row.Cells.Add(cell2)
btnRow.Cells.Add(btncell1)
btnRow.Cells.Add(btncell2)
cTable.Rows.Add(row)
'cTable.Rows.Add(btnRow)
plh.Controls.Add(cTable)
'AddHandler nxtBtn.Click, AddressOf nxtbtn_click
End If


End Sub
Private Sub cmdNext_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdNext.Click
Response.Write("Hi You have Clicked Me")
'Error Coming at this line
Response.Write(cCheck.SelectedItem.Text)
End Sub

End Class
 
S

Sandeep

Can u tell me with exmaple of the methods suggested by u it would be of
great help to me
 
J

Josh

As Shock said earlier you just have to reload the controls when the page
loads. The viewstate will automaticly set the controls to the right status
 

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,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top