Lucian Tarna
Lucian Tarna

Reputation: 1827

Checking a query Result in ASP

In my database i have a Table called News. In News i have a column named Link. This link CAN be null. If the article is written by the editors the link will be null , if the editors just want to reference an article from another site then this Link will contain a value. My task: Make a href to the article. Here i have a problem if my editor writes the article i put a href to that article . If it is not written(so the link is not null) i have to pur that href insteaf. I have no idea how to do this, any tips ? Code: Display:

<asp:DropDownList ID="DropDownSelect" runat="server" AutoPostBack="True" 
            OnSelectedIndexChanged="DropDownSelect_SelectedIndexChanged">
       <asp:ListItem Selected="True" Value="DesDate"> Descending Date </asp:ListItem>
       <asp:ListItem Value="AsDate"> Ascending Date </asp:ListItem>
       <asp:ListItem Value="AsAlp"> Ascending Alphabetical </asp:ListItem>
       <asp:ListItem Value="DesAlp"> Descending Alphabetical </asp:ListItem>
        </asp:DropDownList>
<asp:ListView ID="productList" runat="server" 
                DataKeyNames="NewsID" GroupItemCount="1"
                ItemType="SiteStiri.Models.News" SelectMethod="GetProducts">
            <EmptyDataTemplate>
                    <table >
                        <tr>
                            <td>No data was returned.</td>
                        </tr>
                    </table>
                </EmptyDataTemplate>
                <EmptyItemTemplate>
                    <td/>
                </EmptyItemTemplate>
                <GroupTemplate>
                    <tr id="itemPlaceholderContainer" runat="server">
                        <td id="itemPlaceholder" runat="server"></td>
                    </tr>
                </GroupTemplate>
            <ItemTemplate>
                    <td runat="server">
                        <table>
                            <tr>
                                <td>
                                    <a href="NewsDetails.aspx?newsID=<%#:Item.NewsID%>">
                                        <img src="/Catalog/Images/Thumbs/<%#:Item.ImagePath%>"
                                            width="100" height="75" style="border: solid" /></a>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <a href="NewsDetails.aspx?newsID=<%#:Item.NewsID%>">
                                        <p style="color: black;">
                                            <%#:Item.NewsTitle%>
                                        </p>
                                    </a>

                                </td>
                            </tr>
                            <tr>
                                <td>&nbsp;</td>
                            </tr>
                        </table>
                        </p>
                    </td>
                </ItemTemplate>
            <LayoutTemplate>
                    <table style="width:100%;">
                        <tbody>
                            <tr>
                                <td>
                                    <table id="groupPlaceholderContainer" runat="server" style="width:100%">
                                        <tr id="groupPlaceholder"></tr>
                                    </table>
                                </td>
                            </tr>
                            <tr>
                                <td></td>
                            </tr>
                            <tr></tr>
                        </tbody>
                    </table>
                </LayoutTemplate>
            </asp:ListView>
        </div>

Behind the page:

public IQueryable<News> GetProducts()
        {
            var _db = new SiteStiri.Models.NewsContext();
            IQueryable<News> query = _db.News;

            if ("DesDate".Equals(DropDownSelect.SelectedItem.Value))
            {
                query = query.OrderByDescending(u => u.ReleaseDate);
            }

            if ("AsDate".Equals(DropDownSelect.SelectedItem.Value))
            {
                query = query.OrderBy(u => u.ReleaseDate);
            }
            if ("AsAlp".Equals(DropDownSelect.SelectedItem.Value))
            {
                query = query.OrderBy(u => u.NewsTitle);
            }
            if ("DesApl".Equals(DropDownSelect.SelectedItem.Value))
            {
                query = query.OrderByDescending(u => u.NewsTitle);
            }

            return query;
        }

        public void DropDownSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetProducts();
            productList.DataBind();
        }

How can i put here <a href="NewsDetails.aspx?newsID=<%#:Item.NewsID%>"> either the page link to my site if Link IS null or the Link of the link IS NOT null?

Upvotes: 0

Views: 127

Answers (1)

Christian Phillips
Christian Phillips

Reputation: 18749

To give a more detailed answer, set the Link column as NOT NULL and set the default value to be your URL. This was, if the user / news editor doesn't enter a value, your URL will be added to the field. The advantage of this is that you don't need any code to check for NULL etc.

Also, make sure the UI validates the URL that's added with regular expression, in case a rogue value is added.

If you want to alter it in code, you could try something like this...

public class News
{
    public string Title { get; set; }
    public string Url { get; set; }

    public IQueryable<News> GetNews()
    {
        var news = new List<News> {new News {Title = "News1", Url = "NewsURL"}, 
                                   new News {Title = "News1"}};
        return news.AsQueryable(); 
    }
}

You could then take out the list with no links and update them with your link...

var news = new News();
var initialNews = news.GetNews();

var newsWithLink = initialNews.Where(n => n.Url != null);
var newsWithOutLink = initialNews.Where(n => n.Url == null);

foreach (var newsItem in newsWithOutLink)
{
   newsItem.Url = "MyURL";
}

var newsToDisplay = newsWithLink.Concat(newsWithOutLink);

I've just put that small example together to show how to update the links.

Upvotes: 1

Related Questions