Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamically created button requires 2 clicks to return view state

Posted on 2011-09-14
5
Medium Priority
?
252 Views
Last Modified: 2012-06-21
Hi
 
 
 
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?
 
 
 
Thanks
 
 
 
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">
 
<html
 
xmlns="http://www.w3.org/1999/xhtml">
 <head id="Head1" runat="server">
     <title></title>
 </head>
 <body>
             
    <form id="form1" runat="server">
     <div>
         <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>    
       
    </div>
     </form>
 </body>
 </html>
 
 
 
 
 
VB
 
 
 
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
         Get
             Dim result As String = ViewState.Item(VIEWSTATEKEY_DYNCONTROL)
             If result Is Nothing Then
                 Return String.Empty
             Else
                 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
         PlaceHolder1.Controls.Clear()
 
    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"
                 Me.PlaceHolder1.Controls.Add(Lab)
 
            Case AMEND_SELECTION
                 Dim Lab As New Label
                 Lab.ID = "Tester_2"
                 Lab.Text = "Amend"
                 Me.PlaceHolder1.Controls.Add(Lab)
 
            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(btn_create)
                 Me.PlaceHolder1.Controls.Add(New LiteralControl(" ")) 'space them out
                 Me.PlaceHolder1.Controls.Add(btn_Amend)
                 Me.PlaceHolder1.Controls.Add(New LiteralControl(" ")) 'space them out
                 Me.PlaceHolder1.Controls.Add(Label1)
         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
0
Comment
Question by:brasso_42
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:Rainverse
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,

-MJC
0
 
LVL 1

Author Comment

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

Thanks

Brasso
0
 
LVL 5

Expert Comment

by:Rainverse
ID: 36536797
Check the button state in the load event, instead of waiting for the click event.
0
 
LVL 1

Author Comment

by:brasso_42
ID: 36536815
ok how do I do that?
0
 
LVL 5

Accepted Solution

by:
Rainverse earned 2000 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

-MJC
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

730 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