2011-02-01 27 views
11

我创建了一个html表格。我想隐藏表格行。我将属性runat=serverid用于特定的行,但该行中的客户端代码类似于以下代码。代码块在asp.net控件的上下文中不受支持

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

打完电话后,我得到了这个错误。

代码块是不是在这方面在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> 

回答

17

当您添加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 
} 
+0

喜非常感谢你..我得到这个错误服务器标签不能包含<% ... %>构造。 – mathirengasamy 2011-02-01 15:14:28

+1

你的行不能有runat ='server'在里面。 – Keltex 2011-02-01 15:15:20

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> 

在你的OnLoad方法,你将需要调用的DataBind()方法要么占位符,或包含的任何控制,如在TR或偶数页:

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

    Page.DataBind(); 
} 
相关问题