Web Analytics Made Easy -
StatCounter Problem with dynamic controls - CodingForum

Announcement

Collapse
No announcement yet.

Problem with dynamic controls

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with dynamic controls

    I looked everywhere but no good solution works.

    I create a table with a linkbutton.

    When the list is loaded with the buttons. It looks fine, but when you fire the button it doesnt set any session vars (That part is in the eventhandler of the button). After the postback of the button click, the list is back and when you click again, then the vars are set but with the wrong values.

    So if i click on button 1 with the value 12 and the page reloads and i click on button 2 with the value 15 it displays the value 12! Not 15...
    The first time he doesnt set any session vars. And he ignores the eventhandler of the linkbutton. After the postback, the eventhandler fires but the wrong value.

    I have tried alot of things with LoadViewState and more of these things but it wont work. Maybe its something very simple, but i dont see it

    I have set up a test project.

    Very simple.

    A list loaded every postback with a link button with some vars.

    Here is the code:

    Imports System.Data.SqlClient

    Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Page.IsPostBack Then
    If ViewState("mode").ToString = "1" Then
    Response.Write(Session("AB"))
    Response.Write("<BR>")
    Response.Write(Session("TS"))
    End If

    Else
    ViewState.Add("mode", 0)
    End If
    End Sub

    Public Sub AddOrderRegelHandler(ByVal sender As Object, ByVal e As ImageClickEventArgs)

    Dim Btn As ImageButton = DirectCast(sender, ImageButton)

    Dim a() As String
    a = Btn.CommandArgument.Split(",")

    Session("AB") = ""
    Session("TS") = ""

    Session("AB") = a(0)
    Session("TS") = a(1)
    End Sub

    Protected Sub GetToestellen(ByVal ProviderID As Integer, ByVal AbonnementID As Integer)
    Dim sBgColor As String
    Dim cmd As SqlCommand
    Dim DR As SqlDataReader
    Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("Rinkel").ConnectionString)
    cmd = New SqlCommand("web_get_Combinatieprijzen_Abonnement2", conn)
    cmd.CommandType = Data.CommandType.StoredProcedure
    cmd.Parameters.Add("@Providers_ID", Data.SqlDbType.Int)
    cmd.Parameters("@Providers_ID").Value = ProviderID
    cmd.Parameters.Add("@Abonnementen_ID", Data.SqlDbType.Int)
    cmd.Parameters("@Abonnementen_ID").Value = AbonnementID
    cmd.Parameters.Add("@Filialen_ID", Data.SqlDbType.VarChar, 6)
    cmd.Parameters("@Filialen_ID").Value = "GSMWAR"

    Try
    conn.Open()
    DR = cmd.ExecuteReader

    sBgColor = "#FFF"

    Dim i As Integer
    i = 1
    tblTest.Rows.Clear()

    While DR.Read()
    Dim tableRow As New HtmlTableRow
    Dim tcToestel As New HtmlTableCell
    Dim tcPrijs1 As New HtmlTableCell
    Dim tcPrijs2 As New HtmlTableCell

    Dim BtnPrijs1jr As New ImageButton
    AddHandler BtnPrijs1jr.Click, AddressOf AddOrderRegelHandler

    tcToestel.InnerText = DR("Toestel")

    With BtnPrijs1jr
    .CausesValidation = True
    .ID = "BtnPrijs1jr" & i
    '.Text = DR("Combinatieprijs1")
    .ToolTip = "Click to make selection"
    '.CommandName = "actieKlik"
    .ImageUrl = "images/informatie.gif"
    .CommandArgument = DR("Abonnementen1_ID") & "," & DR("Toestellen_ID")
    End With

    With tcPrijs1
    .Controls.Add(BtnPrijs1jr)
    End With

    With tableRow
    .Cells.Add(tcToestel)
    .Cells.Add(tcPrijs1)
    '.Cells.Add(tcPrijs2)
    End With

    With tblTest
    .Rows.Add(tableRow)
    End With

    i += 1
    End While

    Catch ex As Exception
    Throw New Exception(ex.Message.ToString)
    Finally
    conn.Close()
    End Try
    End Sub

    Protected Overrides Sub LoadViewState(ByVal savedState As Object)
    MyBase.LoadViewState(savedState)

    If ViewState("mode").ToString = "1" Then
    GetToestellen(1, 1121)
    End If
    End Sub

    Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
    GetToestellen(1, 1121)
    ViewState.Add("mode", "1")
    End Sub
    End Class
    Last edited by Iwan; Oct 2, 2006, 08:40 AM.

  • #2
    Try to put the setSession below the declaration of the button and when declaring a button set all values to 0 (zero)

    Comment


    • #3
      Originally posted by Lallo View Post
      Try to put the setSession below the declaration of the button and when declaring a button set all values to 0 (zero)
      Doesnt help...

      It seems when the eventhandler is fired he dont put the right vars in the session.

      When you do a response.write in the eventhandler you see that he has the right vars but he doesnt store them directly in the session, after the next postback he has the vars....but not from the button which is clicked on....

      Comment


      • #4
        when clicking a button before setting session clear all variables
        so (i improvise):
        when click button 02 lets say, the session should be set with value 2 lets say, right? well before setting this value do the following:
        sessionValue = 0;
        sessionValue = button.value

        Got it?

        I'm sure it will work like this because i've met this problem before!

        Regards!

        Comment


        • #5
          Originally posted by Lallo View Post
          when clicking a button before setting session clear all variables
          so (i improvise):
          when click button 02 lets say, the session should be set with value 2 lets say, right? well before setting this value do the following:
          sessionValue = 0;
          sessionValue = button.value

          Got it?

          I'm sure it will work like this because i've met this problem before!

          Regards!
          I have changed it like you said.

          Public Sub AddOrderRegelHandler(ByVal sender As Object, ByVal e As CommandEventArgs)
          Dim Btn As ImageButton = DirectCast(sender, ImageButton)

          Dim a() As String
          a = Btn.CommandArgument.Split(",")

          Session("AB") = 0
          Session("TS") = 0

          Session("AB") = a(0)
          Session("TS") = a(1)

          Response.Write(Btn.ID & " has clicked me")
          Response.Write("<br>" & Btn.CommandArgument)
          End Sub

          Still no luck.

          See what i mean on http://test.gsmwarenhuis.nl

          Click on a I next to the mobile phone and see the result after the click...and then click again!

          Comment


          • #6
            Originally posted by Iwan View Post
            I have changed it like you said.

            Public Sub AddOrderRegelHandler(ByVal sender As Object, ByVal e As CommandEventArgs)
            Dim Btn As ImageButton = DirectCast(sender, ImageButton)

            Dim a() As String
            a = Btn.CommandArgument.Split(",")

            Session("AB") = 0
            Session("TS") = 0

            Session("AB") = a(0)
            Session("TS") = a(1)

            Response.Write(Btn.ID & " has clicked me")
            Response.Write("<br>" & Btn.CommandArgument)
            End Sub

            Still no luck.

            See what i mean on http://test.gsmwarenhuis.nl

            Click on a I next to the mobile phone and see the result after the click...and then click again!
            try to put the
            session("ab") = a(0)
            session("ts") = a(1)

            after
            response.write("<br>" & Btn.CommandArgument)

            Comment


            • #7
              Originally posted by Lallo View Post
              try to put the
              session("ab") = a(0)
              session("ts") = a(1)

              after
              response.write("<br>" & Btn.CommandArgument)
              That doenst matter!

              Because the response.write is going to be away after i solved the problem. I only put it there to show that he gives the right values but doesnt store them. Only the next click he knows them....everytime when you look in the example he is one click late!

              http://test.gsmwarenhuis.nl

              Comment


              • #8
                Solved!

                I called on the wrong moment the session vars!

                thnx for the reading

                Comment

                Working...
                X