Can't reference an ASP button in script - The name 'xxx' does not exist in the current context

StackOverflow https://stackoverflow.com/questions/22160702

  •  19-10-2022
  •  | 
  •  

سؤال

I am very new to ASP.NET so I used their Log-In template in VS2013 so I wouldn't need to set that up myself. This also included a Site Master page with everything looking good, so I just used that. I have not been coding in the ".aspx.cs" file, but rather the Source section in the ".aspx" file, so I can quickly look at the Design section and see how it looks. Here's my code for the ".aspx" file:

<%@ Page Title="Create Schedule" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="false" CodeFile="CreateSchedule.aspx.cs" Inherits="Members_CreateSchedule" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">

<script runat="server">

// I am just experimenting at the moment so I copied some code from Microsoft to see if I can get some kind of response
    void Page_Load(Object sender, EventArgs e)
    {
        Button1.Click += new EventHandler(this.GreetingBtn_Click);
    }

    void GreetingBtn_Click(Object sender,
                           EventArgs e)
    {
        Button clickedButton = (Button)sender;
        clickedButton.Text = "...button clicked...";
        clickedButton.Enabled = false;
    }

</script>

<hgroup class="title">
    <h1>Create a Schedule.</h1>
    <h2>&nbsp;</h2>
</hgroup>
<section class="create">
    <asp:LoginView ID="LoginView1" runat="server" ViewStateMode="Disabled">
        <AnonymousTemplate>
            <p>You are not logged in so you cannot view this page.</p>
            <p><b><a href="../Account/Login.aspx">Click here to log in!</a></b></p>
        </AnonymousTemplate>
        <LoggedInTemplate>
            <header>
                This is where you can create a schedule.
                <br />
            </header>
            <p>Create an ID for your employees to find your schedule: <asp:TextBox ID="scheduleID" runat="server" Width="150px"></asp:TextBox></p>

            <asp:Table ID="Table1" runat="server" CellPadding="10" GridLines="Both" HorizontalAlign="Center">
                    <asp:TableHeaderRow Width="80px" HorizontalAlign="Center">
                        <asp:TableCell Width="80px">
                            &nbsp
                        </asp:TableCell>
                        <asp:TableCell Width="80px">
                            <b>Sunday</b>
                        </asp:TableCell>
                        <asp:TableCell Width="80px">
                            <b>Monday</b>
                        </asp:TableCell>
                        <asp:TableCell Width="80px">
                            <b>Tuesday</b>
                        </asp:TableCell>
                        <asp:TableCell Width="80px">
                            <b>Wednesday</b>
                        </asp:TableCell>
                        <asp:TableCell Width="80px">
                            <b>Thursday</b>
                        </asp:TableCell>
                        <asp:TableCell Width="80px">
                            <b>Friday</b>
                        </asp:TableCell>
                        <asp:TableCell Width="80px">
                            <b>Saturday</b>
                        </asp:TableCell>
                    </asp:TableHeaderRow>
                    <asp:TableRow>
                        <asp:TableCell>
                            <b>8:00 AM</b>
                        </asp:TableCell>
                        <asp:TableCell>
                            <asp:Button ID="Button1" Text="" runat="server" style="width:100%" BackColor="White" />
                        </asp:TableCell>
                        <asp:TableCell>
                            <asp:Button ID="Button2" Text="" runat="server" style="width:100%" BackColor="White" />
                        </asp:TableCell>
                        <asp:TableCell>
                            <asp:Button ID="Button3" Text="" runat="server" style="width:100%" BackColor="White" />
                        </asp:TableCell>
                        <asp:TableCell>
                            <asp:Button ID="Button4" Text="" runat="server" style="width:100%" BackColor="White"/>
                        </asp:TableCell>
                        <asp:TableCell>
                            <asp:Button ID="Button5" Text="" runat="server" style="width:100%" BackColor="White" />
                        </asp:TableCell>
                        <asp:TableCell>
                            <asp:Button ID="Button6" Text="" runat="server" style="width:100%" BackColor="White" />
                        </asp:TableCell>
                        <asp:TableCell>
                            <asp:Button ID="Button7" Text="" runat="server" style="width:100%" BackColor="White" />
                        </asp:TableCell>
                    </asp:TableRow>
                </asp:Table>
            <br />
            <br />
        </LoggedInTemplate>

    </asp:LoginView>
</section>

This is the code for my ".aspx.cs" file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Members_CreateSchedule : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

The purpose of this page is for an employer to create a custom calendar for the employees to join and add shifts, so I am allowing them to select which times on which days they want to offer. My end goal is to be able to click the button and have the color turn green, and later log which buttons were pressed in my database.

Button1 gives the error "The name 'Button1' does not exist in the current context. I'm not sure what I need to be doing in order to get the ASP button to respond to the script at the top.

I think I just need a little clarification on what I'm doing wrong and how I should be going about this. Thanks in advance.

هل كانت مفيدة؟

المحلول

The buttons are inside the LoggedInTemplate of the LoginView. The controls inside the template are not accessible directly.

Button Button1 = (Button)LoginView1.FindControl("Button1");

The buttons will only be available when a user is logged in, otherwise the AnonymousTemplate is used. So it's best to check if a button was retrieved by the previous code before doing anything to it to avoid a null reference error.

if(Button1 != null)
{
     //Do your stuff
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top