javasocute
javasocute

Reputation: 648

Label does not exist in the current context of asp.net page and ajax control

before I begin, there is another question with a similar title and it is unsolved but my situation is pretty different since I am using Ajax.

I recently added a label to my Ajax UpdateProgress control and for some reason my asp.net page is not reading it. My original goal was for the text of the label to be constantly be updating while the long method runs. I am using code behind, and I believe the label is declared. I will post my .cs page if anyone would like to read through it (its not too long), All my other labels work perfectly and even if I take the label OUT of the ajax control it will work fine (not the text updates though). Is there a certain Ajax label I need to use?

Im pretty confused why the error is occurring. The exact error message states : "The name 'lblProgress' does not exist in the current context. Im using c#, ajax controls, a asp.net page, and visual studio. This program uploads a file to a client and stores the information in a database. If anyone can help I would really appreciate it. Thanks in advance!

    using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Threading;


public partial class SendOrders : System.Web.UI.Page
{
protected enum EDIType
{
    Notes,
    Details
}

protected static string NextBatchNum = "1";
protected static string FileNamePrefix = "";
protected static string OverBatchLimitStr = "Batch file limit has been reached.  No more batches can be processed today.";

protected void Page_Load(object sender, EventArgs e)
{
    Initialize();
}
protected void Page_PreRender(object sender, EventArgs e)
{
}
protected void Button_Click(object sender, EventArgs e)
{
    PutFTPButton.Enabled = false;
    lblProgress.Visible = true;
    lblProgress.Text = "Preparing System Checks...";
    Thread.Sleep(3000);
    Button btn = (Button)sender;
    KaplanFTP.BatchFiles bf = new KaplanFTP.BatchFiles();
    KaplanFTP.Transmit transmit = new KaplanFTP.Transmit();

    if (btn.ID == PutFTPButton.ID)
    {
        lblProgress.Text = "Locating Files...";
        //bf.ReadyFilesForTransmission();
        DirectoryInfo dir = new DirectoryInfo(@"C:\Kaplan");
        FileInfo[] BatchFiles = bf.GetBatchFiles(dir);
        bool result = transmit.UploadBatchFilesToFTP(BatchFiles);
        lblProgress.Text = "Sending Files to Kaplan...";
        if (!result)
        {
            ErrorLabel.Text += KaplanFTP.errorMsg;
            return;
        }
        bf.InsertBatchDataIntoDatabase("CTL");
        bf.InsertBatchDataIntoDatabase("HDR");
        bf.InsertBatchDataIntoDatabase("DET");
        bf.InsertBatchDataIntoDatabase("NTS");
        List<FileInfo> allfiles = BatchFiles.ToList<FileInfo>();
        allfiles.AddRange(dir.GetFiles("*.txt"));
        bf.MoveFiles(allfiles);
        lblProgress.Text = "Uploading File Info to Database...";
        foreach (string order in bf.OrdersSent)
        {
            OrdersSentDiv.Controls.Add(new LiteralControl(order + "<br />"));
        }
        OrdersSentDiv.Visible = true;
        OrdersInfoDiv.Visible = false;
        SuccessLabel.Visible = true;
        NoBatchesToProcessLbl.Visible = true;
        BatchesToProcessLbl.Visible = false;
        PutFTPButton.Enabled = false;
        BatchesCreatedLbl.Text = int.Parse(NextBatchNum).ToString();
        Thread.Sleep(20000);

        if (KaplanFTP.errorMsg.Length != 0)
        {
            ErrorLabel.Visible = true;
            SuccessLabel.Visible = false;
            ErrorLabel.Text = KaplanFTP.errorMsg;
        }
    }
}

Here is my aspx code as well.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SendOrders.aspx.cs" Inherits="SendOrders" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
   <title>Kaplan EDI Manager</title>
    <link href="css/style.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .style1
        {
            width: 220px;
            height: 19px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div class="mainPanel">
        <div>
            <h3>Number of Batches Created Today: <asp:Label runat="server" style="display:inline;" ID="BatchesCreatedLbl"></asp:Label>
                <asp:ScriptManager ID="ScriptManager1" runat="server">
                </asp:ScriptManager>
                <span class="red">COUNTDOWN TO SUBMISSION!</span>
                <span id="timespan" class="red"></span>
            </h3>
        </div>
        <div id="batchestoprocessdiv">
        </div>

        </asp:UpdatePanel>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Label ID="BatchesToProcessLbl" runat="server" CssClass="green" 
                    Height="22px" Text="THERE IS AN ORDER BATCH TO PROCESS."></asp:Label>
                <asp:Label ID="NoBatchesToProcessLbl" runat="server" CssClass="red" 
                    Text="There are no Order Batches to Process." Visible="false"></asp:Label>
                <asp:Button ID="PutFTPButton" runat="server" onclick="Button_Click" 
                    Text="Submit Orders" />
                <asp:Label ID="SuccessLabel" runat="server" CssClass="green" 
                    Text="Batch has been processed and uploaded successfully." Visible="false"></asp:Label>
                <asp:Label ID="ErrorLabel" runat="server" CssClass="red" Text="Error: " 
                    Visible="false"></asp:Label>
                <asp:Label ID="lblProgress" runat="server" CssClass="green" Height="16px" 
                    Text="Label" Visible="False"></asp:Label>
                <br />
            </ContentTemplate>
        </asp:UpdatePanel>
        <asp:UpdateProgress ID="UpdateProgress1" runat="server" 
            AssociatedUpdatePanelID="UpdatePanel1">
            <ProgressTemplate>
                <br />
                <img alt="" class="style1" src="images/ajax-loader.gif" />
            </ProgressTemplate>
        </asp:UpdateProgress>
    </div>
    <div id="OrdersInfoDiv" runat="server" visible="false">
        <asp:GridView ID="BatchDetails" Caption="Details of orders ready to be sent" runat="server" AutoGenerateColumns="true" 
        CssClass="InfoTable" AlternatingRowStyle-CssClass="InfoTableAlternateRow" >
        </asp:GridView>
    </div>
    <div id="OrdersSentDiv" class="mainPanel" runat="server" visible="false">
        <h4>Sent Orders</h4>
    </div>
    </form>
    <script src="js/SendOrders.js" type="text/javascript"></script>
</body>
</html>

Upvotes: 0

Views: 8611

Answers (2)

James Johnson
James Johnson

Reputation: 46047

If the control is declared in markup and the code-behind doesn't recognize it, then your designer.cs file is probably out of sync. There are a few ways to fix this:

  1. Close the form and reopen it, and remove and add lblProgress again
  2. Add the Label to the designer.cs file manually

Here's how to add it manually:

/// <summary>
/// lblProgress control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label lblProgress;

EDIT

Just noticed that you're putting the UpdatePanel in an UpdateProgress control, in which case you'll need to reference it using FindControl (like @Valeklosse) suggested.

Upvotes: 1

Valeklosse
Valeklosse

Reputation: 1017

If the Label is created inside the UpdateProgress control, then you will need to do something like this

((Label)upUpdateProgress.FindControl("lblProgress")).Text = "Uploading File...";

Upvotes: 1

Related Questions