Erdem Gundogdu
Erdem Gundogdu

Reputation: 297

Set visibility of a table row in HTML with C# codes?

i want to show/hide a table row with using DateTime.Now.Month in html codes but i couldn't remember the correct syntax. What i mean is above, but not working in this way. What is the correct syntax? Thanks in advance

<tr style='<%# DateTime.Now.Month==11? "display:none": "display:inline"%>' ></tr>

Upvotes: 2

Views: 1870

Answers (3)

Adil
Adil

Reputation: 148150

You are using javascript in style tag which is not executed. You can use javascript on body onload event or jquery document.ready event to execute your script.

You are using 2 digit year and you would be getting 4 digit year, you need to change 11 to 2011.

Using Javascript

<body onload="CallTrShowHIde();">
    <table>    
      <tr id="tr1"  onload='alert("ac")' ><td>hello123</td></tr>
    </table>
</body>

function CallTrShowHIde()
{
      var year = '<%= DateTime.Now.Year %>';
      if (year == 2012)
            document.getElementById('tr1').style.display="none";
        else
            document.getElementById('tr1').style.display = "inline";  
 }

Using jQuery.

<tr id="tr1">
     <td>Show or hide </td>
</tr>

 $(function () {
        var year = '<%= DateTime.Now.Year %>';
        if (year == 2012)
            $('#tr1').hide();
        else
            $('#tr1').hide();
 });

Upvotes: 2

Valentin
Valentin

Reputation: 2858

Asp tags come in a variety of flavors. The code you've written is correct but because you're using the wrong asp tags, nothing shows up.

This is the tag you want to use: <%= %> most useful for displaying single pieces of information

This is the tag you're currently using: <%# %> Data Binding Expression Syntax This doesn't work because you're not using data binding, you're just echoing a string.

More info here: http://forums.asp.net/p/1139381/1828702.aspx and here http://msdn.microsoft.com/en-us/library/6dwsdcf5.aspx

Upvotes: 2

Lost in Alabama
Lost in Alabama

Reputation: 1653

DateTime.Now.Year should probably be DateTime.Now.Month

Upvotes: 3

Related Questions