Simple application of the Linkbutton control in a project

  • 2020-06-07 04:22:19
  • OfStack

Simple application of the Linkbutton control in a project
We know that there is a set of web controls for form submission and return, known as Button controls. Such controls are used to submit pages with user input values to the server so that the values can be processed with the code in the page. It generates an Click event on the server for the user to use in the code.

Button control can be divided into button control, LinkButton control and ImageButton control. Both are used to submit pages. The ImageButton control can be displayed as an image and can provide the coordinate position of the user's click position. The LinkButton control, on the other hand, displays as a hyperlink on the page.

In the online shoe store system, we simply applied the method of using multiple LinkButton controls in one page to execute the same command. Next, let's introduce 1 in more detail.
Question 1: Using LinkButton is a page that links to the details of each brand. So why not HyperLink instead of LinkButton?

In fact, HyperLink controls simply generate a hyperlink that URL points to, whereas LinkButton controls belong to the Button class. It supports event handling itself, does not have NavigateUrl attributes, and its URL linking function is mainly done by event handling -- LinkButton controls support server-side methods such as OnClick and OnCommand. It can be summarized as follows:

LinkButton has the same look and style as hyperlink, but it has two other benefits, namely:
◆ Click to return the same page.
◆ Easy to use OnClick method.
Question 2: LinkButton has been determined. So how do you define methods for multiple LinkButton controls in one page? Do you want to write the same method for each control? This is clearly impractical. If only one method is written, how do you determine which control is triggered and to whom is the server serving?
Here we apply LinkButton's property CommandName and method Command to implement.
First let's look at some of the more important properties and methods of LinkButton in 1.
◆ CommandName property: Gets or sets the command name associated with the LinkButton control. This value is passed to Command to handle the event with CommandArgument attribute 1.
◆ CommandArgument attribute: contains supplementary information about the command, such as Ascending sort order. And CommandName1.
◆ Click event: this event 1 is used when there is no command name associated with LinkButton control (e.g., "Submit" button).
◆ Command event: The Command event is raised when the LinkButton control is clicked. This event is typically used when a command name such as Sort is associated with an LinkButton control. This allows you to create multiple LinkButton controls on one web page and programmatically determine which LinkButton control was clicked.

After understanding the properties and methods of LinkButton. We can assign the CommandName attribute and CommandArgument attribute of LinkButton in the program, and then get the data from the CommandEventArgs class through the Command event, and then determine which LinkButton is triggered. The CommandEventArgs class stores data related to button (Button) events and can be accessed through the properties of the CommandEventArgs class during event processing.

Code:
 
private void BrandLink_Click(object sender, System.Web.UI.WebControls.CommandEventArgs e) 
{ 
string cmd = e.CommandName; 
switch(cmd) 
{ 
case "BrandLink1": 
this.Session["fileName"]=BrandLink1.Text; 
Server.Transfer("BrandPic.aspx"); 
break; 
case "BrandLink2": 
this.Session["fileName"]=BrandLink2.Text; 
Server.Transfer ("BrandPic.aspx"); 
break; 
case "BrandLink3": 
this.Session["fileName"]=BrandLink3.Text; 
Server.Transfer("BrandPic.aspx"); 
break; 
case "BrandLink4": 
this.Session["fileName"]=BrandLink4.Text; 
Server.Transfer("BrandPic.aspx"); 
break; case "BrandLink5": 
this.Session["fileName"]=BrandLink5.Text; 
Server.Transfer("BrandPic.aspx"); 
break; 
case "BrandLink6": 
this.Session["fileName"]=BrandLink6.Text; 
Server.Transfer("BrandPic.aspx"); 
break; 
case "BrandLink7": 
this.Session["fileName"]=BrandLink7.Text; 
Server.Transfer("BrandPic.aspx"); 
break; 
default: 
this.Session["fileName"]=BrandLink0.Text; 
Server.Transfer("Shop.aspx"); 
break; 
} 
} 

Related articles: