I have been fighting with this code for 3 days. I got everything to work....except its a bit glitchy as of right now and I can't figure out why. I am sure it is simple. They basic concept is something like this:


OFFICE
[txtbox] [txtbox] [txtbox] [label]
+add row

BUSINESS
[txtbox] [txtbox] [txtbox] [label]
+add row

SOFTWARE:
[txtbox] [txtbox] [txtbox] [label]
+add row


I add a row using AJAX control, viewstate, Page_Init and all the correct methods... the only thing is... If I hit Add row for Office it will...then if I hit add row for software it adds one to office...then i hit software again and it will add down to software. Its like its one off somewhere in the logic... i cant find it...its driving me insane... PLEASE help :) here is my code and my markup:


MARKUP

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!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 runat="server">
    <title>Property Equipment Purchase Worksheet</title>
    <style type="text/css">
        BODY
        {
            background:#C7D28A;
        }
        .Header
        {
            width:950px;
            border:solid 1px #E98300;
            background:#1e1e1e;
            margin:0 auto;
            padding:10px;
            color:#E98300;
        }
        .Header_Logo
        {
            width:671px;
            margin:0 auto;
        }
        .Header_Title
        {
            width:650px;
            margin: 0 auto;
            font-size:30px;
            text-align:center;
        }
        .Main
        {
            background-color:#ede8c4;
            border:solid 1px #E98300;
            width:950px;
            padding:10px;
            margin:10px auto;
        }
        .WorkSheetNotes
        {
            margin-top:10px;
            margin-bottom:10px;
            margin-left:5px;
            color:#726E20;
     
        }
        .WorksheetInformation .WorkSheetMain .WorkSheetNotes
        {
            width:900;
            margin:5px auto;
            padding:15px;
            font-weight:bold;
        }

        .WorksheetInformation table
        {
            width:100%;
            border:none;
        }
        .WorksheetInformation td
        {
            width:50%;
            text-align:right;
        }
        .InputStyle
        {
            width:175px;
            border:solid 1px black;
        }
        .WorksheetMainHeading
        {
            background:#263F6A;
            font-size:23px;       
            text-align:center;
            text-decoration:underline;
            color:White;
            padding:0px;
            margin:0px;
            width:50%;
        }

        .WorksheetEquipmentHeading
        {
            text-indent:5px;
            text-align:left;
            font-weight:bold;
            background-color:#C5D2E0;
        }

        .WorksheetPriceHeading
        {
            text-indent:5px;
            font-weight:bold;
            background-color:#C5D2E0;
            text-align:left;
            border:solid 1px black;
        }
        .WorksheetEquipment
        {
            width:35%;
            text-indent:15px;
            text-align:left;
            border:solid 1px black;
        }
                .WorksheetPrice
        {
            width:25%;
            text-align:right;
            border:solid 1px black;
        }
        .WorksheetQuantity
        {
            width:20%;
            text-align:center;
            border:solid 1px black;
        }
        .WorksheetTotals
        {
            width:20%;
            text-align:right;
            background-color:Gray;
            border:solid 1px black;
        }
        .EquipmentInupt
        {width:90%;}
        .submit
        {
        
        background-color:#EDE8C4;
        border:1px #E98300 solid;
        width:200px;
        height:25px;
 
        padding-bottom:5px;

        }
        .SubmitDIV
        {
            float:right;
          
            margin:10px;
            padding:10px;
            text-align:right;
            display:inline;
        }
        .EquipmentHeading, .EquipmentListing
        {
            text-indent:5px;
            font-weight:bold;
            background-color:#C5D2E0;
            text-align:left;
            border:solid 1px black;
            margin:1px 01px 0px 0px;
            
        }
        .EquipmentListing
        {
           background-color:#ede8c4;
           height:455px;
        }
        .Equipment
        {
            height:28px;
            width:35%;
            float:left;

            text-align:center;
        }
        .Price
        {
            height:28px;
            width:25%;
            float:left;
            text-align:center;
        }
        .Quantity
        {
            height:28px;
            width:19%;
            float:left;
            text-align:center;
            
        }
        .Totals
        {
            width:19%;
            float:left;
            height:28px;
            text-align:right;
            padding-right:3px;
            
            background-color:inherit;
        }
        .WorksheetInputStyle
        {
  
            width:95%;
            margin:3px auto 5px auto;

            border:solid 1px black;
        }
        .Row { width:950px; } 
        
            
        .AddRow
        {
           
            margin-top:1px;
        }
        
   </style>
