简体   繁体   中英

How can I hide an HTML table row <tr> in aspx file and enable in code behind?

I have a very small search functionality and I have a table row called "Search Results", I want this table row get displayed whenever i have something to display from the search results. So I want to hide this row by default and enable via code behind when my search is fetching some result.

<div>
    <table>
        <tr id="srchResultHeader" style="display: none;" class="header">
            <td colspan="2" class="tdlogintitle" visible="false">Search Results</td>
        </tr>
        <tr>
            <td>/*Data to display actual result from database*/</td>
        </tr>
    </table>
</div>

I'm not able to get the reference of the above table id "srchResultHeader" in my code behind? What is wrong here in my code.

An id by itself is just a client-side identifier. In order for this to be referenced as a server-side object it needs to be a server-side control. The easiest way would just be to add runat="server" on the existing element:

<tr runat="server" id="srchResultHeader" style="display: none;" class="header" >

In this case you probably don't even need the style attribute, since you're controlling the hide/show functionality in server-side code. You can just set .Visible on the control to determine whether or not it renders to the client-side markup at all.

You could use server-side <asp:Table> for this very purpose. Otherwise <tr> is a client-side thing and is not directly accessible in the server-side code. <asp:Table> will render <table> tag on the client-side, but you can access it in the code-behind through its ID . The structure looks like this:

<asp:Table ID="MyTable" runat="server">
    <asp:TableRow runat="server" ID="MyRow1">
        <asp:TableCell>Some value</asp:TableCell>
    </asp:TableRow>
</asp:Table>

You can now write something like this in the code-behind:

MyRow1.Visible = False;

.aspx

  <tr id="divDriverName1"  runat="server" >
<td >  
<label class=" ">label1 </label>
<asp:TextBox ID="TextBox1" runat="server" class=" form-control"></asp:TextBox>  
</td>  
</tr>

.aspx.cs

           ContentPlaceHolder myPlaceHolder = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1"); 
         HtmlTableRow ct = (myPlaceHolder.FindControl("divDriverName1")) as HtmlTableRow;
             divDriverName1.Attributes.Add("style", "display:none");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM