Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi guys!
I have a question about ASP.NET data binding. I'm creating a site that
displays news for some artist...the main page News.aspx contains the whole list of news ordered by date descending....
and each news item has a Read More html link that points to a
ViewNews.aspx?id={id} page than a news id is passed through a query string..
The ViewNews.aspx page displays EXTENDED info about artist....
some news items have photos some don't....
I have an SQL Server table
 CREATE TABLE News(
  [News ID] uniqueidentifier PRIMARY KEY NOT NULL DEFAULT NEWID(),
  [Title] nvarchar(500) NOT NULL,
  [Digest] nvarchar(3000) NOT NULL, // short info
  [Content] ntext NOT NULL, // extended info
  [PhotoLink] varchar(100) NULL // for saving image path on server
);


The point is that ...I want to fully show my news item correctly...
if a news item doesn't have an associated image than I don't need to insert image into HTML, if there is an associated image with that news item then
it must be floatted left and text must be floated right
I do this:

<asp:SqlDataSource ID = "srcNewsSolo" runat = "server" 
        ConnectionString="<%$ ConnectionStrings:MusicDBConnString %>" 
        
        SelectCommand="SELECT [Title], [Content], [PhotoLink] FROM [News] WHERE ([News ID] = @News_ID)">
     <SelectParameters>
         <asp:QueryStringParameter Name="News_ID" QueryStringField="id"   />
     </SelectParameters>
 

<asp:DataList id = "soloNewsItem" DataSourceID = "srcNewsSolo" runat = "server>
<itemtemplate>
<span style="color: Orange;"><%# Eval("Title") %> </span>

<% if ( Eval("PhotoLink") != null) { %>
  <img src = "<%# Eval("PhotoLink") %>" style = "float: left; " />
<% } %>

<span style="color: White"> <%# Eval("Content") %> </span>

</itemtemplate>


I have some errors here ...how to fix them??? how to use Container.DataItem???
I don't have in Intellisense!!!!!!! How to dynamically decide whether to render photo or not .... Please help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Posted
Comments
thatraja 18-Jun-11 14:07pm    
/*I have some errors here ...how to fix them???*/
Include those error messages in your question.

hi,
use this code below,
<img src = '<%# Eval("PhotoLink") %>' style = 'float:left; visibility:<%# String.IsNullOrEmpty(Eval("PhotoLink").ToString())?"hidden":"visible" %>' />
 
Share this answer
 
Declare a public variable of the string type in C# code file of this aspx page.
store the path of this image in variable on the page load event then impose a if condition

]]>

]]>

where strPhotoLink is a public variable in c# code file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900