[英]Hiding a button inside a div from code
我有一個aspx頁面,我想根據我的某些條件從cs文件中隱藏按鈕
我的.aspx看起來像:
<asp: Content Id="contentid" >
<% if (!IsRedeemCardFlowOptin)
{ %>
<ul id="ulid" class="abc">
</ul>
<div class="bcd" id ="div1">
<div id="div2"></div>
<div id="div3"></div>
<div id="div4" runat="server">
<h4><%= m_AutoRenewInfo.NewPageContent.ArCsidOffHeader%></h4>
<button class="abc bcd cde" title="Button" id="buttondiv"><span>Button</span></button> //Want to hide this button
</div>
</div>
<% } %>
</asp:Content>
現在在CS文件中,我想隱藏ID為“ buttondiv”的按鈕,我該怎么做
在我的cs文件中,我嘗試了這2件事,但是它不起作用
Control myDiv = (Control)FindControl("buttondiv");
myDiv.Visible = false;
要么
foreach (Control c in contentid.Controls)
{
if (c.ID == "buttondiv")
{
c.Visible = false;
}
}
誰能告訴我
為了用作服務器端控件,該按鈕將需要是服務器端控件。 添加runat="server"
:
<button class="abc bcd cde" title="Button" id="buttondiv" runat="server">
然后(除非您以某種方式破壞了設計器,自從我使用Web Forms已經有一段時間了),您應該在您的類中具有一個HtmlControl
對象,您可以對其進行設置,而無需“查找”控件:
this.buttondiv.Visible = false;
請將此保存在css文件中。
#buttondiv{
display:none;
}
或如下應用內聯樣式:
<button class="abc bcd cde" title="Button" id="buttondiv" style="display:none"><span>Button</span></button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.