简体   繁体   English

在ascx html中访问ascx.cs公共布尔

[英]access ascx.cs public bool in ascx html

I am trying to access a public bool in the html piece of a custom user control. 我正在尝试在自定义用户控件的html部分访问公共布尔。 Here is a portion of my codebehind: 这是我部分代码的一部分:

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 名称“ ShowAddNewButton”在当前上下文中不存在
The name 'CreateMode' does not exist in the current context 名称“ 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 提前致谢

An alternative method to manage this is using Visible property. 解决此问题的另一种方法是使用Visible属性。 You'll need to set your li as runat="server" 您需要将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>

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. 你可以尝试用前缀thisbase ,看是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM