在asp.net控件中,此上下文不支持代码块

mat*_*amy 11 c# asp.net code-behind

我正在创建一个html表.我想要隐藏表格行.我将属性runat=serverid特定行放在一起,但该行中有客户端代码,类似于以下代码.

<% if ((strFlag=="d") || (strApprvdFlag=="y")) {%>
Run Code Online (Sandbox Code Playgroud)

调用此行后,我收到此错误.

在asp.net控件中,此上下文不支持代码块.

以下是我的示例代码:

<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>
Run Code Online (Sandbox Code Playgroud)

Kel*_*tex 18

当您runat='server'向HTML控件添加时,您更改了渲染,并且内部不支持代码块.因此,如果有需要更改的属性(样式?类?),您可能不得不这样做:

<tr id='myrow' runat='server'>
    <td> 
        your code here
    </td>
</tr>
Run Code Online (Sandbox Code Playgroud)

做这样的事情:

<tr id='myrow' <%= GetRowProperties() %>>
    <td> 
        your code here
    </td>
</tr>
Run Code Online (Sandbox Code Playgroud)

注意: runat='server'从中删除tr.然后在您的代码隐藏中,您可以执行以下操作:

protected string GetRowProperties()
{
    return "class='myclass'"; // something like this
}
Run Code Online (Sandbox Code Playgroud)

  • 您可以将其包装在<asp:placeholder>中 (3认同)

Ant*_*bry 5

您可以使用数据绑定来控制控件的可见性.那应该可以解决你的问题.

<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>
Run Code Online (Sandbox Code Playgroud)

在OnLoad方法中,您需要将DataBind()方法调用到PlaceHolder或包含它的任何控件,如tr或偶数Page:

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

    Page.DataBind();
}
Run Code Online (Sandbox Code Playgroud)