在非ASP元素中使用代码服务器端脚本分隔符/ ASP.NET内联表达式

时间:2022-02-10 15:12:46

I get this error message:

我收到此错误消息:

Cannot create an object of type 'System.Boolean' from its string representation <%: false %> for the 'Visible' property.

无法从其“可见”属性的字符串表示形式<%:false%>创建“System.Boolean”类型的对象。

When I try to run this code within my ASP.net website:

当我尝试在我的ASP.net网站中运行此代码时:

<a runat="server" visible='<%: false %>' href="~/" >Home</a>

Is there a syntax error? false should be replaceable by any method result same with:

有语法错误吗? false应该可以通过任何方法替换为:

<asp:Panel runat="server" Visible='<%: GetTrueOrFalse() %>'>Home</a>

4 个解决方案

#1


1  

You can also declare a public boolean and use that. You will need to use DataBind() if the link is outside a GridView/Repetater etc.

您还可以声明一个公共布尔值并使用它。如果链接在GridView / Repetater等之外,则需要使用DataBind()。

public bool isVisible = true;

protected void Page_Load(object sender, EventArgs e)
{
    DataBind();
}

Now you can use that on the aspx.

现在你可以在aspx上使用它了。

<a runat="server" visible='<%# isVisible %>' href="~/">Home</a>

However you can also use a ternary operator based on a different variable or class value within your code.

但是,您也可以在代码中使用基于不同变量或类值的三元运算符。

public int myValue = 11;
public Book myBook = new Book() { category = "Fantasy" };

protected void Page_Load(object sender, EventArgs e)
{
    DataBind();
}

Now you can set the visibility based on myValue, even though it is not a Boolean.

现在,您可以基于myValue设置可见性,即使它不是布尔值。

<a runat="server" visible='<%# myValue > 10 ? true : false %>' href="~/">Home</a>

//or

<a runat="server" visible='<%# myBook.category == "Fantasy" ? true : false %>' href="~/">Home</a>

#2


2  

Suppose you have a method that returns bool value like this:

假设您有一个返回bool值的方法,如下所示:

public bool IsVisible()
{
    if (some_condition) // example condition test
    {
        return true;
    }
    else
    {
        return false;
    }
}

You need to use binding like this:

你需要像这样使用绑定:

ASPX

ASPX

<a runat="server" visible='<%# IsVisible() %>' href="~/" >Home</a>

ASPX.CS (Code-behind)

ASPX.CS(代码隐藏)

protected void Page_Load(object sender, EventArgs e)
{
    // do something

    Page.DataBind();
}

NB: This trick apply for either methods or properties which returns bool.

注意:这个技巧适用于返回bool的方法或属性。

Update 1:

更新1:

Since a tag doesn't set any id attribute, you can remove runat="server":

由于标记未设置任何id属性,因此可以删除runat =“server”:

<a visible='<%# IsVisible() %>' href="~/" >Home</a>

Or use CSS with display: none or visibility: hidden:

或者使用CSS with display:none或visibility:hidden:

<a visible='<%# IsVisible() %>' href="~/" style="visibility:hidden; display:none;">Home</a>

Reference:

参考:

Is code rendering block <%=%> useful for boolean type?

代码渲染块<%=%>对布尔类型有用吗?

#3


1  

Learn this technique you're gonna use it a lot. Server side controls or containers can easily be manipulated server side. How? well you did the first part right you game it runat="server" now all you have to do is to give it an id so it looks something like this let's name the id MyLink

学习这项技术你会经常使用它。服务器端控件或容器可以很容易地在服务器端进行操作。怎么样?嗯,你做了第一部分你游戏它runat =“服务器”现在你所要做的就是给它一个id所以它看起来像这样让我们命名为id MyLink

<a runat="server" id="MyLink" href="~/" >Home</a>

-Now you noticed that I've removed the attribute Visible. yes because now we're gonna take full control of it server side wise. Let's assume you wanna start off the page first time with a hidden , well that's easy: in your pageload event we'll use a good technique to determine that the code we'll write will only run once at the very first load.

- 你注意到我删除了属性Visible。是的,因为现在我们将完全控制服务器方面。让我们假设您想首次使用隐藏的页面开始,这很容易:在您的页面加载事件中,我们将使用一种好的技术来确定我们编写的代码只会在第一次加载时运行一次。

    protected void Page_Load(object sender, EventArgs e)
    {

       //this condition means if is not post back (meaning the very first time only)

        if(!IsPostBack)
          {
            MyLink.Visible = false;
          }
    } 

Now you got it you can simply make your control visible again whenever wherever you want simply

现在你已经掌握了它,你可以随时随地再次看到你的控件

MyLink.Visible = true; 

and done. lemme know if you need any more help!

并做了。 lemme知道你是否需要更多的帮助!

if you wanna do it inline it's a string value not bool so you should wrap it in double quotes visible='<%: "false" %>' <= notice the ""

如果你想内联它是一个字符串值而不是bool所以你应该用双引号将它包装起来visible ='<%:“false”%>'<=注意“”

#4


1  

Using this <%: ... %> syntax will raise your above parser error. The correct syntax for data binding server-control values is <%# ... %>. Inline expression's more detail is here.

