Pass Arguments to Subroutines from a Button Control

You can use the CommandName and CommandArgument attributes of a button to submit arguments to an ASP.NET method on the server. For example, suppose you have a repeater control in a page that shows a nicely formatted list of products. You can include a button on that page that, when clicked, calls a subroutine to add the product to the user’s shopping cart.

Rather than writing functions to get the product ID and name, or query the database to get that information, you can set the Button’s CommandName property to the name of the product and its CommandArgument property to the product ID. You can even use comma-separated values if you need more than two values (although in that case it would probably make more sense to go back to the database). Here’s a simplified example just to show how it works.

First, here’s the Button code:


And here’s the server code:

   Public Sub AddToCart(sender As Object, e As CommandEventArgs)       Session("currentCart") = Session("currentCart") & _           e.CommandName & "," & e.CommandArgument & ";"   End Sub

When the user clicks the button, you can retrieve the CommandName and CommandArgument values from the CommandEventArgs parameter defined in the AddToCart method.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet