dynamic control confusion

Discussion in 'C#' started by ff88, Aug 4, 2009.

  1. #1
    Hi guys,

    I'm sort of trying to familiarise myself with ASP for the next school year and I am so lost in what I have to do. I've been going through the old notes from last year and I am pretty stuck on a few things.

    Now I've created two pages on visual web developer and I've got a couple of tasks to put in which are :-


    On the first page, provide a textbox for the number of products to be checked against a price threshold, and a textbox for the price threshold. (I don't quite understand what this means checking the number of products vs price?? So I don't actually know what I am meant to be doing here.)


    On the second page, for each product, dynamically create two textboxes:
    Product name
    Product price

    (I think I've sort of done this but not sure if I have done it right? I don't think I've confirmed it, so don't know if it works)

    3. Using dynamically-created labels, print out on the second page those products whose price is bellow the threshold, then those whose price is above or equals the threshold. Calculate the total price of all the products, and show it on the second page. (I'm completely lost with this and really haven't got a clue about what to do here)



    So you can see I have a few problems here. Now here is the code for what I have done :-

    Page 1

    <%--


    --%>

    <%@ Page Language="VB" EnableViewState="True" %>

    <script runat="server">

    Sub Click_Num(sender as Object, e as EventArgs)

    Session("NumOfBoxes") = txtNum.Text
    Response.Redirect("default2.aspx")

    End Sub


    Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

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

    End Sub
    </script>

    <html> <head>
    <title>This will be a tutorial on OOP - Part 2</title>
    </head>
    <body>
    <form id="Form1" method="post" runat="server">

    Enter the number of records to be created <br><br>

    <asp:TextBox id="txtNum" Width = 150 runat="server" BorderColor="Black" />
    <asp:RequiredFieldValidator id="valRF_Num" runat="server" ControlToValidate="txtNum"
    ErrorMessage="Please, enter a number!" Display="dynamic" />

    <asp:CompareValidator id="valComp_Num"
    ControlToValidate="txtNum"
    Operator="DataTypeCheck" ErrorMessage="Should be an integer number!"
    Type="Integer" runat= "server" /><br>

    <asp:TextBox ID="pricethreshold" runat="server" OnTextChanged="TextBox1_TextChanged" Width="150px" BorderColor="Black" ForeColor="Black"></asp:TextBox><br>

    <asp:Button id="btnSubmit_Num" Text ="Submit" OnClick="Click_Num" runat="server" /><br><br>

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

    Page 2

    <%--

    --%>


    <%@ Page Language="VB" EnableViewState="True" %>

    <script runat="server">

    Sub Click(s As Object, e As EventArgs)

    Dim nNumOfLabels As Integer = CInt(lblNumOfRecords.Text)

    Dim i As Integer

    Dim txtBox As TextBox = New TextBox()

    Dim lblLabel(nNumOfLabels) As Label


    '>>>>> We dynamically add labels to the placeholder

    plhLabelsHere.Controls.Add(New LiteralControl("<hr><br>")) ' horisontal line and linebreak are attached

    plhLabelsHere.Controls.Add(New LiteralControl("Output:<br>")) ' "Output" message attached

    For i = 0 To nNumOfLabels - 1

    lblLabel(i) = New Label()

    txtBox = plhTextBoxesHere.FindControl("txt" & i.ToString())

    lblLabel(i).Text = "Record No. " & i.ToString & ": " & txtBox.Text

    plhLabelsHere.Controls.Add(lblLabel(i)) ' lblLabel(i) is attached

    plhLabelsHere.Controls.Add(New LiteralControl("<br>")) ' linebreak is attached
    Next

    '<<<<< End adding labels

    End Sub


    Sub Page_Load(sender as Object, e as EventArgs)

    lblNumOfRecords.Text = Session("NumOfBoxes")

    Dim strInput As String = lblNumOfRecords.Text

    Dim nNumOfBoxes = CInt(strInput)

    Dim txtBox(nNumOfBoxes) As TextBox
    Dim compValidator(nNumOfBoxes) As CompareValidator
    Dim rfValidator(nNumOfBoxes) As RequiredFieldValidator

    Dim i As Integer


    '>>>>> We dynamically add textboxes and validators to the placeholder

    For i = 0 To nNumOfBoxes - 1

    txtBox(i) = New TextBox() 'txtBox(i) object is instantiated
    txtBox(i).ID = "txt" & i.ToString() 'and its ID initialised
    plhTextBoxesHere.Controls.Add(txtBox(i)) 'txtBox(i) is attached to the placeholder

    '>>>>> We validate the textfields for data type entered

    ' We instantiate and initialise objects of CompareValidator class
    compValidator(i) = New CompareValidator()
    compValidator(i).ID = "compVal" & i.ToString()
    compValidator(i).ControlToValidate = txtBox(i).ID
    compValidator(i).Operator = ValidationCompareOperator.DataTypeCheck
    compValidator(i).ErrorMessage = " Should be a number!"
    compValidator(i).Type = ValidationDataType.Double
    compValidator(i).Display = ValidatorDisplay.Dynamic

    ' We instantiate and initialise objects of RequiredFieldValidator class
    rfValidator(i) = New RequiredFieldValidator()
    rfValidator(i).ID = "rfVal" & i.ToString()
    rfValidator(i).ControlToValidate = txtBox(i).ID
    rfValidator(i).ErrorMessage = " Please, enter a value!"
    rfValidator(i).Display = ValidatorDisplay.Dynamic

    plhTextBoxesHere.Controls.Add(compValidator(i)) ' compValidator(i) is attached to the placeholder
    plhTextBoxesHere.Controls.Add(rfValidator(i)) ' rfValidator(i) is attached to the placeholder

    '<<<<< End validation

    plhTextBoxesHere.Controls.Add(New LiteralControl("<br>")) ' linebreak is attached to the placeholder

    Next

    '<<<<< End adding textboxes

    End Sub


    </script>

    <html> <head>
    <title>This will be a tutorial on OOP - Part 2</title>
    </head>

    <body>
    <form id="Form1" method="post" runat="server">
    Number of dynamically created records:
    <asp:Label id="lblNumOfRecords" runat="server" /><br><br>

    Please, enter your data in the "Double" format <br><br>

    <asplaceholder runat="server" id="plhTextBoxesHere" /><br><br>

    <asp:Button id="btnSubmit" Text ="Submit" OnClick="Click" runat="server" /><br><br>

    <asp:Label id="lblLabel" runat="server" />

    <asplaceholder runat="server" id="plhLabelsHere" /><br><br>
    Product name: <br>
    <asp:TextBox id="txtProductname" Width = 150 runat="server" />

    <asp:RequiredFieldValidator id="valRF_Productname" runat="server" ControlToValidate="txtproductname"
    ErrorMessage="Name?" InitialValue="0" Display="dynamic" /><br><br>


    Product Price: <br>
    <asp:TextBox id="txtProductprice" Width = 150 runat="server" />

    <asp:RequiredFieldValidator id="valRF_Productprice" runat="server" ControlToValidate="txtProdcutprice"
    ErrorMessage="Must be a number?" InitialValue="0" Display="dynamic" /><br><br>

    </form>

    </body>
    </html>



    So could anyone tell me where I have gone wrong and how to sort out the problems I have with the 3 I've posted up there. Because I'm pretty lost at the moment.

    Thanks to anyone who can help me out here
     
    ff88, Aug 4, 2009 IP