在代码中隐藏div内的按钮

时间:2017-08-23 15:03:43

标签: c# html asp.net

我有一个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为#34; buttondiv&#34;的按钮,我该怎么做

在我的cs文件中,我尝试了这2件事,但它不起作用

Control myDiv = (Control)FindControl("buttondiv");
myDiv.Visible = false;

或者

foreach (Control c in contentid.Controls)
{
    if (c.ID == "buttondiv")
       {
           c.Visible = false;
       }
}

任何人都可以告诉我

2 个答案:

答案 0 :(得分:1)

为了用作服务器端控件,该按钮需要 服务器端控件。添加StopWatch sw = new StopWatch(); sw.start(); try (Statement st = conn.createStatement()) { st.execute("CREATE TABLE zzzTemp (id LONG, val DOUBLE)"); } double newDbl = ((Long) System.currentTimeMillis()).doubleValue(); // test data try (PreparedStatement ps = conn.prepareStatement("INSERT INTO zzzTemp (id, val) VALUES (?,?)")) { for (int i = 1; i <= 3600; i++) { ps.setInt(1, i); ps.setDouble(2, newDbl); ps.addBatch(); } ps.executeBatch(); } System.out.printf("Overall elapsed time: %d ms%n", sw.getTime()); try (Statement st = conn.createStatement()) { st.execute("MERGE INTO mytab m USING zzzTemp z ON m.id = z.id WHEN MATCHED THEN UPDATE SET m.val = z.val"); System.out.printf("Overall elapsed time: %d ms%n", sw.getTime()); st.execute("DROP TABLE zzzTemp"); System.out.printf("Overall elapsed time: %d ms%n", sw.getTime()); }

runat="server"

然后(除非你以某种方式打破了设计师,因为我已经使用过Web Forms已经有一段时间了)你的班级应该有一个<button class="abc bcd cde" title="Button" id="buttondiv" runat="server"> 对象,你可以设置它而不需要“找到”控制:

HtmlControl

答案 1 :(得分:0)

请将其保存在css文件中。

#buttondiv{
display:none;
}

或者如下所示应用样式内联:

<button class="abc bcd cde" title="Button" id="buttondiv" style="display:none"><span>Button</span></button>