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

标签 c# asp.net code-behind

我正在创建一个 html 表格。我想隐藏表格行。我正在为特定行放置属性 runat=serverid,但该行中包含类似于以下代码的客户端代码。

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

调用此行后,出现此错误。

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

下面是我的示例代码:

<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>

最佳答案

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

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

做这样的事情:

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

注意:runat='server' 已从 tr 中删除。然后在你的代码隐藏中你可以做这样的事情:

protected string GetRowProperties()
{
    return "class='myclass'"; // something like this
}

关于c# - 在 asp.net 控件中,此上下文不支持代码块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4864138/

相关文章:

c# - Microsoft AVRO 是否能够序列化 ConcurrentDictionary?

c# - EF TPH 继承查询

c# - 我需要删除循环绘制的图形

c# - 如何从后面的代码向我的 div 添加点击事件?

重新加载页面时,asp.net session 丢失(ispostback = false)

asp.net - .net开发动态创建控件需要自己的方法

c# - VS2017 中的 XAML 中的 DesignInstance 在设计时不显示数据

c# - 计算 Web 应用程序的在线用户数

c# - 需要在常规超链接上执行隐藏代码

c# - 如何在代码隐藏中添加 GridView 列?