Transitioning from Asp.Net using C# to incorporating Javascript Ajax

New to Ajax and still getting the hang of how it works in Asp.Net.

Using Asp.Net 3.5 with a c# server code that runs and then calls a subscribed event to update text in a textbox control.

c# code :

public partial class TestDBLoader : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        dbManager1.LoadDBCompleted += new DBManager.AsyncLoadDBDelegate(dbManager1_LoadDBCompleted);
        dbManager1.LoadDBAsync(sender, e, null);
    }

    public void dbManager1_LoadDBCompleted(object sender, EventArgs e)
    {
        txtResult.Text = "Finish!";
        updatePanel.Update();
    }
}

public partial class DBManager : System.Web.UI.UserControl
{
    public AsyncLoadDBDelegate asyncLoadDB;
    public delegate void AsyncLoadDBDelegate(object sender, EventArgs e);
    public event AsyncLoadDBDelegate LoadDBCompleted;

    private void StartLoad(object sender, EventArgs e)
    {
        for (int i = 0; i <= 10; i++)
        {
            Thread.Sleep(1000);
        }

        LoadDBCompleted(sender, e);
    }

    public IAsyncResult LoadDBAsync(object sender, EventArgs e, AsyncCallback callback)
    {
        IAsyncResult asyncResult;

        asyncLoadDB = new AsyncLoadDBDelegate(StartLoad);

        asyncResult = asyncLoadDB.BeginInvoke(sender, e, callback, null);

        return asyncResult;
    }
}

Asp code :

<asp:ScriptManager ID="ScriptManager" runat="server" />   
<asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="dbManager1" EventName="LoadDBCompleted" />
    </Triggers>
    <ContentTemplate>
        <uc:DBManager ID="dbManager1" runat="server" />
        <asp:TextBox ID="txtResult" runat="server" />
    </ContentTemplate>
</asp:UpdatePanel>

Debugging shows that dbManager1_LoadDBCompleted is being called but not updating the textbox. What could be the issue?

EDIT : Revised code for more clarity.

EDIT2 : Any alternative methods rather than using the UpdatePanel? Please advise.

Answer №1

While the id of your textbox is txtbox1, you are attempting to set txtbox.text ...

Answer №2

The issue might be that the UpdatePanel isn't triggering its update method, resulting in no changes to its contents.

To fix this, make sure the control that triggers WorkFinished is inside the UpdatePanel's ContentTemplate, include the ID of the control in the Triggers collection as an AsynchronousPostbackTrigger, or add UpdatePanel.Update(); at the end of WorkFinished to force a reload of the UpdatePanel.

It's important to use UpdatePanels with caution as they can impact performance significantly, as discussed here:

Answer №3

It's important to note that while often referred to as "AJAX", an updatepanel functions by requesting the HTML content for a specific area after a postback event, leading to potential excessive communication.

In order to provide precise assistance, could you please share additional code detailing what triggers the WorkFinished function?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing the malfunction of setTimeout in this specific function?

Developed a custom function that dynamically creates tabs by accepting element IDs as arguments and adding event listeners to respond to click events. Here's the code snippet for reference: function toggleTabs() { var panel = [], li = []; for(var ...

The hunt is on for greater value within the index

