[英]CSS Style not being set Dynamically in ASP.NET Code behind
我試圖通過使用此后面的代碼來更改一組asp.net控件的CSS類和屬性:
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>
碼:
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");
但是,當我運行此程序時,看不到預期的結果。 如果我將適當的類硬編碼到控件中,則它們可以正常工作,但代碼不會動態執行。
我究竟做錯了什么?
您是否在回發期間更新CSS? 首先嘗試將更改添加到窗體上的Page_Load方法,這將告訴您設置樣式和類時代碼正在工作。 如果代碼有效,那么我將確保您在頁面上和/或跨度的父控件上具有EnableViewState =“ false”。
這實際上是一個簡單的錯誤。 我使用了錯誤的代碼來更改跨度的CSS類“ followbtn_mainbtn”。
我替換了這個
followbtn_mainbtn.Attributes.CssStyle.Add("className", "follow-btner-no-hoverer");
有了這個
followbtn_mainbtn.Attributes["class"] = "follow-btner-no-hoverer";
瞧! 木已成舟。 謝謝您的所有回答和評論:)
您可以使用此代碼在后面的代碼中更改CSS
img_followingbtn.Style.Add("display", "block");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.