i create run time control like this
Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged
Dim index As Integer
For index = 1 To RadioButtonList1.SelectedValue
Dim name_tb As New TextBox()
'validator
Dim name_req As New RequiredFieldValidator
name_tb.ID = "name_tb" & index
name_req.ID = "name_req" & index
name_tb.TextMode = TextBoxMode.SingleLine
tabelcell.Controls.Add(name_tb)
'tabelcell add in trow
'trow then added in htmltable
div.Controls.Add(htmltable)
Me.PlaceHolder1.Controls.Add(div)
next
I need to get textbox text when i click on submitt button on form.
Thanks
Loading
Madhu KPosted Nov 22, 2010, 12:20 AM
For Each textbox As System.Web.UI.Control In cell.Controls.Count
Dim tbox As TextBox = CType(textbox, System.Web.UI.WebControls.TextBox)
erum mirzaPosted Nov 19, 2010, 9:33 AM
have a look on code behind submit button
dim plholder As PlaceHolder = DirectCast(FindControl("PlaceHolder1"), PlaceHolder)
For Each div As System.Web.UI.Control In plholder.Controls
If div.[GetType]().Name = "HtmlGenericControl" Then
Dim hml As HtmlGenericControl = DirectCast(div, HtmlGenericControl)
Dim str12 As String
str12 = hml.TagName
str12 = hml.ID
For Each table As System.Web.UI.Control In div.Controls
If table.[GetType]().Name = "HtmlTable" Then
Dim htab As HtmlTable = DirectCast(table, HtmlTable)
Dim str13 As String
str13 = htab.TagName
str13 = htab.ID
For Each row As System.Web.UI.Control In table.Controls
Dim hrow As HtmlTableRow = DirectCast(row, HtmlTableRow)
Dim str14 As String
str14 = hrow.TagName
str14 = hrow.ID
For Each cell As System.Web.UI.Control In row.Controls
Dim hcell As HtmlTableCell = DirectCast(cell, HtmlTableCell)
Dim str15 As String
str15 = hcell.TagName
str15 = hcell.ID
For Each textbox As System.Web.UI.Control In cell.Controls
Dim tbox As TextBox = CType(textbox, System.Web.UI.WebControls.TextBox)
Dim str16 As String
str16 = tbox.TextMode
str16 = tbox.ID
Next 'textbox
Next 'cell
Next 'row
End If
Next 'table
'here you can access inner controls of html
'If str12.ToLower() = "div" Then
' Response.Write(str12)
' 'Else If
'End If
End If
next
but i m getting an error
Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'.
on line
Dim tbox As TextBox = CType(textbox, System.Web.UI.WebControls.TextBox) in above code
Madhu KPosted Nov 18, 2010, 7:46 AM
http://forums.asp.net/t/1186195.aspx
erum mirzaPosted Nov 18, 2010, 6:34 AM
Madhu KPosted Nov 18, 2010, 6:31 AM
erum mirzaPosted Nov 18, 2010, 6:21 AM
Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged
Dim index As Integer
For index = 1 To RadioButtonList1.SelectedValue
Dim newdiv As New HtmlGenericControl("div")
newdiv.ID = "mydiv"
'newdiv.Attributes.Add("id", "mydiv")
newdiv.EnableViewState = True
Dim ht As New HtmlTable()
ht.ID = "mytable" & index.ToString()
ht.EnableViewState = True
ht.Width = "100%"
ht.CellSpacing = "0"
ht.CellPadding = "0"
ht.Border = 0
Dim htr, htr1, htr2 As New HtmlTableRow()
Dim cell, htc, htc1, htc2, htc3, htc4, htc5, htc6, htc7, htc8 As New HtmlTableCell()
Dim name_tb As New TextBox()
Dim sal_ddl As New DropDownList
'validator
Dim name_req As New RequiredFieldValidator
Dim tel_req As New RequiredFieldValidator
Dim email_req As New RequiredFieldValidator
name_tb.ID = "name_tb" & index
sal_ddl.ID = "sal_ddl" & index
name_req.ID = "name_req" & index
tel_req.ID = "tel_req" & index
email_req.ID = "email_req" & index
name_tb.EnableViewState = True
sal_ddl.EnableViewState = True
tel_req.EnableViewState = True
sal_ddl.Items.Add("MR")
sal_ddl.Items.Add("Ms")
sal_ddl.Items.Add("Mrs")
Dim Tel_tb1 As New TextBox
Tel_tb1.ID = "Tel_tb1" & index
Tel_tb1.EnableViewState = True
name_tb.TextMode = TextBoxMode.SingleLine
Dim email_tb1 As New TextBox
email_tb1.ID = "email_tb1" & index
email_tb1.TextMode = TextBoxMode.SingleLine
htc.InnerText = "Name : "
htc.Width = "20%"
htc.Align = "Left"
htc1.Controls.Add(sal_ddl)
htc1.Width = "10%"
htc2.Width = "70%"
htc2.Align = "left"
htc2.Controls.Add(name_tb)
htc2.Controls.Add(name_req)
htr.Controls.Add(htc)
htr.Controls.Add(htc1)
htr.Controls.Add(htc2)
ht.Controls.Add(htr)
name_req.ControlToValidate = "name_tb" & index
name_req.Display = ValidatorDisplay.Dynamic
name_req.ErrorMessage = ("Please Enter name of attendenes")
'make 2nd row
htc3.InnerText = "Telephone : "
htc3.Width = "20%"
htc3.Align = "Left"
htc4.InnerHtml = ""
htc4.Width = "10%"
htc5.Width = "70%"
htc5.Align = "left"
htc5.Controls.Add(Tel_tb1)
htc5.Controls.Add(tel_req)
htr1.Controls.Add(htc3)
htr1.Controls.Add(htc4)
htr1.Controls.Add(htc5)
ht.Controls.Add(htr1)
tel_req.ControlToValidate = "Tel_tb1" & index
tel_req.Display = ValidatorDisplay.Dynamic
tel_req.ErrorMessage = ("Please Enter Phone")
'make 3rd row
htc6.InnerText = "Email : "
htc6.Width = "20%"
htc6.Align = "Left"
htc7.InnerHtml = ""
htc7.Width = "10%"
htc8.Width = "70%"
htc8.Align = "left"
htc8.Controls.Add(email_tb1)
htc8.Controls.Add(email_req)
htr2.Controls.Add(htc6)
htr2.Controls.Add(htc7)
htr2.Controls.Add(htc8)
ht.Controls.Add(htr2)
email_req.ControlToValidate = "email_tb1" & index
email_req.Display = ValidatorDisplay.Dynamic
email_req.ErrorMessage = ("Please Enter Email")
'Add a blank rows
cell.ColSpan = 3
cell.InnerHtml = "
"
Dim row As New HtmlTableRow
row.Cells.Add(cell)
ht.Rows.Add(row)
newdiv.Controls.Add(ht)
newdiv.Attributes.Add("table", "ht")
newdiv.Attributes.Add("Style", "border-bottom :#ccc solid 0px")
newdiv.Attributes.Add("style", "height:23px")
newdiv.Attributes.Add("style", "padding-left:20px")
Me.PlaceHolder1.Controls.Add(newdiv)
PlaceHolder1.EnableViewState = True
Next
'End Sub
End Sub
and here is the code behind submiit button where i want to retrive the div
For index = 1 To RadioButtonList1.SelectedValue
Dim plholder As PlaceHolder = DirectCast(FindControl("PlaceHolder1"), PlaceHolder)
'Dim name_tb As TextBox = DirectCast(plholder.FindControl("name_tb" & index.ToString()), TextBox)
Dim newdiv As HtmlGenericControl = DirectCast(FindControl("mydiv"), HtmlGenericControl)
Response.Write(newdiv.ID)
Next
Madhu KPosted Nov 18, 2010, 5:13 AM
erum mirzaPosted Nov 18, 2010, 5:03 AM
i have code like this while adding div
Dim newdiv As New HtmlGenericControl("div")
newdiv.ID = "mydiv"
and while getting i have
Dim plholder As PlaceHolder = DirectCast(FindControl("PlaceHolder1"), PlaceHolder) //work fine
Dim newdiv As HtmlGenericControl = DirectCast(FindControl("mydiv"), HtmlGenericControl)
Response.Write(newdiv.ID)
//this gives above mentioned exception
any help
Madhu KPosted Nov 18, 2010, 4:57 AM
HtmlGenericControl div = new HtmlGenericControl("div");
div.ID = "div12";
plcDiv.Controls.Add(div);
//Button click event
Dim div As HtmlGenericControl = DirectCast(FindControl("div12"), HtmlGenericControl)
erum mirzaPosted Nov 18, 2010, 4:36 AM
for example i got placeholder like this way
Dim plholder As PlaceHolder = DirectCast(FindControl("PlaceHolder1"), PlaceHolder)
but what about div??
Madhu KPosted Nov 18, 2010, 3:59 AM
erum mirzaPosted Nov 18, 2010, 12:03 AM
placeholder ,div,htmltable,row,tablecell ,textbox,
right now i m facing problem with how to find div dynamically added in placeholder,
i added it
Dim newdiv As New HtmlGenericControl("div")
Me.PlaceHolder1.Controls.Add(newdiv)
i need to find it on submitt button
Sam HobbsPosted Nov 14, 2010, 11:28 PM
Also, you need to fix the numerous syntax errors in your code; the C# compiler will choke on it.