simple application of LinkButton control in Project
We know that there is a set of controls in the Web control that are used for form submission and return, the button control. Such controls are used to submit pages with user input values to the server so that they can be processed using code in the page. It produces a click event on the server for users to use in their code.
button controls can be grouped into button controls, LinkButton controls, and ImageButton controls. They are all used to submit pages. Where the ImageButton control can be displayed as an image and can provide the coordinates where the user clicks the position. The LinkButton control is displayed as a hyperlink on the page.
In the online shoe store system, we simply applied the method of using multiple LinkButton controls on one page to execute the same command. Next we have a more detailed introduction.
Problem one: Using LinkButton is a page that is used to link to individual branding details. So why not choose the navigation hyperlink and replace it with LinkButton?
In fact, the hyperlink control simply produces a hyperlink that has a URL pointing, and the LinkButton control belongs to the Button class control. It itself supports event handling, and there is no NavigateUrl property, its URL link function is mainly handled by the event processing--linkbutton control support onclick, OnCommand and other server-side methods. Can be summarized as follows:
LinkButton has the same appearance and style as hyperlink, but it has two other benefits, namely:
You can return to the same Web page when clicked.
Easy to use OnClick method.
Question two: The use of LinkButton has been identified. So how do you define methods on multiple LinkButton controls on a single page? Do you want to write the same method for each control? This is clearly unrealistic. So if you write only one method, and how do you determine which control is triggering, who does the server serve?
Here we apply the LinkButton property CommandName and the method command to implement it.
First, let's take a look at some of the more important attributes and methods of LinkButton.
CommandName property: Gets or sets the name of the command associated with the LinkButton control. This value is passed along with the CommandArgument property to the command handling event.
CommandArgument property: Contains supplemental information about the command, such as the ascending sort order. Used in conjunction with CommandName.
Click event: This event is typically used when there is no command name associated with LinkButton control, such as the 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 a LinkButton control. This allows you to create multiple LinkButton controls on a Web page and programmatically determine which LinkButton control is clicked.
After you understand the properties and methods of LinkButton. We can assign values to LinkButton's CommandName and CommandArgument properties in the program, and then get the data from the CommandEventArgs class through the command event. And then determine which LinkButton is triggered. Where the CommandEventArgs class stores data related to a button event, and can access the data through the properties of the CommandEventArgs class in event handling.
Code:
Copy Code code 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
}
}