Guide to storing a variable value when a user clicks on the client-side in a Grid View:

How can I store data in a variable on client click within a grid view?

I have a Stored Procedure that returns Service Id based on the Department code provided. We are binding these details to a Grid View. How can we bind the Service Id to a variable that can be accessed throughout the code and passed to different Stored Procedures? Currently, I am hard coding test data for this purpose. While binding to the grid view, we are not displaying the Service Id anywhere but it is present in the data table. I am using RowDataBound but struggling to store the clicked element in a variable.

C# part

protected void gvServiceCount_RowDataBound(object sender, GridViewRowEventArgs e)
{   int serviceId = Convert.ToInt32(e.Row.Cells[2].Text);
    Session["serviceId"] = serviceId;
}    

ASP.NET part

<asp:GridView ID="gvServiceCount" runat="server" AutoGenerateColumns="false" EmptyDataText="No Data Found"
                    Width="100%" HeaderStyle-BackColor="#facf5a" OnSelectedIndexChanged="gvServiceCount_SelectedIndexChanged" OnRowDataBound="gvServiceCount_RowDataBound"
                    HeaderStyle-ForeColor="Black" RowStyle-BackColor="White"
                    RowStyle-ForeColor="Black">
                    <Columns>
                         <asp:TemplateField HeaderText="Closed">
                            <ItemTemplate>
                                <asp:LinkButton ID="lblClosedCount" Text='<%#Eval("SUMOFAPPROVEREJECTED") %>' runat="server" OnClick="lblClosedCount_Click"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                         </Columns>
                </asp:GridView>

I need to find a way to store the data in a variable upon client click.

Answer №1

Here is a code snippet that you can try:

<asp:TemplateField HeaderText="Closed">
    <ItemTemplate>
    <asp:LinkButton ID="lblClosedCount" Text='<%#Eval("SUMOFAPPROVEREJECTED") %>' runat="server" data-serviceid='<%#Eval("ServiceId")' OnClick="lblClosedCount_Click"></asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>

Implement the following logic in your code:

protected void lblClosedCount_Click(object sender,EventArgs e)
{
    string serviceId = lblClosedCount.Attributes["data-serviceid"].toString();
    // your coding here ----
}

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

Having trouble with QuickBlox video calling feature on the web?

I have encountered an issue while trying to integrate video chat into my Java web application using QuickBlox. I am utilizing Angular/JavaScript on the frontend. The problem arises when attempting to create a session for a user that I have created in Quic ...

The latest update of MS CRM 2013 now includes a version number for WebResources that are of script

I came across an unusual issue in MS CRM 2013 that seems to be intentional, and I need some assistance in finding a workaround for it. The problem is that calling the getScript jQuery method from a WebResource is not possible. In CRM, a version string is ...

What is the best way to share an HTML element across multiple pages in Next.js?

I am in the process of developing a website that features a table of contents with links on the left side, content sections in the middle, and an overview on the right. For a visual representation of the general concept, please visit: https://i.sstatic.net ...

Tips on activating the CSS style while typing using the onChange event in React

Is it possible to dynamically adjust the width of an input as we type in React? Currently, my input has a default width of 1ch. I would like it to increase or decrease based on the number of characters entered, so that the percentage sign stays at the end ...

Data from AngularFire not displaying in my list application

While going through tutorials on the Angular website, I encountered a roadblock while attempting to create a list that utilizes Firebase for data storage. Strangely, everything seems to be functional on the Angular site, but clicking on the "Edit Me" link ...

One way to automatically pass a default date parameter to an AJAX GET call in JavaScript is to set the initial date value before making the request. As the

Upon page load, I want to display the current date's attendance. After selecting a specific date, I aim to fetch and show the details for that selected date. The current issue I am facing: When I search for a particular date, I receive data for bot ...

Unveiling the approach to accessing a nested function with jQuery

While the title may be a bit misleading, I couldn't think of a better way to describe it. I've created a function that allows a small pop-up window to appear when a link is clicked (to confirm whether or not an article should be deleted). Addit ...

React.js issue with onChange event on <input> element freezing

I am experiencing an issue where the input box only allows me to type one letter at a time before getting stuck in its original position. This behavior is confusing to me as the code works fine in another project of mine. const [name, setName] = useStat ...

What is the best way to display grouped items horizontally in an ASP.NET ListView?

My ASP ListView currently displays items vertically in columns with GroupItemCount set to 3: A D G B E H C F I Is there a way to adjust the display so that it groups items horizontally in rows like this? A B C D E F G H I This is the ListVi ...

Increment and decrement the like count on fa-heart multiple times

Is there a way to increment the count of a fa-heart value on click and decrement it on the second click? The issue I'm facing is that I have multiple fa-heart elements on the same page, making it challenging to increment or decrement the clicked fa-h ...

Tracking mouse positions across an HTML document

I've been working on a project inspired by the voxel painter example on the three.js page. In this particular example, the mouse coordinates are utilized to move a roll-over helper that indicates where a box will be placed upon click. mouse.set((even ...

Tips for creating a consistent format based on test cases

var years = Math.floor(seconds / (3600*24*365)) seconds -= years*3600*24*365 var days = Math.floor(seconds / (3600*24)) seconds -= days*3600*24 var hrs = Math.floor(seconds / 3600) seconds -= hrs*3600 var minutes = Math.floor(seconds / 60) ...

JQuery fails to retrieve accurate width measurements

Utilizing this code snippet, I have been able to obtain the width of an element and then set it as its height: $(document).ready(function(){ $(".equal-height").each(function(){ var itemSize = $(this).outerWidth(); cons ...

A helpful guide on how to dynamically input database values into the href attribute of an 'a' tag

How do I successfully pass an ID to href in my code? When I try to return it via req.params, it keeps coming back as undefined. I need the ID from the URL in order to use the findOne method to access the data stored in the database. I've searched thr ...

Struggling to properly line up the baselines of navigation list items that are styled as circular elements using CSS

I have transformed my navigation menu into a series of CSS circles with text inside. The issue I am facing is that the text spills out unevenly based on the amount of content in each circle. To address this, I used a JavaScript solution to center-align the ...

Change the ng-model of the initial controller when a button is clicked in a separate controller

I am working on an AngularJS application that has two controllers. I am facing an issue where I need to update the ng-model of an input field in the first controller by clicking a button in the second controller. Accessing the $scope of the first controlle ...

What is the best way to cycle through a series of lists and retrieve the URL when a specific string is found within a class of the list

I'm new to JQuery and currently experimenting on JSFiddle to improve my skills for developing a web app I have in mind. One of the requirements I need to meet is iterating through lists on the page. If a specific class contains a certain string, I sh ...

Capturing user inputs in PHP or AJAX

Is there a way to capture keystrokes on a web page regardless of where the user is focused? For example, if someone has the webpage open and they press '1', can it trigger an action? And if they press '2', can something else happen wit ...

Managing Timezones in DateTime in C#, JSON, and Android

I am facing an issue with date transformation within a SQL Server database. The original date appears as 2012-01-10 00:00:00.000, but when converted to JSON format, it changes to "/Date(1326150000000+0100)/". When I try to extract only the milliseconds in ...

Display of Navigation Bar in Angular is Not Proper

Currently diving into the world of Angular, I've encountered an issue with a material menu that isn't displaying correctly. The expected outcome based on my code should resemble this image: https://i.stack.imgur.com/z70Aq.png This snippet showc ...