Simple application of the Linkbutton control in the project
We know that there is a set of controls in the web Control for form submission and return, that is, the Button control. This type of control is used to submit pages with user input values to the server, so that these values can be processed using code on the page. It generates a Click event on the server for use in the code.
The Button control can be divided into three types: button control, LinkButton control, and ImageButton control. They are used to submit pages. The ImageButton control can be displayed as an image and provides the coordinates of the clicked position. The LinkButton control is displayed as a hyperlink on the page.
In the online shoe store system, we simply applied the method of executing the same command using multiple LinkButton controls on a single page. Next we will give a more detailed introduction.
Question 1: Using LinkButton is used to link to the details page of each brand. So why don't I use the navigation HyperLink instead of the LinkButton?
In fact, the HyperLink control only generates a HyperLink with a URL pointing to, and the LinkButton control belongs to the Button control. It supports event processing and does not have the NavigateUrl attribute. Its URL link function is mainly completed by event processing. The LinkButton control supports Server methods such as OnClick and OnCommand. It can be summarized as follows:
The appearance and style of a LinkButton are the same as that of hyperlink, but it also has two advantages:
◆ You can click to return to the same webpage.
◆ Easy-to-use OnClick method.
Problem 2: You are sure to use LinkButton. So how do I define multiple LinkButton controls in one page? Do you want to write the same method for each control? This is obviously impractical. If you only write one method, how can you determine which control is triggered and who the server provides the service?
Here we apply the CommandName attribute of LinkButton and the method Command to implement it.
First, let's take a look at several important attributes and methods of LinkButton.
◆ CommandName attribute: gets or sets the command name related to the LinkButton Control item. This value is passed to the Command to process the event together with the CommandArgument attribute.
◆ CommandArgument attribute: contains additional information about commands, such as Ascending sorting order. And CommandName.
◆ Click Event: this event is generally used when the command name is not associated with the LinkButton control (for example, the "Submit" button.
◆ Command event: a Command event is triggered when you click the LinkButton control. This event is usually used when the command name (such as Sort) is associated with the LinkButton control. This allows you to create multiple LinkButton controls on a web page and determine which one is clicked programmatically.
After learning about the attributes and methods of LinkButton. In the program, we can assign values to the CommandName and CommandArgument attributes of the LinkButton, and then obtain data from the CommandEventArgs class through the Command event to determine which LinkButton is triggered. The CommandEventArgs class stores data related to Button events, and can access the data through CommandEventArgs class attributes in event processing.
Code:
Copy codeThe Code is as follows:
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;
}
}