views:

44

answers:

4

I'm doing such a small web page that contain 4 main HTML div which should contain last 4 NEWS from a SQL Database.

so I've designed them, and put in each on a label control, and an Image control.

and to bind information from the SQL to them, I've used (DataView) object, and using a loop from 0 to 3 I've get the data I need.

but I believe that way is not the perfect one! so I need something more clear and effectiveness!

A: 

You could retrieve data using LINQ.

To exemplify:

var latestNews = (from n in db.News orderby n.Date descending select n).Take(4).ToList();

label1.Text = latestNews[0].Content;
label2.Text = latestNews[1].Content;
label3.Text = latestNews[2].Content;
label4.Text = latestNews[3].Content;
Leniel Macaferi
This is just a redescription of his current solution
Roger Willcocks
After you're edits, your still missing the point. What if he wants to suddenly select the top 10 news articles? He would need to then add another 6 lines of code. Restrictions of data are best done where their responsibility lies - in the DAL or the database itself. The UI should be a simple binding operation to the data that it is presented with.
RPM1984
I'm just addressing the question he asked with a simple example... :) I agree with your points about separation of concerns but the asker's problem is about a such small web page. I think there's no DAL/layers involved here.
Leniel Macaferi
Agreed, and understand (didnt mean to sound rude). Just figured as though it seems the OP is new to asp.net and databinding, might be best to start him off on the right foot. =)
RPM1984
I Really Thank You Dear.
Hashem Alrifai
+1  A: 

create an <asp:repeater ... /> control on your page and bind it to your SQL data source.
Create the appropriate Select statement within your data source (or use LINQ, or whatever you like).

layout the repeater control something like this

        <asp:Repeater ID="Repeater1" runat="server" DataSourceID="MyDataSource">
            <HeaderTemplate>
                <div style="height: 30px;">
                     Header info
                </div>
            </HeaderTemplate>
            <ItemTemplate>
               <div>
                  <%# Eval("SomeInfo")%>
                  <img src='<%# Eval("SomeImage")%>' title='<%# Eval("SomeImageTitle")%>' alt='<%# Eval("SomeImageAlt")%>' />
               </div>
            </ItemTemplate>
            <SeparatorTemplate>
                <hr />
            </SeparatorTemplate>
            <FooterTemplate>
                <div style="height: 25px;">
                       Footer Stuff
                </div>
            </FooterTemplate>
        </asp:Repeater>
rockinthesixstring
@RPM1984 mentioned LinqDataSource, and he's probably right. You should probably use LinqDataSource instead of SqlDataSource.
rockinthesixstring
Thanks, worked great! but I don't have Linq skills.but I'll get them absolutely..Thanks again.....
Hashem Alrifai
Please don't forget to mark the answer
rockinthesixstring
A: 

Hi John.

Assuming that your data view contains only the 4 items you want to display, you probably want to make use of one of the DataRepeater style ASP.NET controls.

You bind the DataView to the controls Datasource property, and call .DataBind() for the page or the control (the page call is recursive over all controls)

Inside the DataRepeater will be a "Template" control, the content of which would be something like

<div style="st1"><%# Eval("NewsText") %>&nbsp;<img src='<%# Eval("ImgSrc") %>' /></div>
Roger Willcocks
why are you answering the question for "John"? the user is Hashem ;-)
rockinthesixstring
So it is. I misread the author of the comment against the question as being the author of the question. Sorry Hashem.
Roger Willcocks
Thanks dear for the answer :)and no problem about the misreading :DKnowledge is the purpose, Right?
Hashem Alrifai
+1  A: 

Don't know what version of .NET you are using, so that may affect this answer, but...

LinqDataSource would be a nice way of doing this, as you can specify data-level filtering (i.e. TOP 3), instead of page-level filtering (in memory looping).

RPM1984
Thanks dear, I'm using ASP.NET 4.0I'll make some Linq skills and apply them... :)
Hashem Alrifai