使用此<%:...%>语法将引发上述解析器错误。数据绑定服务器控制值的正确语法是<%#...%>。内联表达式的更多细节在这里。


And you can done it in another way:

你可以用另一种方式完成它:

<% if(GetTrueOrFalse()) { %>
    <a ID="alink" runat="server" href="~/" >Home</a>
    //... other code
<% } %>

#1


1  

You can also declare a public boolean and use that. You will need to use DataBind() if the link is outside a GridView/Repetater etc.

您还可以声明一个公共布尔值并使用它。如果链接在GridView / Repetater等之外,则需要使用DataBind()。

public bool isVisible = true;

protected void Page_Load(object sender, EventArgs e)
{
    DataBind();
}

Now you can use that on the aspx.

现在你可以在aspx上使用它了。

<a runat="server" visible='<%# isVisible %>' href="~/">Home</a>

However you can also use a ternary operator based on a different variable or class value within your code.

但是,您也可以在代码中使用基于不同变量或类值的三元运算符。

public int myValue = 11;
public Book myBook = new Book() { category = "Fantasy" };

protected void Page_Load(object sender, EventArgs e)
{
    DataBind();
}

Now you can set the visibility based on myValue, even though it is not a Boolean.

现在,您可以基于myValue设置可见性,即使它不是布尔值。

<a runat="server" visible='<%# myValue > 10 ? true : false %>' href="~/">Home</a>

//or

<a runat="server" visible='<%# myBook.category == "Fantasy" ? true : false %>' href="~/">Home</a>

#2


2  

Suppose you have a method that returns bool value like this:

假设您有一个返回bool值的方法,如下所示:

public bool IsVisible()
{
    if (some_condition) // example condition test
    {
        return true;
    }
    else
    {
        return false;
    }
}

You need to use binding like this:

你需要像这样使用绑定:

ASPX

ASPX

<a runat="server" visible='<%# IsVisible() %>' href="~/" >Home</a>

ASPX.CS (Code-behind)

ASPX.CS(代码隐藏)

protected void Page_Load(object sender, EventArgs e)
{
    // do something

    Page.DataBind();
}

NB: This trick apply for either methods or properties which returns bool.

注意:这个技巧适用于返回bool的方法或属性。

Update 1:

更新1:

Since a tag doesn't set any id attribute, you can remove runat="server":

由于标记未设置任何id属性,因此可以删除runat =“server”:

<a visible='<%# IsVisible() %>' href="~/" >Home</a>

Or use CSS with display: none or visibility: hidden:

或者使用CSS with display:none或visibility:hidden:

<a visible='<%# IsVisible() %>' href="~/" style="visibility:hidden; display:none;">Home</a>

Reference:

参考:

Is code rendering block <%=%> useful for boolean type?

代码渲染块<%=%>对布尔类型有用吗?

#3


1  

Learn this technique you're gonna use it a lot. Server side controls or containers can easily be manipulated server side. How? well you did the first part right you game it runat="server" now all you have to do is to give it an id so it looks something like this let's name the id MyLink

学习这项技术你会经常使用它。服务器端控件或容器可以很容易地在服务器端进行操作。怎么样?嗯,你做了第一部分你游戏它runat =“服务器”现在你所要做的就是给它一个id所以它看起来像这样让我们命名为id MyLink

<a runat="server" id="MyLink" href="~/" >Home</a>

-Now you noticed that I've removed the attribute Visible. yes because now we're gonna take full control of it server side wise. Let's assume you wanna start off the page first time with a hidden , well that's easy: in your pageload event we'll use a good technique to determine that the code we'll write will only run once at the very first load.

- 你注意到我删除了属性Visible。是的,因为现在我们将完全控制服务器方面。让我们假设您想首次使用隐藏的页面开始,这很容易:在您的页面加载事件中,我们将使用一种好的技术来确定我们编写的代码只会在第一次加载时运行一次。

    protected void Page_Load(object sender, EventArgs e)
    {

       //this condition means if is not post back (meaning the very first time only)

        if(!IsPostBack)
          {
            MyLink.Visible = false;
          }
    } 

Now you got it you can simply make your control visible again whenever wherever you want simply

现在你已经掌握了它,你可以随时随地再次看到你的控件

MyLink.Visible = true; 

and done. lemme know if you need any more help!

并做了。 lemme知道你是否需要更多的帮助!

if you wanna do it inline it's a string value not bool so you should wrap it in double quotes visible='<%: "false" %>' <= notice the ""

如果你想内联它是一个字符串值而不是bool所以你应该用双引号将它包装起来visible ='<%:“false”%>'<=注意“”

#4


1  

Using this <%: ... %> syntax will raise your above parser error. The correct syntax for data binding server-control values is <%# ... %>. Inline expression's more detail is here.

使用此<%:...%>语法将引发上述解析器错误。数据绑定服务器控制值的正确语法是<%#...%>。内联表达式的更多细节在这里。


And you can done it in another way:

你可以用另一种方式完成它:

<% if(GetTrueOrFalse()) { %>
    <a ID="alink" runat="server" href="~/" >Home</a>
    //... other code
<% } %>