Is there a way to incorporate an onclick event to a div that was generated within the C# code on the backend?

Within a loop, I have created a div that is populated with labels/data from my database:

    HtmlGenericControl availableTime =
    new HtmlGenericControl("div");
    availableTime.Attributes["id"] = "availableTime" + idCount.ToString();
    availableTime.Attributes["runat"] = "server";
    availableTime.Attributes["class"] = "availableTimeDiv";
    availableTime.Attributes.Add("onclick", "divClick()");

Below is the function I am attempting to call:

    protected void divClick(object sender, EventArgs e)
    {
        Response.Redirect("CreateMeeting.aspx");
    }

Despite my efforts, the function remains uncalled and the div is not clickable.

Furthermore, I require the id of the div and other elements within it. Is calling a method in the code behind the most effective way to achieve this?

Answer №1

Since you have not provided a specific question, I will give you a general example to illustrate. Let's imagine the scenario like this:

_myButton.Attributes.Add("onclick", "return confirm_delete();");

javascript:
function confirm_delete()
{
  if (confirm("Are you sure you want to delete the custom search by pressing button on div?")==true)
    return true;
  else
    return false;
}

The basic syntax is as follows:

div.Attributes.Add("onclick", DoSomething);

Here is another example :

Button divButton = new Button();
divButton.Attributes["style"] = "display:none;";
divButton.ID = "divBtn_" + id;
divButton.Text = id;
divButton.Click += divButton_Click;

div.Attributes.Add("onclick", "return divclick();");

<script>
function divclick()
{
    var button = document.getElementById('divBtn_');
    button.click();

    return false;
}
</script>

I trust that this information is sufficient for you to grasp how to implement a click event on your div.

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

Hide the address bar in a Google Maps iFrame

After numerous attempts, I still can't seem to hide the address bar on this Google Maps iFrame. Can anyone provide a solution or workaround for this issue? https://i.sstatic.net/x0pl9.png I have tried using display:none; in our CSS, which successfull ...

Echarts: scatter plots linked with a line to the axis (resembling the PACF diagram)

I am currently working with echarts (js). Is there a method to link the dot of the scatter plot with the 0 value on the y-axis? I want it to resemble a pacf plot, similar to this example: The desired outcome should look something like this: https://i.sta ...

Obtain Relative URL with the help of Selenium using the PhantomJS driver

Utilizing Selenium along with the Phantom JS driver, I am attempting to load an HTML page and extract all of the HREF links from it. The issue arises when PhantomJS provides absolute URLs after resolving them entirely. My specific requirement is to extrac ...

Give Jquery a quick breather

My goal is to have my program pause for 3 seconds before continuing with the rest of the code. I've been researching online, but all I can find are methods that delay specific lines of code, which is not what I need. What I would like to achieve look ...

Does one require the express.js framework in order to create a web application using nodeJS?

Exploring the creation of a basic web application using HTML, NodeJS, and Postgres. Can this be achieved without incorporating the ExpressJS framework? Seeking guidance on performing CRUD operations with NodeJs, Javascript, and Postgres sans ExpressJS. G ...

How can I execute code once the webpage has finished loading properly?

On my webpage, referred to as X, I inserted a script tag within the head section. This script is designed to scan the body after it has finished rendering. I attempted using jQuery.ready() but unfortunately it did not trigger! Is it possible that the DOM ...

Identifying multiple buttons on a page using Selenium

While working with Selenium and C#, I'm encountering difficulty clicking on a button by its text due to multiple buttons having the same text on the screen. Fortunately, there is a "store" property assigned to each button that could help me differenti ...

Transferring data through Ajax, Jquery, and PHP: A seamless process

My goal is to develop an attendance button that adds +1 to the total number of people attending when clicked by the user. I am looking for a way to achieve this without having to refresh the page. Ideally, I would like to connect it to a database to upda ...

Generating random numbers for ids in C# by clicking a button

Utilizing c# winforms and selenium webdrivers. I attempted the following driver2.FindElement(By.XPath("//div[@class='ad-ttl']/a")).Click(); to try to click it unsuccessfully. HTML Code : <div id="yui_3_10_0_1_1418194162300_146" class="a ...

Tips for formatting a date variable in C# to align with the date format stored in an SQL database

When I input a date from a textbox in C# program and compare it to entries in a SQL database, I am encountering an issue where they do not match even though the SQL query shows that there are entries for that date. I have attempted saving the date to a va ...

Is it possible to control the maximum number of elements in a Page Object list?

I am currently developing an automated test and I have successfully retrieved a list of elements necessary for the page using the following code snippet: using System.Collections.Generic; using OpenQA.Selenium; using OpenQA.Selenium.Support.PageObjects; ...

The .NET8 Maui app on Windows encountered an interface that had been marshalled for a separate thread within the application

We're in the process of converting a Xamarin.Forms app to a MAUI Windows app. The app builds without errors and runs locally up to the login page (haven't started on the API yet). However, when we package it and try to run it, it crashes and thro ...

Checking with Protractor to see if the modal is displayed

I am currently working on a Protractor test to check if a bootstrap modal window that confirms the deletion of a record is visible at this time. The record that needs to be deleted is displayed in an angular ng-repeat, so I have to trigger the delete butt ...

Utilize Vue.js to easily upload images alongside form input fields

I have recently started a small project with Vue Js. I am trying to incorporate an upload file option in my contact form. Due to the numerous input text fields, I am using serialize for the form. However, I am facing issues with the append function. How ca ...

The functionality of the Eslint object-property-newline feature is not functioning as expected

Within my configuration file .eslintrc, I have set "object-property-newline": ["error", { "allowAllPropertiesOnSameLine": true }], and "max-len": "off". However, objects like const something = {a: 5, ffsdfasdasdsddddd: 'asdasdasdddddddddddssssssddddd ...

Javascript experiencing issues with Heapsort

I have been working on implementing heapsort in JavaScript, but I've encountered an issue with an undefined element at position array.length - 2, and the element at index 0 is not sorted. Here is the code snippet: var heapSort = function(array) { ...

The validation in the model state fails specifically in the edit view when using ASP.NET MVC4

Just starting out with asp.net mvc and I'm puzzled as to why ModelState.IsValid is false in the edit view when using: if (ModelState.IsValid) { //code } Here's the complete code snippet for my ...

Ways to prevent the jQuery simple slider from transitioning slides while it is in an invisible state

There is a jQuery slider on my website that behaves strangely when I navigate away from the Chrome browser and return later. It seems to speed through all pending slides quickly when it was not visible. Now, I want the slider to pause when it becomes invi ...

The API response is indicating that it is empty, however, upon further examination, it is not

I created an API that shows a user's followers and following users. Everything is displaying correctly on the screen. However, when I try to use console.log() to display the array it is stored in after calling the method, it shows as an empty array. I ...

What is the best approach for conducting dynamic comparisons in .NET Core?

The database contains conditional rules set up for each user. Here is an example of the rules configured: | key | expression |rule| |:---- |:--------------:|---:| | 001 | >=500 and <=600| 1.2| | 001 | >600 | 2.0| | 002 | ==400 ...