Solved

ASP.NET Dynamic Control Adding - PostBack Issues

Posted on 2014-01-04
8
624 Views
Last Modified: 2014-01-12
Hello,
I have an issue with an ASP.Net VB page I am trying to build.
The interface is for a Job Sheet so it has one hard coded box for each of the 3 main categories (Tasks, Materials Used, Outstanding Tasks).
OnloadViewNext to each of the first boxes it has a Add Text Box field. The code for this is in VB.net and is below.
  Protected Sub btnDescription(ByVal sender As Object, ByVal e As EventArgs)

        AddRows_tblDescriptionRowCounter(1)
        ViewState("tblDescriptionRowCounter") = (tblDescriptionRowCounter + 1)

    End Sub

    Public Sub AddRows_tblDescriptionRowCounter(ByVal rowstoadd As Integer)
        Dim i As Integer = 0
        Do While (i < rowstoadd)
            Dim row As TableRow = New TableRow
            Dim cell1 As TableCell = New TableCell
            Dim cell2 As TableCell = New TableCell
            Dim intWidth As New Unit(100, UnitType.Percentage)
            Dim intFontSize As FontSize = FontSize.XXLarge

            Dim txtTextBox As New TextBox()
            txtTextBox.BorderStyle = BorderStyle.Solid
            txtTextBox.Width = intWidth
            txtTextBox.TextMode = TextBoxMode.MultiLine
            txtTextBox.Font.Size = txtDescriptionOfJobOrTask_1.Font.Size
            txtTextBox.Height = txtDescriptionOfJobOrTask_1.Height
            cell1.Controls.Add(txtTextBox)
            row.Cells.Add(cell1)
            row.Cells.Add(cell2)
            tblDescriptionOfJobs.Rows.Add(row)
            i = (i + 1)
        Loop
    End Sub

Open in new window

I have attempted to put in a manual ID (and make it unique based on a number) for each TextBox but it always says “ID already on page”.
The issue I am getting is when someone enters data in this order (For example).
Enter Data in Description of Task
Press Add Row (ON Description)
Enter Data in Materials Used
Press Add Row (On Materials)
Enter Data in new box on the Description Tasks
Enter Data in new box on the Materials.
Press Add Row (on either Materials or Tasks).
It then puts the data in the wrong text boxes.
WrongDataIt should be like this
CorrectData
However when the user does enter data for that section and presses Add Row. Then once a blank box has appeared underneath, the user moves onto the next section and presses Add Row. All the data is being returned in the correct boxes.

I believe it has something to do about the ViewState controls and ID’s of the controls during a postback (not the best web developer but doing some research appears to point to this).
Any idea on how I can correct this error please?
Thanks,
Alex
0
Comment
Question by:BoltonWanderer
  • 4
  • 4
8 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Are you re-adding the controls on post-back, before the ViewState is processed?
0
 

Author Comment

by:BoltonWanderer
Comment Utility
Hi,

The code I am using when the page is loaded is


 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        If IsPostBack = False Then
         
            For x = 0 To 24 - 1
                ddlStartTime_Hour.Items.Add(PadWithStartZeros(2, x))
                ddlEndTime_Hour.Items.Add(PadWithStartZeros(2, x))
            Next

            For x = 0 To 60 - 1 Step 5
                ddlStartTime_Minute.Items.Add(PadWithStartZeros(2, x))
                ddlEndTime_Minute.Items.Add(PadWithStartZeros(2, x))
            Next

            'Load Next Job Sheet Ref
            txtCompanyID.Text = 0
            Try

                GetNextJobSheetRef()

            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
            bdpDate.SelectedDate = Now
            bdpClientDate.SelectedDate = Now
            bdpEmployeeDate.SelectedDate = Now
            ddlStartTime_Minute.Text = ddlStartTime_Minute.Items(0).ToString
            ddlEndTime_Minute.Text = ddlEndTime_Minute.Items(0).ToString

            ddlStartTime_Hour.Text = ddlStartTime_Hour.Items(Now.Hour).ToString
            ddlEndTime_Hour.Text = ddlEndTime_Hour.Items(Now.Hour).ToString
            LoadAccountRefDetails()
            If ddlAccountRef.Text = "*Other" Then
                txtAccountName.Visible = True
                txtAccountName.Enabled = True
            Else
                txtAccountName.Visible = False
            End If
            GetAllJobSheets()
            
        Else
        
            If ddlAccountRef.Text = "*Other" Then
                txtAccountName.Visible = True
                txtAccountName.Enabled = True
            Else
                txtAccountName.Visible = False
            End If
            If (Not (ViewState("tblDescriptionRowCounter")) Is Nothing) Then 
                tblDescriptionRowCounter = Convert.ToInt32(ViewState("tblDescriptionRowCounter"))
              
                AddRows_tblDescriptionRowCounter(tblDescriptionRowCounter)
            End If
            If (Not (ViewState("tblMaterialsRowCounter")) Is Nothing) Then
                tblMaterialsRowCounter = Convert.ToInt32(ViewState("tblMaterialsRowCounter"))
               
                AddRows_tblMaterialsRowCounter(tblMaterialsRowCounter)
            End If
            If (Not (ViewState("tblOutstandingRowCounter")) Is Nothing) Then
                tblOutstandingRowCounter = Convert.ToInt32(ViewState("tblOutstandingRowCounter"))
                AddRows_tblOutstandingRowCounter(tblOutstandingRowCounter)
            End If
          
        End If
      
    End Sub

