views:

44

answers:

2

I have some links on a page such as:

< a id="Digg" runat="server">< img alt="Digg" id="imgDigg" runat="server" src="~/resources/images/icons/social/digg_32.png" border="0" />< /a>

I have a database table that can "turn" them on or off, in my code behind I have the following:

string[] SocialMedia = new string[] { "Twitter", "Facebook", "LinkedIn", "Digg", "Email", "Print" };
        private void CheckForSocialMedia()
        {
            int i = 0;
            for (i = 0; i < SocialMedia.Length; i++)
            {
                bool AddSocialMedia = (bool)AllowSocialMedia(SocialMedia[i]);
                if (AddSocialMedia == false)
                {
                   // Hide the link: Digg.Visible = false;
                }
            }
        }

        protected bool AllowSocialMedia(string sSocialMedia)
        {
            SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString);
            SqlCommand myCommand = myConnection.CreateCommand();
            myCommand.CommandText = " select Settings.AllowsocialMedia  " +
                                    " from ArticleSocialMediaSettings Settings " +
                                    " inner join SocialMedia on Settings.SocialMediaId = SocialMedia.id " +
                                    " where SocialMedia.NetworkName = '" + sSocialMedia + "'" +
                                    " and Settings.RetailerId =  '1234'";

            myConnection.Open();

            try
            {
                SqlDataReader myReader = myCommand.ExecuteReader();
                if (myReader.Read())
                {
                    return (bool)myReader["AllowsocialMedia"];
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return true;
        }

How can I hide the link if the function returns false? Or is there a better way to do this?

A: 

It looks like that is the correct method for checking the database. To hide the link you can use:

document.getElementById("Digg").style.display = "none";

right where your comment is.

JasonOfEarth
I don't really want to register a start up script, can this be done in .net ?
Neil
I assume your Digg.Visible = false; didn't work. Did you try making the link into an asp:HyperLink with an ID and trying to set that !Visible.
JasonOfEarth
Im running an <a tag at server with an ID of the same name as the Array value, but I don't know how to cast or convert an array value to a HtmlAnchor - so where my comment is I can add something like SocialMedia[i].visible = false
Neil
Where my comment is, I added HtmlAnchor anchor = pnlSharing.FindControl(SocialMedia[i]) as HtmlAnchor; and it finds the anchor (i stepped through the code to check), so then when I try anchor.visible = false, I get a null reference exception!
Neil
Not sure if it's just quick typing but it should be anchor.Visible. Though getting a null reference means anchor is null. When you stepped through it found a value for anchor?
JasonOfEarth
Yes, when I stepped through the loop, the first iteration grabbed the Twitter link, along with its Href, Name and Target values etc so I'm not sure why it is null
Neil
A: 

Any other suggestions?

Neil
Redesigned it using a datalist
Neil