[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

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
0
coregenic
Asked:
coregenic
  • 4
  • 3
1 Solution
 
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
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now