Open in new window


Not sure if I am re adding the controls before the viewstate is processed? Am I doing it correctly? I did post a question a good time ago about this and was advised to do it this way Old Question. I believe I followed it correctly?
Thanks,
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Let's review the page life cycle:

ASP.NET Page Life Cycle Overview
http://msdn.microsoft.com/en-us/library/ms178472.aspx

You can see in this image that LoadViewState comes after Page_Init, and before Page_Load.

ASP.NET Page Life Cycle
0
 

Author Comment

by:BoltonWanderer
Comment Utility
Thanks for the diagram. I have adjusted my code so that the Dymaic controls being loaded is happening in the PreInit method.  However where would I load the actual data as it appears that it only ever allows me to have one table with two textboxes and can't add any more.

 Private Sub JobSheet_PreInit(sender As Object, e As EventArgs) Handles Me.PreInit
        If IsPostBack = True Then
            If (Not (ViewState("tblDescriptionRowCounter")) Is Nothing) Then
                tblDescriptionRowCounter = Convert.ToInt32(ViewState("tblDescriptionRowCounter"))
                AddRows_tblDescriptionRowCounter(tblDescriptionRowCounter)
            End If
            If (Not (ViewState("tblMaterialsRowCounter")) Is Nothing) Then
                tblMaterialsRowCounter = Convert.ToInt32(ViewState("tblMaterialsRowCounter"))
                AddRows_tblMaterialsRowCounter(tblMaterialsRowCounter)
            End If
            If (Not (ViewState("tblOutstandingRowCounter")) Is Nothing) Then
                tblOutstandingRowCounter = Convert.ToInt32(ViewState("tblOutstandingRowCounter"))
                AddRows_tblOutstandingRowCounter(tblOutstandingRowCounter)
            End If
        End If
    End Sub

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

        'http://msdn.microsoft.com/en-us/library/xhy3h9f9.aspx
        If IsPostBack = False Then
            For x = 0 To 24 - 1
                ddlStartTime_Hour.Items.Add(PadWithStartZeros(2, x))
                ddlEndTime_Hour.Items.Add(PadWithStartZeros(2, x))
            Next
            For x = 0 To 60 - 1 Step 5
                ddlStartTime_Minute.Items.Add(PadWithStartZeros(2, x))
                ddlEndTime_Minute.Items.Add(PadWithStartZeros(2, x))
            Next

            'Load Next Job Sheet Ref
            txtCompanyID.Text = 0
            Try
                GetNextJobSheetRef()
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
            bdpDate.SelectedDate = Now
            bdpClientDate.SelectedDate = Now
            bdpEmployeeDate.SelectedDate = Now
            ddlStartTime_Minute.Text = ddlStartTime_Minute.Items(0).ToString
            ddlEndTime_Minute.Text = ddlEndTime_Minute.Items(0).ToString
            ddlStartTime_Hour.Text = ddlStartTime_Hour.Items(Now.Hour).ToString
            ddlEndTime_Hour.Text = ddlEndTime_Hour.Items(Now.Hour).ToString
            LoadAccountRefDetails()
            If ddlAccountRef.Text = "*Other" Then
                txtAccountName.Visible = True
                txtAccountName.Enabled = True
            Else
                txtAccountName.Visible = False
            End If
            GetAllJobSheets()
        Else
            If ddlAccountRef.Text = "*Other" Then
                txtAccountName.Visible = True
                txtAccountName.Enabled = True
            Else
                txtAccountName.Visible = False
            End If
           
        End If

    End Sub

Open in new window


ab
Thanks,
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
However where would I load the actual data as it appears that it only ever allows me to have one table with two text boxes and can't add any more.

Did this happen when you moved to PreInit?  Was this working correctly earlier?
0
 

Author Comment

by:BoltonWanderer
Comment Utility
Yes it was working before I did the move to PreInit. Thanks
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
Comment Utility
If you are grabbing rows to add from ViewState, it can't be processed until after LoadViewState.  You would need to figure out a different approach.
0
 

Author Closing Comment

by:BoltonWanderer
Comment Utility
Removed the feature of Add Row and just kept it as one big box. Sadly not an actual solution if others need this.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

11 Experts available now in Live!

Get 1:1 Help Now