0

I'm trying to change the CSS class and attributes of a set of asp.net controls via the code behind using this:

ASP.NET:

<span id="followbtn_mainbtn" runat="server" class="follow-btner" onclick="profile_followers_follow(this)">

        <img id="img_followingbtn" runat="server" class="profile-single-profile-follow-btn-img" src="icons/profico/following.png" style="display: none;">
        <img id="img_unfollowbtn" runat="server" class="profile-single-profile-unfollow-btn-img" src="icons/profico/unfollow.png" style="display: none;">
        <img id="img_followicon" runat="server" class="profile-single-profile-follow-icon" src="icons/profico/followIcon.png">

        <span id="span_following_text" runat="server" class="profile-single-profile-follow-btn-following">Follow</span>
        <span id="span_unfollow_text" runat="server" class="profile-single-profile-follow-btn-unfollow" style="display: none;">Unfollow</span>

      </span>

Code:

followbtn_mainbtn.Attributes.CssStyle.Add("className", "follow-btner-no-hoverer");
span_unfollow_text.InnerText = "Following";
img_followingbtn.Attributes.CssStyle.Add("display", "block");
img_unfollowbtn.Attributes.CssStyle.Add("display", "block");
span_unfollow_text.Attributes.CssStyle.Add("display", "block");

However when I run this, I do not see the desired results. If I hard code the appropriate classes to the controls, they work properly but the code doesn't do it dynamically.

What am I doing wrong?

3
  • Did you inspect your elements to see if the added attributes are actually there? Commented Sep 15, 2015 at 7:32
  • No they are not set. The default attributes remain unchanged @Bgl86 Commented Sep 15, 2015 at 7:38
  • @Earthling In which event are you changing the element CSS class under special case during Page_Load() or in some Click events? Commented Sep 15, 2015 at 7:58

3 Answers 3

1

Are you updating the css during a postback? First try adding the changes to your Page_Load method on the form and that will tell you that your code is working when setting the styles and classes. If the code works then I would make sure you have EnableViewState="false" on your page and/or parent control of the span.

Sign up to request clarification or add additional context in comments.

Comments

0

It was actually a simple error. I've used the wrong code to change the css class of the span named "followbtn_mainbtn".

I replaced this

followbtn_mainbtn.Attributes.CssStyle.Add("className", "follow-btner-no-hoverer");

with this

followbtn_mainbtn.Attributes["class"] = "follow-btner-no-hoverer";

Voila! Done deal. Thanks for all your answers and comments though :)

1 Comment

followbtn_mainbtn.CssClass="follow-btner-no-hoverer"; should work as well
0

You can use this code to change css in code behind

 img_followingbtn.Style.Add("display", "block");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.