</head>
<body>

<script language=javascript>
    function click_btn_office() {
        document.form1.btnAddOfficeRow.click();
    }
    function click_btn_business() {
        document.form1.btnAddBusinessRow.click();
    }
    function click_btn_software() {
        document.form1.btnAddSoftwareRow.click();
    }

    </script>
    <form id="form1" runat="server">
                    <asp:ScriptManager ID="ScriptManager1" runat="server">
                </asp:ScriptManager>
    
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:BHPropertyInfoConnectionString %>" 
        SelectCommand="SELECT [PROPERTY_NAME] FROM [PROPERTY_INFORMATION]">
    </asp:SqlDataSource>
    
    <div class="Header">     
        <div class="Header_Logo"><img src="Images/BH_Header.png" /></div>
        <div class="Header_Title">Property Equipment Purchase Worksheet</div>
    </div>
    
    <div class="Main">
        
        <div class="WorksheetInformation">
            <table border="0">
                <tr>
                    <td style="text-align:left"><asp:Label ID="lblDate" runat="server" Text="1"></asp:Label></td>
                    <td>Property Name:  
                        <asp:DropDownList CssClass="InputStyle" ID="DropDownList1" runat="server" 
                            DataSourceID="SqlDataSource1" DataTextField="PROPERTY_NAME" 
                            DataValueField="PROPERTY_NAME">
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align:right;">Budget Cost Code: <asp:TextBox  CssClass="InputStyle" ID="BudgetCostCode" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align:center"><asp:Label ID="lblPurchasePrice" runat="server" Text="PurchacePrice: $0000.00"></asp:Label></td>
                </tr>
            </table>
         </div>
         <div class="WorkSheetNotes">
            Note: Printer Prices do not include tax or shipping costs. If building wiring is required contact Kevin Nance.
         </div>
         <div class="WorkSheetMain">
            <div class="Row">
               <div class="EquipmentHeading Equipment">Office Equipment</div>
               <div class="EquipmentHeading Price">Purchase Price</div>
               <div class="EquipmentHeading Quantity">Quantity</div>
               <div class="EquipmentHeading Totals">Totals</div>

                        <div class="EquipmentListing Equipment"><asp:TextBox CssClass="WorksheetInputStyle" ID="OfficeEquipment0" runat="server"></asp:TextBox></div>
                        <div class="EquipmentListing Price"><asp:TextBox CssClass="WorksheetInputStyle" ID="OfficePrice0" runat="server"></asp:TextBox></div>
                        <div class="EquipmentListing Quantity"><asp:TextBox CssClass="WorksheetInputStyle" ID="OfficeQuantity0" runat="server"></asp:TextBox></div>
                        <div class="EquipmentListing Totals"><asp:Label CssClass="Totals" ID="OfficeTotal0" runat="server" Text="$"></asp:Label></div>                          
                <asp:UpdatePanel ID="upPanel" runat="server">
                 <ContentTemplate>

                 
                    <asp:Panel ID="phOfficeEquipment" runat="server"></asp:Panel> 
                    <div class="AddRow"><a href="#" onclick="click_btn_office()">+ Add Row</a><asp:Button ID="btnAddOfficeRow" runat="server" Text="Button" /></div>                 
 
               <div class="EquipmentHeading Equipment">Business Center Equipment</div>
               <div class="EquipmentHeading Price">Purchase Price</div>
               <div class="EquipmentHeading Quantity">Quantity</div>
               <div class="EquipmentHeading Totals">Totals</div>

                        <div class="EquipmentListing Equipment"><asp:TextBox CssClass="WorksheetInputStyle" ID="BusinessEquipment0" runat="server"></asp:TextBox></div>
                        <div class="EquipmentListing Price"><asp:TextBox CssClass="WorksheetInputStyle" ID="BusinessPrice0" runat="server"></asp:TextBox></div>
                        <div class="EquipmentListing Quantity"><asp:TextBox CssClass="WorksheetInputStyle" ID="BusinessQuantity0" runat="server"></asp:TextBox></div>
                        <div class="EquipmentListing Totals"><asp:Label CssClass="Totals" ID="BusinessTotal0" runat="server"  Text="$"></asp:Label></div>                          


                 
                    <asp:Panel ID="phBusinessEquipment" runat="server"></asp:Panel> 
                    <div class="AddRow"><a href="#" onclick="click_btn_business()">+ Add Row</a><asp:Button ID="btnAddBusinessRow" runat="server" Text="Button" /></div>                 

                              <div class="EquipmentHeading Equipment">software Center Equipment</div>
               <div class="EquipmentHeading Price">Purchase Price</div>
               <div class="EquipmentHeading Quantity">Quantity</div>
               <div class="EquipmentHeading Totals">Totals</div>

                        <div class="EquipmentListing Equipment"><asp:TextBox CssClass="WorksheetInputStyle" ID="softwareEquipment0" runat="server"></asp:TextBox></div>
                        <div class="EquipmentListing Price"><asp:TextBox CssClass="WorksheetInputStyle" ID="softwarePrice0" runat="server"></asp:TextBox></div>
                        <div class="EquipmentListing Quantity"><asp:TextBox CssClass="WorksheetInputStyle" ID="softwareQuantity0" runat="server"></asp:TextBox></div>
                        <div class="EquipmentListing Totals"><asp:Label CssClass="Totals" ID="softwareTotal0" runat="server"  Text="$"></asp:Label></div>                          


                 
                    <asp:Panel ID="phsoftwareEquipment" runat="server"></asp:Panel> 
                    <div class="AddRow"><a href="#" onclick="click_btn_software()">+ Add Row</a><asp:Button ID="btnAddsoftwareRow" runat="server" Text="Button" /></div>                 
                </ContentTemplate>
                </asp:UpdatePanel>
               <div class="SubmitDIV">
                <asp:Button ID="Button1" runat="server" CssClass="submit" Text="Submit Request" />
               </div>
                
            </div>
          </div>
         </div>

  </form>
 </body>
