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

老子叫甜甜 提交于 2019-12-01 16:09:53

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
}

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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!