Dynamically created button requires 2 clicks to return view state

Posted on 2011-09-14
Last Modified: 2012-06-21
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" "">
 <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
       = "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
Question by:brasso_42
  • 3
  • 2

Expert Comment

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


Author Comment

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



Expert Comment

ID: 36536797
Check the button state in the load event, instead of waiting for the click event.

Author Comment

ID: 36536815
ok how do I do that?

Accepted Solution

Rainverse earned 500 total points
ID: 36537016
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


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Populating an array of locale_time_info 8 37
Jquey and footrable 2 34
Anyway to make "All" the default in the dropdown? 6 34
Need a starter for ETL protocol? 4 33
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now