Hi Cannot Retain Attributes of Dynamic Controls

Discussion in '.NET' started by milan04, Mar 27, 2011.

  1. milan04

    milan04

    Joined:
    Mar 27, 2011
    Messages:
    1
    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...
    milan04, Mar 27, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    2
    Views:
    2,700
    Steve C. Orr [MVP, MCSD]
    Jul 6, 2004
  2. Ronald
    Replies:
    2
    Views:
    822
    Robbe Morris [C# MVP]
    Jan 17, 2006
  3. Denny Smolinski via .NET 247

    Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls

    Denny Smolinski via .NET 247, May 4, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    453
    Eidolon
    May 4, 2004
  4. DCV via .NET 247

    assign dynamic attributes in web controls in aspx file

    DCV via .NET 247, Jun 17, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    133
    DCV via .NET 247
    Jun 17, 2004
  5. Diego
    Replies:
    4
    Views:
    196
    Diego
    Jun 21, 2004
Loading...

Share This Page