在ascx html中访问ascx.cs public bool

时间:2022-09-02 12:19:07

I am trying to access a public bool in the html piece of a custom user control. Here is a portion of my codebehind:

我试图在自定义用户控件的html片段中访问公共bool。这是我的代码隐藏的一部分:

public partial class ToDoList : System.Web.UI.UserControl
{
    public bool ShowAddNewButton { get; set; }
    public bool CreateMode { get;set;}

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

}

Here is my html:

这是我的HTML:

<%@ Control Language="C#" AutoEventWireup="True" CodeBehind="ToDoList.ascx.cs" Inherits="ToDoList" %>

<div id="leftNav">
    <asp:HiddenField ID="hfNAVItems" runat="server" />
    <asp:HiddenField ID="hfRequestID" runat="server" />
    <asp:HiddenField runat="server" ID="hdnAllowDragDrop" />
    <div class='nav-header'>
        <div>
            My To-Do List</div>
    </div>
    <ul id="mainNav" class="nav-item-list">
        <li class="nav-item" id="firstNavContainer">
            <div>
                <span class="nav-item-todos">
                    <asp:Label Text="" runat="server" ID="lblfirstSectionHeader" /></span>
                <% if(ShowAddNewButton)
                   { %>
                <asp:Button ID="Button1" runat="server" Text="Add New" CssClass="navaddnewrequest todo-button"
                    OnClick="btnNewToDo_Click" />
                <%} %>
            </div>
            <ul id="todosNav">
                <% if (CreateMode)
                   { %>
                <li class="SelectedNavHeader">New Todo</li>
                <% } %>
                <asp:Repeater runat="server" ID="rptToDoNavItems">
                    <ItemTemplate>
                    <li class="ToDoSelectedNavGroup" runat="server" id="liToDoNavGroupCont">
                        <asp:Literal runat="server" ID="lblToDoNavGroupDisplay" /></li>
                    </ItemTemplate>
                </asp:Repeater>
            </ul>
        </li>
    </ul>
    <ul id="lineItemNav">
        <li class="darkgradient header">My Lists</li>
    </ul>

</div>

I am getting the following errors:

我收到以下错误:

The name 'ShowAddNewButton' does not exist in the current context
The name 'CreateMode' does not exist in the current context

当前上下文中不存在名称“ShowAddNewButton”当前上下文中不存在名称“CreateMode”

scratching my head on this one... seems pretty elemental. Would be happy to learn that I am missing something completely obvious.

在这一个上挠我的头......看起来非常元素。很高兴得知我错过了一些完全明显的东西。

Thanks in advance

提前致谢

2 个解决方案

#1


0  

An alternative method to manage this is using Visible property. You'll need to set your li as runat="server"

另一种管理方法是使用Visible属性。您需要将li设置为runat =“server”

<asp:Button ID="Button1" runat="server" Text="Add New" CssClass="navaddnewrequest todo-button"
    OnClick="btnNewToDo_Click" Visible='<%= ShowAddNewButton %>'/>

 ....

<li ID="liNewTodo" runat="server" class="SelectedNavHeader" 
    Visible='<%= CreateMode %>'>New Todo</li>

#2


0  

If memory serves, you can't really mix code-behind properties and server-side script. You have to pick one. But it's been a long time since I tried it, so maybe you can.

如果内存服务,你不能真正混合代码隐藏属性和服务器端脚本。你必须选一个。但是自从我尝试它已经很久了,所以也许你可以。

It seems to me that, assuming you can do it, the problem is that the server-side script is interpreting your property names as variable names. You could try prefixing them with this or base and seeing if that helps.

在我看来,假设您可以这样做,问题是服务器端脚本将您的属性名称解释为变量名称。您可以尝试使用this或base作为前缀,并查看是否有帮助。

#1


0  

An alternative method to manage this is using Visible property. You'll need to set your li as runat="server"

另一种管理方法是使用Visible属性。您需要将li设置为runat =“server”

<asp:Button ID="Button1" runat="server" Text="Add New" CssClass="navaddnewrequest todo-button"
    OnClick="btnNewToDo_Click" Visible='<%= ShowAddNewButton %>'/>

 ....

<li ID="liNewTodo" runat="server" class="SelectedNavHeader" 
    Visible='<%= CreateMode %>'>New Todo</li>

#2


0  

If memory serves, you can't really mix code-behind properties and server-side script. You have to pick one. But it's been a long time since I tried it, so maybe you can.

如果内存服务,你不能真正混合代码隐藏属性和服务器端脚本。你必须选一个。但是自从我尝试它已经很久了,所以也许你可以。

It seems to me that, assuming you can do it, the problem is that the server-side script is interpreting your property names as variable names. You could try prefixing them with this or base and seeing if that helps.

在我看来,假设您可以这样做,问题是服务器端脚本将您的属性名称解释为变量名称。您可以尝试使用this或base作为前缀,并查看是否有帮助。