</html>

CODE:

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub btnAddOfficeRow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddOfficeRow.Click
        If Page.IsPostBack = True Then
            Session("office_row_click") = Session("office_row_click") + 1
            Session("lastclicked") = "office"
        End If
    End Sub

    Protected Sub btnAddBusinessRow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddBusinessRow.Click
        If Page.IsPostBack = True Then
            Session("business_row_click") = Session("business_row_click") + 1
            Session("lastclicked") = "business"
        End If
    End Sub
    Protected Sub btnAddsoftwareRow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddsoftwareRow.Click
        If Page.IsPostBack = True Then
            Session("software_row_click") = Session("software_row_click") + 1
            Session("lastclicked") = "software"
        End If
    End Sub
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        add_office_rows()
        add_business_rows()
        add_software_rows()

    End Sub




    Public Function add_office_rows() As String

        Dim cnt As Integer = Session("office_row_click")
        lblPurchasePrice.Text = cnt.ToString
        For x = 1 To cnt
            Dim OfficeEquipmentRow, OfficePriceRow, OfficeQuantityRow As TextBox
            Dim OfficeEquipmentRowDiv, OfficePriceRowDiv, OfficeQuantityRowDiv, OfficeTotalRowDiv As Panel
            Dim OfficeTotalRow As Label = New Label
            OfficeEquipmentRow = New TextBox
            OfficePriceRow = New TextBox
            OfficeQuantityRow = New TextBox
            OfficeTotalRowDiv = New Panel
            OfficeEquipmentRowDiv = New Panel
            OfficePriceRowDiv = New Panel
            OfficeQuantityRowDiv = New Panel
            OfficeTotalRowDiv = New Panel


            phOfficeEquipment.Controls.Add(OfficeEquipmentRowDiv)
            OfficeEquipmentRowDiv.Controls.Add(OfficeEquipmentRow)

            phOfficeEquipment.Controls.Add(OfficePriceRowDiv)
            OfficePriceRowDiv.Controls.Add(OfficePriceRow)

            phOfficeEquipment.Controls.Add(OfficeQuantityRowDiv)
            OfficeQuantityRowDiv.Controls.Add(OfficeQuantityRow)

            phOfficeEquipment.Controls.Add(OfficeTotalRowDiv)
            OfficeTotalRowDiv.Controls.Add(OfficeTotalRow)


            OfficeEquipmentRow.ID = "OfficeEquipment" & x
            OfficePriceRow.ID = "OfficePrice" & x
            OfficeQuantityRow.ID = "OfficeQuantity" & x
            OfficeTotalRow.ID = "OfficeTotal" & x

            OfficeEquipmentRowDiv.ID = "OfficeEquipmentDiv" & x
            OfficePriceRowDiv.ID = "OfficePriceDiv" & x
            OfficeQuantityRowDiv.ID = "OfficeQuantityDiv" & x
            OfficeTotalRowDiv.ID = "OfficeTotalDiv" & x
            OfficeEquipmentRowDiv.CssClass = "EquipmentListing Equipment"
            OfficePriceRowDiv.CssClass = "EquipmentListing Price"
            OfficeQuantityRowDiv.CssClass = "EquipmentListing Quantity"
            OfficeTotalRowDiv.CssClass = "EquipmentListing Totals"
            OfficeEquipmentRow.CssClass = "WorksheetInputStyle"
            OfficePriceRow.CssClass = "WorksheetInputStyle"
            OfficeQuantityRow.CssClass = "WorksheetInputStyle"
            OfficeTotalRow.CssClass = "Totals"
            OfficeTotalRow.Text = "$"
        Next
        Return vbNull
    End Function

    Public Function add_business_rows() As String

        Dim cnt As Integer = Session("business_row_click")
        lblPurchasePrice.Text = cnt.ToString
        For x = 1 To cnt
            Dim businessEquipmentRow, businessPriceRow, businessQuantityRow As TextBox
            Dim businessEquipmentRowDiv, businessPriceRowDiv, businessQuantityRowDiv, businessTotalRowDiv As Panel
            Dim businessTotalRow As Label = New Label
            businessEquipmentRow = New TextBox
            businessPriceRow = New TextBox
            businessQuantityRow = New TextBox
            businessTotalRowDiv = New Panel
            businessEquipmentRowDiv = New Panel
            businessPriceRowDiv = New Panel
            businessQuantityRowDiv = New Panel
            businessTotalRowDiv = New Panel


            phBusinessEquipment.Controls.Add(businessEquipmentRowDiv)
            businessEquipmentRowDiv.Controls.Add(businessEquipmentRow)

            phBusinessEquipment.Controls.Add(businessPriceRowDiv)
            businessPriceRowDiv.Controls.Add(businessPriceRow)

            phBusinessEquipment.Controls.Add(businessQuantityRowDiv)
            businessQuantityRowDiv.Controls.Add(businessQuantityRow)

            phBusinessEquipment.Controls.Add(businessTotalRowDiv)
            businessTotalRowDiv.Controls.Add(businessTotalRow)


            businessEquipmentRow.ID = "businessEquipment" & x
            businessPriceRow.ID = "businessPrice" & x
            businessQuantityRow.ID = "businessQuantity" & x
            businessTotalRow.ID = "businessTotal" & x


            businessEquipmentRowDiv.ID = "businessEquipmentDiv" & x
            businessPriceRowDiv.ID = "businessPriceDiv" & x
            businessQuantityRowDiv.ID = "businessQuantityDiv" & x
            businessTotalRowDiv.ID = "businessTotalDiv" & x
            businessEquipmentRowDiv.CssClass = "EquipmentListing Equipment"
            businessPriceRowDiv.CssClass = "EquipmentListing Price"
            businessQuantityRowDiv.CssClass = "EquipmentListing Quantity"
            businessTotalRowDiv.CssClass = "EquipmentListing Totals"
            businessEquipmentRow.CssClass = "WorksheetInputStyle"
            businessPriceRow.CssClass = "WorksheetInputStyle"
            businessQuantityRow.CssClass = "WorksheetInputStyle"
            businessTotalRow.CssClass = "Totals"
            businessTotalRow.Text = "$"
        Next
        Return vbNull
    End Function
    Public Function add_software_rows() As String

        Dim cnt As Integer = Session("software_row_click")
        lblPurchasePrice.Text = cnt.ToString

        For x = 1 To cnt
            Dim softwareEquipmentRow, softwarePriceRow, softwareQuantityRow As TextBox
            Dim softwareEquipmentRowDiv, softwarePriceRowDiv, softwareQuantityRowDiv, softwareTotalRowDiv As Panel
            Dim softwareTotalRow As Label = New Label
            softwareEquipmentRow = New TextBox
            softwarePriceRow = New TextBox
            softwareQuantityRow = New TextBox
            softwareTotalRowDiv = New Panel
            softwareEquipmentRowDiv = New Panel
            softwarePriceRowDiv = New Panel
            softwareQuantityRowDiv = New Panel
            softwareTotalRowDiv = New Panel


            phsoftwareEquipment.Controls.Add(softwareEquipmentRowDiv)
            softwareEquipmentRowDiv.Controls.Add(softwareEquipmentRow)

            phsoftwareEquipment.Controls.Add(softwarePriceRowDiv)
            softwarePriceRowDiv.Controls.Add(softwarePriceRow)

            phsoftwareEquipment.Controls.Add(softwareQuantityRowDiv)
            softwareQuantityRowDiv.Controls.Add(softwareQuantityRow)

            phsoftwareEquipment.Controls.Add(softwareTotalRowDiv)
            softwareTotalRowDiv.Controls.Add(softwareTotalRow)


            softwareEquipmentRow.ID = "softwareEquipment" & x
            softwarePriceRow.ID = "softwarePrice" & x
            softwareQuantityRow.ID = "softwareQuantity" & x
            softwareTotalRow.ID = "softwareTotal" & x

            softwareEquipmentRowDiv.ID = "softwareEquipmentDiv" & x
            softwarePriceRowDiv.ID = "softwarePriceDiv" & x
            softwareQuantityRowDiv.ID = "softwareQuantityDiv" & x
            softwareTotalRowDiv.ID = "softwareTotalDiv" & x
            softwareEquipmentRowDiv.CssClass = "EquipmentListing Equipment"
            softwarePriceRowDiv.CssClass = "EquipmentListing Price"
            softwareQuantityRowDiv.CssClass = "EquipmentListing Quantity"
            softwareTotalRowDiv.CssClass = "EquipmentListing Totals"
            softwareEquipmentRow.CssClass = "WorksheetInputStyle"
            softwarePriceRow.CssClass = "WorksheetInputStyle"
            softwareQuantityRow.CssClass = "WorksheetInputStyle"
            softwareTotalRow.CssClass = "Totals"
            softwareTotalRow.Text = "$"
        Next
        Return vbNull
    End Function



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

        If Session("office_row_click") = vbNull Then
            Session("office_row_click") = 1
        End If
        If Session("office_row_click") = 0 Then
            Session("office_row_click") = 1
        End If
        If Session("business_row_click") = 0 Then
            Session("business_row_click") = 1
        End If

        If Session("business_row_click") = vbNull Then
            Session("business_row_click") = 1
        End If
        If Session("software_row_click") = 0 Then
            Session("software_row_click") = 1
        End If

        If Session("software_row_click") = vbNull Then
            Session("software_row_click") = 1
        End If

    End Sub



    Protected Sub OfficePrice0_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles OfficePrice0.TextChanged

    End Sub
