code blocks are not supported in this context in asp.net control
Asked Answered
P

2

13

I'm creating one html table. I want to hide the table row. I'm putting the attributes runat=server and id for the particular row, but the row has client side code in it similar to the following code.

<% if ((strFlag=="d") || (strApprvdFlag=="y")) {%>

After call this line, I got this error.

Code blocks are not supported in this context in asp.net control.

Below is my sample code:

<table>
  <tr colspan="4" ID="trMedical"  scriptrunat="server">
    <td style="WIDTH: 45px;HEIGHT: 12px" align="left" class="LabelTaxText" width="45"><b>G&nbsp;
        </b>
    </td>
    <td style="WIDTH: 182px;HEIGHT: 12px" class="LabelTaxText" align="left" width="182"
        colSpan="2">Medical
    </td>
    <td style="WIDTH: 81px; HEIGHT: 12px" align="right" class="LabelTaxText" width="81">
        <asp:textbox onchange="onChangeFlag(),intOnly(this);" onkeyup="intOnly(this);" onkeypress="return CheckNumericWithOutDecimals(event)"
            id="TxtMedical" tabIndex="24" runat="server" Width="96px" MaxLength="12" style="Z-INDEX: 0"></asp:textbox>
    </td>
    <% if ((strFlag=="d") || (strApprvdFlag=="y")) {%>
        <td class="LabelTaxText" style="WIDTH: 107px; HEIGHT: 12px" align="right" width="107">
            <asp:textbox onchange="onChangeFlag(),intOnly(this);" onkeyup="intOnly(this);" onkeypress="return CheckNumericWithOutDecimals(event)" id="TxtMedicalProof" tabIndex="24"     onblur="charAlert(TxtMedical,TxtMedicalProof)" runat="server" MaxLength="12" Width="96px">
            </asp:textbox>
        </td>
    <% } %>
    <% if (strApprvdFlag=="y") {%>
        <td class="LabelTaxText" style="WIDTH: 68px; HEIGHT: 24px" align="right" width="68">
            <asp:textbox id="TxtMedicalApproved" tabIndex="24" runat="server" MaxLength="12" Width="96px"></asp:textbox>
        </td>
        <td class="LabelTaxText" style="WIDTH: 43px">&nbsp;
            <asp:Label ID="lblMedicalRemarks" Runat="server"></asp:Label>
        </td>
    <% } %>
  </tr>
</table>
Predicative answered 1/2, 2011 at 15:3 Comment(0)
M
18

When you add a runat='server' to an HTML control you change the rendering and code blocks aren't supported inside. So if there are properties you need to change (style? class?) you probably have to instead of doing this:

<tr id='myrow' runat='server'>
    <td> 
        your code here
    </td>
</tr>

Do something like this:

<tr id='myrow' <%= GetRowProperties() %>>
    <td> 
        your code here
    </td>
</tr>

Note: runat='server' removed from tr. Then in your codebehind you can do something like this:

protected string GetRowProperties()
{
    return "class='myclass'"; // something like this
}
Minneapolis answered 1/2, 2011 at 15:7 Comment(6)
hi thank you so much..now i got this error Server tags cannot contain <% ... %> constructs.Predicative
Your row CANNOT have runat='server' in it.Minneapolis
but i need runat server property..then how can i hide the row in code behind..give any ideaPredicative
You can wrap it in an <asp:placeholder>Minneapolis
Why can't you have runat='server' at a row?Spermic
This is not going to work if you have in GetRowProperties a value thats changing like return "id='" + rowId.ToString() + "'";Spermic
A
6

You can use data binding to control the visibility of a control. That should solve your problem.

<tr runat="server">

    some content...

    <asp:PlaceHolder runat="server"
        visible='<%# (strFlag=="d") || (strApprvdFlag=="y") %>'>

        This content will only be rendered if strFlag is "d" or "y"

    </asp:PlaceHolder>

    more content...

</tr>

On your OnLoad method, you will need to call the DataBind() method to either the PlaceHolder, or any control that contains it, like the tr or even Page:

protected override void OnLoad(EventArgs e) {
    base.OnLoad(e);

    Page.DataBind();
}
Aliment answered 1/2, 2011 at 15:16 Comment(2)
i put the place holder inside the table row... but i got this error System.Web.UI.HtmlControls.HtmlTableCellCollection must have items of type 'System.Web.UI.HtmlControls.HtmlTableCell'. 'asp:PlaceHolder' is of type 'System.Web.UI.WebControls.PlaceHolder'.Predicative
I never tried to use this technique into a <tr runat="server">. Do you really need the TR to be a server-side control?Aliment

© 2022 - 2024 — McMap. All rights reserved.