IF ELSE type functionality ASP Repeater Web Control. Display or not display a certain HTML code based on database value.

Hello World,

We are currently building a piece of functionality for a record company's website where we display track listings for a particular album. We are using a simple SQL query then databinding that to an ASP:Repeater control for the UI. Pretty simple stuff. The problem we are having is we would like to have a IF ELSE type check for the repeater where it would hide or show a certain HTML snippet base on a column value in the row. For this example if the column value in the database is not null then show the html else hide the html. How would we go about tackling this? I search the web far and wide without any luck. This is the snippet of the code I have. Is there something built into the repeater object to handle this?

      void getTracks() {            
                        SqlCommand objSqlCommand;
                        SqlDataAdapter daSqlAdapter;
                        SqlConnection objSqlConn;
                        DataSet dsDataSet;
                        string sql_str = "SELECT * FROM tracksTbl WHERE albumID = " + Request.QueryString["albumID"];
            
                        dsDataSet = new DataSet();
                        objSqlConn = new SqlConnection(ConfigurationSettings.AppSettings["uaConnection_str"]);
                        objSqlCommand = new SqlCommand(sql_str, objSqlConn);
      
                        daSqlAdapter = new SqlDataAdapter(objSqlCommand);
                        daSqlAdapter.Fill(dsDataSet, "albumTracks");
                        
                        rptAlbumTracks.DataSource = dsDataSet.Tables["albumTracks"];
                        rptAlbumTracks.DataBind();
      }

<asp:Repeater ID="rptAlbumTracks" runat="server">
     <ItemTemplate>
          <div class="tracks">
                    <div class="trackIcon"><img src="images/trackIcon.gif" width="11" height="11"></div>
                    <div class="trackTitle"><%# DataBinder.Eval(Container.DataItem, "trackTitle") %></div>
                    <div class="trackIcon"><a href="player.aspx?url="<%# DataBinder.Eval(Container.DataItem, "trackURL") %>">BUTTON</a></div>
          </div>
     </ItemTemplate>
</asp:Repeater>


The "<div class="trackIcon">" is what I want to hide or show if "trackURL" is not equal to "" or NULL. There must be an easy way to do this since I would think this is a common scenario. Any help would be greatly appreciated.

Michael
coregenicAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GavinMannionCommented:
Take a look at the ItemDataBound event of the Repeater control. Inside there you should be able to evaluate your data and modify the look and feel as you see fit
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
coregenicAuthor Commented:
Gavin,

I'll take a look at this. Is there a quick and dirty example you can show me? Thanks.
0
GavinMannionCommented:
quick and dirty... no problem

<asp:Repeater ID="rptAlbumTracks" runat="server" OnItemDataBound="ComputeSum">
     <ItemTemplate>
          <div class="tracks">
                    <div class="trackIcon"><img src="images/trackIcon.gif" width="11" height="11"></div>
                    <div class="trackTitle"><%# DataBinder.Eval(Container.DataItem, "trackTitle") %></div>
                    <div class="trackIcon"><a href="player.aspx?url="<%# DataBinder.Eval(Container.DataItem, "trackURL") %>">BUTTON</a></div>
          </div>
     </ItemTemplate>
</asp:Repeater>

Then in codebehind

protected void itemDataBoundRepeater_ItemDataBound(object source, RepeaterItemEventArgs e) {
//Not sure here but look at e.Item.DataItem to find the URL
//You will notice I changed it from a div to an ASp.Net pane
Panel myDiv = (Panel)e.Item.FindControl("tracks");
myDiv.visible = false;
}

Sorry it is late in SA at the moment, if you cannot find the URL datafield just let me know and when I wake up I will think properly again (hopefully) and post an answer :)
l
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

coregenicAuthor Commented:
Gavin,

I don't understand your example as the way you did it, it would just hide a global panel, I need it to hide the div. How do I find the URL in e.item.dataitem? Sorry for the newbie question but this is confusing to me.

Thanks!
0
GavinMannionCommented:
Change your div (tracks) to be a panel called tracks.

It will then show/hide the panel which renders as a div...

To get the url

string URL = (string)DataBinder.Eval(e.Item.DataItem, "TrackURL");



0
coregenicAuthor Commented:
You can't access a property of a asp web control from inside of a asp.net repeater as when it repeats it would generate multiple panels with the same ID. I ended up using a user control like so... The logic is then placed the user control. After some serious heavy lifting I think this is the only way to accomplish something like this.

on .aspx page:

    void itemDataBoundRepeater_ItemDataBound(object source, RepeaterItemEventArgs e)
    {
            ((trackListingControl)e.Item.FindControl("ucTrack")).SetTrackPlayer(((DataRowView)e.Item.DataItem));
    }

<asp:Repeater ID="rptAlbumTracks" runat="server" OnItemDataBound="itemDataBoundRepeater_ItemDataBound">
      <ItemTemplate>
            <uc1:ucTrackClip ID="ucTrack" runat="server" />
      </ItemTemplate>
</asp:Repeater>



on user control

    public void SetTrackPlayer(DataRowView dr)
    {
        this.lblTrackTitle.Text = (string)dr["trackTitle"];

        if (dr["trackClipUrl"].ToString() != "")
        {
            this.litTrackClip.Text = (string)dr["trackClipUrl"];
        }
        else
        {
            this.litTrackClip.Text = "NO TRACKS";
        }    
    }


<div class="tracks">
        <div class="trackIcon"><img src="images/trackIcon.gif" width="11" height="11"></div>
        <div class="trackTitle"><asp:label runat=server ID=lblTrackTitle /></div>
        <asp:Literal runat=server ID=litTrackClip />
</div>

0
GavinMannionCommented:
Strange that since I realise it will repeat multiple but since you are only looking through one line at a time it should still find it.

but asp.net does strange things sometimes... Glad you got it working though ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.