End Class

(this code is still in development and may not be the neatest...do forgive me :) )

Recommended Answers

All 5 Replies

Why don't you do Line 27-29

add_office_rows()
add_business_rows()
add_software_rows()

with ifs or in Button press why do you do them all?

If I dont run those three functions in init then the rows that have been added prior to this postback will not populate or even be there.

Well there is too much code to try and debug and see what is wrong.

I am not sure what you mean, but I want to make sure you know what I mean.

Can't you do

if  software then do 
    add_software_rows()
else if business 
    add_business_rows()
else if office
    add_office_rows()

no these functions must run every time regardless if that button was pushed or not. If they are not run then the rows that were added previously will not render. As you see in Page_Load I incriment the counter to tell me how many rows to render which is doing the same thing you have in mind..my way just keeps the viewstate intact.

here is a more brief code for you to hopefully be able to help debug. This is just an example script but esentially does...or needs to do the same thing.

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Session("tb_num") += 1
    End Sub

    Public Function add_text_box() As Double
        For z = Session("tb_num") To 0 Step -1
            Dim x As New TextBox
            form1.Controls.Add(x)
            x.ID = "tb" + z.ToString
            Label1.Text = Session("tb_num")
        Next
    End Function

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

    End Sub

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

        If Session("tb_num") = Nothing Then
            Session("tb_num") = 1
        End If
        add_text_box()
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        For z = Session("tb_num") To 0 Step -1
            Dim tb As TextBox = New TextBox
            tb.ID = "tb" + z.ToString
            Label1.Text += tb.Text
        Next

    End Sub

    Public Function find_control(ByVal cnt As Control) As Double
        For Each x As Control In cnt.Controls
            If TypeOf x Is HtmlInputControl Then
                Label1.Text += CType(x, HtmlInputControl).Value
                CType(x, HtmlInputControl).Disabled = True


            End If
            If x.HasControls Then
                find_control(x)
            End If
        Next
    End Function
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.