我正在创建一个 html 表格。我想隐藏表格行。我正在为特定行放置属性 runat=server
和 id
,但该行中包含类似于以下代码的客户端代码。
<% 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
</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">
<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/