Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Dynamic control issue driving me nuts

Dynamic control issue driving me nuts

Scheduled Pinned Locked Moved ASP.NET
helpdesignworkspace
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    andyk1967
    wrote on last edited by
    #1

    I can't take it any longer... Can someone pleaseeeee help me. I have included my code below. I have searched everything and read many articles but I still don't get it. So please no reaction in the sense; you must create your controls in page_load, you must recreate your controls in the onpageinit, preserve viewstate, etc. because I have seen all those matters, many many times. My guess is that I just miss a tiny bit... On a webform I have a placeholder and in codebehind this placeholder is filled with 2 dropdownlists and two buttons. The two lists are filled with data from a dbase. When a user clicks one button an item is transfered from one list to the other, vice versa. What happens in code below is that I have to click a button twice to have an item move. Actually the first time when I click the button the item is transfered but the list wont refresh, while I regenerate the listboxes in my on page init (i tried the on postback as well). When I do a response.redirect to the same page it works fine but that shouldn't be the way to go. Imports System.Data.SqlClient Imports System.Data Partial Class Beheer_viewstate Inherits System.Web.UI.Page Dim bID As Integer Sub Page_Init(ByVal s As Object, ByVal e As EventArgs) Handles MyBase.Init Session("boekingID") = 105 bID = 105 dynamiccontent() End Sub Sub dynamiccontent() Dim conn As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("MyProvider")) conn.Open() 'ongeboekte extras per reiziger Dim get_boeker_ongeboekte_extras As New SqlCommand("get_boeker_ongeboekte_extras", conn) get_boeker_ongeboekte_extras.CommandType = CommandType.StoredProcedure get_boeker_ongeboekte_extras.Parameters.AddWithValue("@boekerID", bID) get_boeker_ongeboekte_extras.Parameters.AddWithValue("@boekingID", Session("boekingID")) Dim rdr_get_boeker_ongeboekte_extras As SqlDataReader = get_boeker_ongeboekte_extras.ExecuteReader() Dim extras As New ListBox extras.ID = "lbx_extras" & bID extras.DataSource = rdr_get_boeker_ongeboekte_extras extras.DataValueField = "extraID" extras.DataTextField = "naam" extras.EnableViewState = "true" extras.DataBind() rdr_get_boeker_ongeboekte_extras.Close() 'geboekte extras Dim get_boeker_geboekte_extras As New SqlCommand("get_boeker_geboekte_extras", conn) get_boeker_geboekte_extras.CommandType = CommandType.

    M 1 Reply Last reply
    0
    • A andyk1967

      I can't take it any longer... Can someone pleaseeeee help me. I have included my code below. I have searched everything and read many articles but I still don't get it. So please no reaction in the sense; you must create your controls in page_load, you must recreate your controls in the onpageinit, preserve viewstate, etc. because I have seen all those matters, many many times. My guess is that I just miss a tiny bit... On a webform I have a placeholder and in codebehind this placeholder is filled with 2 dropdownlists and two buttons. The two lists are filled with data from a dbase. When a user clicks one button an item is transfered from one list to the other, vice versa. What happens in code below is that I have to click a button twice to have an item move. Actually the first time when I click the button the item is transfered but the list wont refresh, while I regenerate the listboxes in my on page init (i tried the on postback as well). When I do a response.redirect to the same page it works fine but that shouldn't be the way to go. Imports System.Data.SqlClient Imports System.Data Partial Class Beheer_viewstate Inherits System.Web.UI.Page Dim bID As Integer Sub Page_Init(ByVal s As Object, ByVal e As EventArgs) Handles MyBase.Init Session("boekingID") = 105 bID = 105 dynamiccontent() End Sub Sub dynamiccontent() Dim conn As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("MyProvider")) conn.Open() 'ongeboekte extras per reiziger Dim get_boeker_ongeboekte_extras As New SqlCommand("get_boeker_ongeboekte_extras", conn) get_boeker_ongeboekte_extras.CommandType = CommandType.StoredProcedure get_boeker_ongeboekte_extras.Parameters.AddWithValue("@boekerID", bID) get_boeker_ongeboekte_extras.Parameters.AddWithValue("@boekingID", Session("boekingID")) Dim rdr_get_boeker_ongeboekte_extras As SqlDataReader = get_boeker_ongeboekte_extras.ExecuteReader() Dim extras As New ListBox extras.ID = "lbx_extras" & bID extras.DataSource = rdr_get_boeker_ongeboekte_extras extras.DataValueField = "extraID" extras.DataTextField = "naam" extras.EnableViewState = "true" extras.DataBind() rdr_get_boeker_ongeboekte_extras.Close() 'geboekte extras Dim get_boeker_geboekte_extras As New SqlCommand("get_boeker_geboekte_extras", conn) get_boeker_geboekte_extras.CommandType = CommandType.

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      andyk1967 wrote:

      My guess is that I just miss a tiny bit...

      What you miss is the control life cycle, or more exactly the order of the events. You bind the datasource to the ListBox in the Page_Load event, and update the data base in the button Click event, and basically the Page_Load happens before the postback event such as the button's Click event. So the list does not reflect the updated data until you click the button again or call redirect in order for the data binding code to re-run. Here, in the buton click event handler, after you update database you simply run the code to bind the datasource to the LixtBox again to refresh the control.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups