Results 1 to 9 of 9

Thread: I'm so confused!

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870

    I'm so confused!

    Hello everyone,

    Right, here's what i am trying to do. I have a database which i store details of articles on my website. I want to read some records and display them (top 10 say). Each record is a date, title, small desc, and url. I want them then displayed on my web page one after the other.

    Any tips on how to do this as i'm stuck and this is the first asp.net thing i'm trying!!!

    Thanks
    Nick
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Sure. Use a datagrid on the web page. Next, in your page load, get a dataset from the database with all the information in it. Next, set the datagrids datasource to the dataset, and then call the datagrids databind method.

    (wrap it into a if not page.isPostBack block)

    Example:

    C#:
    Code:
    if(!Page.IsPostBack)
    {
       DataSet ds = AMethodThatReturnsADataSetFromYourDB();
    			
       ArticleGrid.DataSource = ds;
       ArticleGrid.DataBind();
    }
    or if VB.Net:
    Code:
    If Not Page.IsPostBack
        Dim ds As DataSet = AMethodThatReturnsADataSetFromYourDB()
       ArticleGrid.DataSource = ds
       ArticleGrid.DataBind()
    End If

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    You can also use datareader objects as a datasource along with others.

    Also, you can customize the grid even further by editing the collections property.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    yeah that's the way it says to do it in the books i got. I didn't want to uses datagrids really as i don't like using bound objects. I wanted to dynamically create the labels and url objects in a loop once i got a dataset.

    I'll try using the datagrid and then perhaps the other approach.

    Thanks
    nick
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    There are other ways to do it. If you use the Table server control, you can create a row object, and cell objects. Then you fill the cell object, add it to the row objects control collection, and then add that row object to the table object.

    I have done this in the past, it is pretty easy to do, but to tell you the truth, the datagrid is faster, and much less code to implement. I actually have switched over all my dynamically created tables to datagrids because it completely simplifies the code.

    I will write some code on how to do this so you have an example.
    Give me a few minutes.

  6. #6
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I didn't know if you use C#, or VB.Net. I did it in C#, but you can easily convert it.
    PHP Code:
    // First, your code to fill out a dataset goes here.


    // Create the header row:
    TableRow headerRow = new TableRow();
    // Create the cell objects to go in the tablerow.
    TableCell headerCell1 = new TableCell();
    TableCell headerCell2 = new TableCell();
    // Create some literal controls sw you can format the text with html.
    // The first is for column one, the second for column two.
    LiteralControl lc1 = new LiteralControl();
    LiteralControl lc2 = new LiteralControl();

    // Create the text for header cell 1.            
    lc1.Text "<b>First Column</b>";
    // Add the literal control to the cell.
    headerCell1.Controls.Add(lc1);

    // Create the text for header cell 2.
    lc2.Text "<b>Second Column</b>";
    // Add the literal control to the cell.
    headerCell2.Controls.Add(lc2);

    // Add the two completed cells to the row object.
    headerRow.Cells.Add(headerCell1);
    headerRow.Cells.Add(headerCell2);

    // Add the row object to the table.
    Table1.Rows.Add(headerRow);

    // Now add the items.
    foreach(DataRow dr in ds.Tables[0].Rows)
    {
       
    // Create a table row object and two cell objects.
       
    TableRow tr = new TableRow();
       
    TableCell tc1 = new TableCell();
       
    TableCell tc2 = new TableCell();

       
    // Create a literal control for cell 1, and a hyperlink control for cell 2.
       
    LiteralControl litCont = new LiteralControl();
       
    HyperLink hl = new HyperLink();

       
    // Set the text of the literalcontrol to a column in the datarow.
       
    litCont.Text dr["Column1"].ToString();
       
    // Add the literalcontrol to the first cell.
       
    tc1.Controls.Add(litCont);

       
    // Set the text of the hyperlink control to a column in the datarow.
       
    hl.Text dr["Column2"].ToString();
       
    // Create the url for this link dynamically by using a querystring value
       // from the datarow.
       
    hl.NavigateUrl "http://www.myhyperlink.com/index.aspx?CatName=" 
                      
    dr["Column3"].ToString();
       
    // Add the hyperlink control to the second cell.
       
    tc2.Controls.Add(hl);

       
    // Add the cells to the row.
       
    tr.Cells.Add(tc1);
       
    tr.Cells.Add(tc2);

       
    // Add the row to the table.
       
    Table1.Rows.Add(tr);


  7. #7
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    To do the exact same with a datagrid, here is all that is needed:
    In the code:
    Code:
    DataGrid1.DataSource = ds;
    DataGrid1.DataBind();
    In the HTML:
    Code:
    <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
       <Columns>
           <asp:BoundColumn DataField="Column1" HeaderText="Column 1"></asp:BoundColumn>
           <asp:TemplateColumn HeaderText="Column 2">
               <ItemTemplate>
                   <asp:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%# DataBinder.Eval(Container, "DataItem.Column3", "http://www.yoursite.com/index.aspx?CatName={0}") %>' Text='<%# DataBinder.Eval(Container, "DataItem.Column2") %>'>
                   </asp:HyperLink>
               </ItemTemplate>
             </asp:TemplateColumn>
       </Columns>
    </asp:DataGrid>

  8. #8
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    There is also the repeater control and datalist control you can use. Just thought I would throw in that info. Good luck.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    thanks mate,

    i'll try that tomorrow. off to bed now as my brain is fried!

    cheers
    Nick
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

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