Action Controls in ASP.NET

Introduction and Demonstration

Action controls allow users to perform some action on that page, such as navigating to a different URL, submitting a form, resetting a form's values, or executing a client script. Here is list of such controls.

ControlPurpose
ButtonDisplays a command button that posts a form to the server when clicked.
ImageButtonDisplays an image that posts a form to the server when clicked.
LinkButtonDisplays a hyperlink text that posts a form to the server when clicked.
HyperlinkDisplays a hyperlink text that navigates from one page to another when clicked.

The simplified syntax of these controls is as follows:

<asp:button id="MyButton" text="Click Me!!" runat="server" />
<asp:imagebutton id="MyImageButton" imageurl="aspnetian.jpg" runat="Server" />
<asp:linkbutton id="MyLinkButton" text="Click Me" runat="server" />
<asp:hyperlink id="MyHyperLink" text="Click Me" navigateurl="ActionControls.aspx"  target="_blank" runat="server" />

The controls can then be accessed programmatically with code fragments like the following:

MyButton.CommandName = "Sort"
MyImageButton.CommandArgument = "Ascending"
MyLinkButton.CommandName = "Filter"
MyHyperLink.NavigateUrl = "http://www.itorian.com"

Index.aspx Page

<%@ Page Language="vb" %> 
<html>
<
head>
    <title>Action Control Example</title>
    <script runat="server">
Sub Page_Load( )
MyButton.CommandName = "Sort"
MyImageButton.CommandArgument = "Ascending"
MyLinkButton.CommandName = "Filter"
MyHyperLink.NavigateUrl = "http://www.itorian.com"
End Sub
    </script
>
</head>
<
body>
    <h1>
        Action Control Example</h1>
    <form id="Form1" runat="server">
    <asp:Table ID="MyTable" border="1" CellPadding="5" CellSpacing="0" runat="server">
        <asp:TableRow ID="Tablerow1" runat="server">
            <asp:TableCell ID="Tablecell1" runat="server">
Button Control:
            </asp:TableCell
>
            <asp:TableCell ID="Tablecell2" runat="server">
                <asp:Button ID="MyButton" Text="Click Me!!" runat="server" />
            </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow ID="Tablerow2" runat="server">
            <asp:TableCell ID="Tablecell3" runat="server">
ImageButton Control:
            </asp:TableCell
>
            <asp:TableCell ID="Tablecell4" runat="server">
                <asp:ImageButton ID="MyImageButton" ImageUrl="aspnetian.jpg" runat="Server" />
            </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow ID="Tablerow3" runat="server">
            <asp:TableCell ID="Tablecell5" runat="server">
LinkButton Control:
            </asp:TableCell
>
            <asp:TableCell ID="Tablecell6" runat="server">
                <asp:LinkButton ID="MyLinkButton" Text="Click Me" runat="server" />
            </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow ID="Tablerow4" runat="server">
            <asp:TableCell ID="Tablecell7" runat="server">
HyperLink Control:
            </asp:TableCell
>
            <asp:TableCell ID="Tablecell8" runat="server">
                <asp:HyperLink ID="MyHyperLink" Text="Click Me" NavigateUrl="ActionControls.aspx"
                    Target="_blank" runat="server" />
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>
    </form>
</body>
</
html>

HAVE A HAPPY CODING!


Similar Articles