Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Hi Cannot Retain Attributes of Dynamic Controls (http://www.velocityreviews.com/forums/t745783-hi-cannot-retain-attributes-of-dynamic-controls.html)

milan04 03-27-2011 07:54 AM

Hi Cannot Retain Attributes of Dynamic Controls
 
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...


All times are GMT. The time now is 03:01 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57