Hi Cannot Retain Attributes of Dynamic Controls


Joined
Mar 27, 2011
Messages
1
Reaction score
0
On button click i am creating a dyamic control

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Try
'create a new instance of the control
Dim new_button As Button = New Button()
new_button.ID = txtID.Text
new_button.ForeColor = System.Drawing.Color.FromName(txtForeColor.Text)
new_button.Text = txtText.Text
If (txtAttributes.Text = "onclick") Then
new_button.Attributes.Add("onclick", "alert('hi');")
End If
new_button.EnableViewState = True
'add button to button array
btn_arr(btn_count) = new_button
btn_count = btn_count + 1
'call our add function
lblStatus.Text &= "Created button " & new_button.ID & " and of color " & new_button.ForeColor.ToString()
pnlMain.Controls.Add(new_button)
'add a spacer after the control
pnlMain.Controls.Add(New LiteralControl("<br>"))
'add_button()
Catch ex As Exception
lblStatus.Text += ex.Message.ToString()
End Try
End Sub

And on PageLoad i am re-creating the control but my attributes are getting vanished on page load

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim Count As Integer
Count = 1
If TypeOf btn_arr(0) Is Button Then
'for each button saved in our array, recreate it
For Each button As Button In btn_arr
add_button(button)
If Count = btn_count Then
Exit For
End If
Count = Count + 1
Next button
End If
Catch ex As Exception
lblStatus.Text += ex.Message.ToString()
End Try


Protected Sub add_button(ByVal new_button As Button)
Try

'add to a container on the page
pnlMain.Controls.Add(new_button)
'add a spacer after the control
pnlMain.Controls.Add(New LiteralControl("<br>"))
Catch ex As Exception
lblStatus.Text += ex.Message.ToString()
End Try
End Sub

Please help me...
 
Ad

Advertisements


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

Top