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
    Likes Received:
    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...
     
    milan04, Mar 27, 2011
    #1
    1. Advertisements

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,874
    Steve C. Orr [MVP, MCSD]
    Jul 6, 2004
  2. =?Utf-8?B?VGVyb3M=?=

    Dynamic Variables? OR Dynamic Controls

    =?Utf-8?B?VGVyb3M=?=, Aug 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    585
    Scott Allen
    Aug 10, 2004
  3. mytestemailaccount@gmail.com
    Replies:
    5
    Views:
    5,798
    mytestemailaccount@gmail.com
    Mar 23, 2005
  4. Ronald
    Replies:
    2
    Views:
    1,055
    Robbe Morris [C# MVP]
    Jan 17, 2006
  5. =?Utf-8?B?U3RldmUgRnVuaw==?=

    Dynamic Wizard with Dynamic Validation Controls

    =?Utf-8?B?U3RldmUgRnVuaw==?=, Oct 13, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    558
    =?Utf-8?B?U3RldmUgRnVuaw==?=
    Oct 13, 2006
  6. 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:
    658
    Eidolon
    May 4, 2004
  7. 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:
    224
    DCV via .NET 247
    Jun 17, 2004
  8. Diego
    Replies:
    4
    Views:
    307
    Diego
    Jun 21, 2004
Loading...