Dynamically created button requires 2 clicks to return view state

I'm trying to create a page dynamically, but I falling at the first herdle.  I have create the below after reading may blogs/post/articles.  The problem I have is when I click the first buttons I created nothing happend, if I then click it again it works.
Can any one tell me where I'm going wrong?
ASP Page:
<%@ Page Title="Create / Amend Quotes" Language="vb" AutoEventWireup="false" CodeBehind="Quote.aspx.vb" Inherits="BIS.Quote" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <head id="Head1" runat="server">
    <form id="form1" runat="server">
         <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>    
Imports System.Data
 Imports System.Data.SqlClient
 Public Class Quote
     Inherits System.Web.UI.Page
    Const CREATE_SELECTION As String = "create"
     Const AMEND_SELECTION As String = "amend"
     Const VIEWSTATEKEY_DYNCONTROL As String = "VS_Controls"
    Protected Property VS_Controls() As String
             Dim result As String = ViewState.Item(VIEWSTATEKEY_DYNCONTROL)
             If result Is Nothing Then
                 Return String.Empty
                 Return result
             End If
         End Get
         Set(ByVal value As String)
             ViewState.Item(VIEWSTATEKEY_DYNCONTROL) = value
         End Set
    End Property
    Private Sub Quote_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    End Sub
    Private Sub Quote_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Select Case Me.VS_Controls
             Case CREATE_SELECTION
                 Dim Lab As New Label
                 Lab.ID = "Tester"
                 Lab.Text = "Create"
            Case AMEND_SELECTION
                 Dim Lab As New Label
                 Lab.ID = "Tester_2"
                 Lab.Text = "Amend"
            Case Else
                 Dim btn_create As New Button
                 Dim btn_Amend As New Button
                 Dim Label1 As New Label
                btn_create.ID = "but_Create"
                 btn_create.Text = "Create"
                 btn_create.CommandName = "Create"
                 AddHandler btn_create.Click, AddressOf onClick
                btn_Amend.ID = "but_Amend"
                 btn_Amend.Text = "Amend"
                 btn_Amend.CommandName = "Amend"
                 AddHandler btn_Amend.Click, AddressOf onClick
                Label1.id = "lab1"
                Label1.Text = VS_Controls
                 Me.PlaceHolder1.Controls.Add(New LiteralControl(" ")) 'space them out
                 Me.PlaceHolder1.Controls.Add(New LiteralControl(" ")) 'space them out
         End Select
    End Sub
    Private Sub onClick(ByVal sender As Object, ByVal e As EventArgs)
        Dim From As Button = DirectCast(sender, Button)
         Dim But_String As String = From.ID
        If But_String = "but_Create" Then
             Me.VS_Controls = CREATE_SELECTION
             ' Me.VS_Controls = "create"
             ' ViewState("create_amend") = "create"
         ElseIf But_String = "but_Amend" Then
             Me.VS_Controls = AMEND_SELECTION
             'ViewState("create_amend") = "amend"
         End If
    End Sub
    Private Sub Quote_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
    End Sub
 End Class
Who is Participating?
RainverseConnect With a Mentor Commented:
Try something like this:

Select Case Request.Form("__EVENTTARGET").ToString()
      Case "but_Create"
            ' Do code for Create
      Case "but_Amend"
            ' Do code for Amend
End Select

You have to create the controls on every load, within the load.  The click even comes after the Load in the page life-cycle,

brasso_42Author Commented:
Ok so how can I achive dynamically created controls based on what a user has select on the page?


Check the button state in the load event, instead of waiting for the click event.
brasso_42Author Commented:
ok how do I do that?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.