简体   繁体   中英

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. 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:

<%@ 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

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. You'll need to set your li as 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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