Asp. NET Webfrom Component LinkButton programming

Source: Internet
Author: User
Asp.net|web| programming

The LinkButton component in the ASP.net WebForm component is also a server-side component that is somewhat similar to the <A> identifier in HTML. Its main function is to display a hyperlink in the ASP.net page. When the link is pressed, the page passes information to the server and handles the corresponding event on the server side.

A Important components of the LinkButton component:

Let's use this table to illustrate some of the common properties and events of the LinkButton component.

The common attributes of the LinkButton component, the meaning of the event representative
Text This is an attribute, which is the text that the LinkButton component displays
Commandname,commandargument these two properties are essentially the same in functionality, and when the Click event is triggered, it is easy to pass data to the server by these two properties. Some important uses for the use of these two attributes are described below.
The OnClick event is triggered when the component is click
OnCommand This is also an event and is similar to the above onclick event, except that this event passes the Commandname,commandargument property value to the server when it is triggered.

Two Take advantage of the CommandName and CommandArgument properties in the asp.net page:

You might be a little confused about the CommandName and CommandArgument attributes in the LinkButton component, and what they do. In fact, they have a very important role. Examples are as follows:

If there are 100 LinkButton in one of your pages, be on the server side to determine which LinkButton is being pressed. Without these two properties, you can assign a value to each LinkButton Text property, and then check the Text property value to determine which LinkButton is being pressed. Of course you can also by giving each LinkButton a different name (of course you just want the same name, in the ASP.net is also not possible), and then in the event handler, by detecting the sender attribute value to determine which LinkButton was pressed. Although these methods are feasible, none of them are trivial. With the CommandName attribute and the CommandArgument attribute, you can easily solve these problems. Assign values to LinkButton's CommandName and CommandArgument properties in your program, and then, through the OnCommand event, you get the data from the CommandEventArgs class. thereby to judge which LinkButton was pressed. So what are the CommandEventArgs? The CommandEventArgs class stores data related to button events and can access the data through the properties of the CommandEventArgs class in event handling. To be clear, when the LinkButton is pressed, the data triggered by this LinkButton is stored in the CommandEventArgs class of the server. Accessing the properties in the CommandEventArgs class also accesses the LinkButton that was pressed. That said, perhaps you are still a little unclear, the following is a procedural example to specify.
Three Specific usage of LinkButton components:

This article describes how the LinkButton component passes CommandName property values and CommandArgument property values through a OnCommand () event that is associated with a LinkButton component. And how to read the data in the CommandEventArgs class in the program.
The following illustration is the interface that this program is running:



Link.aspx source code is as follows:

<% @ Page Language = "C #" Debug = "true"%>
<title >link Button Component Demo program </title
<script runat = "server" >
protected void Linkbutton_command (object sender, CommandEventArgs e)
{
Message. Text = "Your choice is:" + E. CommandArgument;
}
</script >
<body >
<form runat = "server" >
<table cellspacing = 0 cellpadding = 0 border = 1 >
<TR > <TD > Please select a date: </td > </tr >
<TR >
<TD align = center >
<asp:linkbutton Text = "Monday" runat = "server" CommandArgument = "Monday" OnCommand = "Linkbutton_command"/>
</TD >
</TR >
<TR >
<TD align = center >
<asp:linkbutton Text = "Tuesday" runat = "server" CommandArgument = "Tuesday" OnCommand = "Linkbutton_command"/>
</TD >
</TR >
<TR >
<TD align = center >
<asp:linkbutton Text = "Wednesday" runat = "server" CommandArgument = "Wednesday" OnCommand = "Linkbutton_command"/>
</TD >
</TR >
<TR >
<TD align = center >
<asp:linkbutton Text = "Thursday" runat = "server" CommandArgument = "Thursday" OnCommand = "Linkbutton_command"/>
</TD >
</TR >
<TR >
<TD align = center >
<asp:linkbutton Text = "Friday" runat = "server" CommandArgument = "Friday" OnCommand = "Linkbutton_command"/>
</TD >
</TR >
<TR >
<TD align = center >
<asp:linkbutton Text = "Saturday" runat = "server" CommandArgument = "Saturday" OnCommand = "Linkbutton_command"/>
</TD >
</TR >
<TR >
<TD align = center >
<asp:linkbutton Text = "Sunday" runat = "server" CommandArgument = "Sunday" OnCommand = "Linkbutton_command"/>
</TD >
</TR >
</table >
<asp:label id = "message" runat = "server"/>
</form >
</body >

Four Summarize:

This article describes some of the important components of the LinkButton component, namely, some important properties and events. And through a program example to specify his use of methods. The presence of LinkButton components makes it increasingly convenient to pass parameters in asp.net pages. One drawback of this component, however, is that it cannot be used on browsers that do not support JavaScript languages, because he submits the page using the Submit () method in the JavaScript language.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.