Results 1 to 11 of 11

Thread: ASP.NET VB in HTML body

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    106

    ASP.NET VB in HTML body

    Hi, I'm writing a .aspx in asp.net vb codes, i have
    a datatable, however i can't seem to display
    the individual field data of each records as i'm not
    sure if i'm coding it correctly, please correct me!
    help appreciated!

    <BODY>

    <% Dim i As Integer %>
    <% For i = 0 to DataTable.Rows.Count - 1 %>
    <% DataRow = DataTable.Rows(i) %>
    <% DataRow.Item("ID") %> , <% DataRow.Item("Name") %> , <% DataRow.Item("Age") %>
    <% Next %>

    </BODY>

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: ASP.NET VB in HTML body

    You aren't really using ASP.NET as was intended - your coding is more like classic ASP.

    Try using a repeater and databinding it to your DataTable.

    Let me know if you need any more help.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    106

    Re: ASP.NET VB in HTML body

    yeah i wish to have more control.
    can you show me some examples to display my datatable record
    fields inside html body, thanks.

  4. #4
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: ASP.NET VB in HTML body

    Could you show me the code you use to populate your datatable.

    Cheers

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    106

    Re: ASP.NET VB in HTML body

    If Session("Table") Is Nothing Then
    DataTable.Columns.Add("Code", GetType(String))
    DataTable.Columns.Add("Product", GetType(String))
    DataTable.Columns.Add("Quantity", GetType(Integer))
    DataTable.Columns.Add("Cost", GetType(Decimal))
    Session("Table") = DataTable
    Else
    DataTable = Session("Table")
    End If


    DataRow = DataTable.NewRow
    DataRow("Code") = DataSet.FieldValue("product_id", nothing)
    DataRow("Product") = DataSet.FieldValue("product_name", nothing)
    DataRow("Quantity") = Int32.Parse(txtQuantity.Text)
    DataRow("Cost") = Double.Parse(DataSet.FieldValue("product_cost", nothing))
    DataTable.Rows.Add(DataRow)

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    106

    Re: ASP.NET VB in HTML body

    the last 6 lines are actually in a button 'add' they will add
    values of the product the user chooses, i tried using a
    datagrid, it works perfectly, however i wish to have
    more control to display the value not in a form of table
    but at my own discrete.. thats why i chose for loop.

  7. #7
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: ASP.NET VB in HTML body

    OK gotcha.

    Use the repeater control as it basically implements the loop you were writing within a control.

    Within the ItemTemplate tags you can enter any HTML code you like to format the data.

    To match your original example with the comma separated values you would do:
    Code:
    <asp:Repeater id="rptTest" runat="server">
    	<ItemTemplate>
    		<%# Container.DataItem("ID") %>, <%# Container.DataItem("Name") %>, <%# Container.DataItem("Age") %><br />
    	</ItemTemplate>
    </asp:Repeater>
    You must of course set the DataSource of the repeater to be the DataTable and call the repeater's DataBind method.

    HTH

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    106

    Re: ASP.NET VB in HTML body

    i tried setting <asp:Repeater id="rptTest" runat="server" DataSource='<%# DataTable.DefaultView %>'>

    it tells me DataTable is not declared. how do i declare it?

  9. #9
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: ASP.NET VB in HTML body

    As the DataTable is created dynamically you'll need to databind it to the repeater just after it is created.

    Code:
    If Session("Table") Is Nothing Then
    DataTable.Columns.Add("Code", GetType(String))
    DataTable.Columns.Add("Product", GetType(String))
    DataTable.Columns.Add("Quantity", GetType(Integer))
    DataTable.Columns.Add("Cost", GetType(Decimal))
    Session("Table") = DataTable
    Else
    DataTable = Session("Table")
    End If
    
    
    DataRow = DataTable.NewRow
    DataRow("Code") = DataSet.FieldValue("product_id", nothing)
    DataRow("Product") = DataSet.FieldValue("product_name", nothing)
    DataRow("Quantity") = Int32.Parse(txtQuantity.Text)
    DataRow("Cost") = Double.Parse(DataSet.FieldValue("product_cost", nothing))
    DataTable.Rows.Add(DataRow)
    
    rptTest.DataSource = DataTable.DefaultView
    rptTest.DataBind()
    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  10. #10

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    106

    Re: ASP.NET VB in HTML body

    works, thanks.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    106

    Re: ASP.NET VB in HTML body

    how do i do a remove_item? (to delete that datarow?)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width