My requirement is to check the text content of a box. If it matches any value stored in an ARRAY, then I have to execute a specific action. var ARRAY1 = ["Saab", "Volvo", "BMW"]; if (select[i].innerHTML.indexOf('ARRAY1') != -1){//code here} ...

How to remove the initial negative value present in an array using C#

I have searched for various solutions, but none of them effectively address my requirement to eliminate the initial negative number in a sequence while retaining the others (there may be several negative numbers in the array). ...

What is the reason behind PHP disregarding ajax POST requests?

The following code snippet can be found in the file index.php: <head> <?php if(isset($_GET['text1'])) { some_function(); } ?> </head> <body> <form id="form"> <input type="text" id="text1" name="text1"/> < ...

JSON manipulation using JavaScript

Looking to dynamically choose a JSON key directly from the JSON Object. var text = '{"employees":[' + '{"firstName":"lastName", "lastName":"Doe" }]}'; var obj = JSON.parse(text); var firstName = obj.employees[0].firstNa ...

Creating a Multi-Step Form and Transmitting Data to Various Endpoints using Vanilla JavaScript

My goal is to design a multi-step form that directs each step's information to a distinct endpoint, and I'm also interested in integrating validation processes to guarantee that users can only advance to the subsequent step if their data is accur ...

What is the process for running .js files on my browser from my local machine?

Hi there! I'm trying to figure out how I can create a JavaScript game in TextMate on my Mac. I want to have a regular .js file, then open it and run it in Chrome so that whatever I have coded - for example, "Hello World!" - will display in the browser ...

Navigating through a picker wheel on Appium using C#, to pinpoint the desired value

Although everything seems to be working fine, I encountered an issue when trying to use scrollObject.Add("name", "Mon, Apr 9") as it does not select the specific value from the pickerWheel. Dictionary<string, object> scrollObject = new Dictionary< ...

Guide to Implementing a Single Trigger Click with jQuery for Window Scrolling

I have implemented JavaScript code for a button that loads additional posts from the database. The button has the class of .getmore. My goal now is to enable infinite scroll so that users do not have to manually click the button. In order to achieve this ...

verify communication via javascript

I've been struggling to establish a connection using Javascript, but none of the tutorials I've tried seem to work for me. For instance, here's the code I'm working with that isn't functioning: <!DOCTYPE HTML> <html> ...

Luxurious Selection Tool with Advanced A4j Integration and Dynamic List Modification

I've noticed a strange behavior when using the rich picklist component with an a4j support onlistchange event. When I have "n" selected items in the picklist component, the server ends up trying to populate it "n" times (calling gruposDeTributosQuery ...

"Encountering an Asp.net error while trying to utilize the Visual Basic

I recently encountered an issue when using a Visual Basic Reference in C# to add a new location to a table. While this process worked perfectly on my local machine, once I published it to the hosting site, an error occurred. The error message displayed is ...

What is the best method for transferring a string from JavaScript to a JSON file?

Is there a way to update the value of "JSValue" in my JSON (JSValue) using JavaScript? Specifically, how can I assign JSValue to a variable called Value using JavaScript? JavaScript var Value = 1 + 1 JSON { "DATA": [ { "JSValue": "0" } ...

Discover records that include the specific object id within an array (Mongodb)

I'm currently tackling a node project and struggling with using the find() function on an array of object ids. Let me share an example of a document stored in my mongodb database: { "_id": { "$oid": "6515923586714e6ae70 ...

Is it possible for a Windows service application and a web application to use the same bin folder?

I have a situation where both a windows service application and an asp.net web application share some common dlls. I am looking for a way to avoid creating separate copies of the dlls and do not want to add them to the GAC either. My approach has been t ...

Changes are not reflected in the array passed as a prop

This question is aimed at gaining a deeper understanding of how React handles and reacts to changes, rather than focusing on implementation. Therefore, I am temporarily setting aside the immutable-props approach. My goal is to retrieve the first element o ...

I possess both a minimum and maximum number; how can I effectively create an array containing n random numbers within

Given a minimum number of 10.5 and a maximum number of 29.75, the task is to generate an array within these two ranges with a specific length denoted by 'n'. While the function for generating the array is provided below, it is important to calcul ...

The process of implementing server-side rendering for React Next applications with Material-ui using CSS

I have developed a basic React application using Next.js with an integrated express server: app.prepare() .then(() => { const server = express() server.get('/job/:id', (req, res) => { const actualPage = '/job' const ...

Tips for transferring information using '&' on websites

When using jQuery's $.ajax method to send data to add_showcase.php, an issue arises when passing a complex data structure as the "desc" parameter. The data might not be received in its entirety by the PHP file. $.ajax({ type: "GET", ...

Using JS/jQuery to modify linear-gradient

I'm currently working on a project that involves using input type color. My goal is to create a gradient using these colors. Any suggestions on how I can achieve this? Here are the color tags: <div id="part1" align=center> <input type="colo ...