ASP.Net/Asp Control/Link Button

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

asp:linkbutton: command name and arguments

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
End Sub
Sub CommandButton_Click(Sender As Object, E As CommandEventArgs)
  lblButtonPressed.Text = "You pressed: " _
      & E.rumandName & "<BR>With this argument: " _
      & E.rumandArgument & "<BR>"
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Creating a Basic LinkButton Control</TITLE>
</HEAD>
<form runat="server">
<asp:label 
    id="lblButtonPressed" 
    runat="server"
/>
<BR><BR>
<asp:linkbutton 
    id="lnkbutSortName"
    text="Sort By Name"
    oncommand="CommandButton_Click"
    commandname="Sort"
    commandargument="By Name"
    runat="server"
/>
<BR><BR>
<asp:linkbutton 
    id="lnkbutSortPrice"
    text="Sort By Price"
    oncommand="CommandButton_Click"
    commandname="Sort"
    commandargument="By Price"
    runat="server"
/>
<BR><BR>
<asp:linkbutton 
    id="lnkbutNewSearch"
    text="New Search"
    oncommand="CommandButton_Click"
    commandname="New Search"
    commandargument="None"
    runat="server"
/>
</form>
</BODY>
</HTML>



asp:link button event to change the label text

<%@ Page Language="VB" %>
<script runat="server">
    Sub LinkButton1_Click(sender As Object, e As EventArgs)
      Label1.Text = "This is item 1"
    End Sub
    
    Sub LinkButton2_Click(sender As Object, e As EventArgs)
      Label1.Text = "Some stuff about item 2"
    End Sub
    
    Sub LinkButton3_Click(sender As Object, e As EventArgs)
      Label1.Text = "And now onto item 3"
    End Sub
</script>
<html>
<body>
    <form runat="server">
    <table style="WIDTH: 300px; HEIGHT: 150px" height="20">
        <tbody>
            <tr>
                <td>
                    <asp:LinkButton id="LinkButton1" onclick="LinkButton1_Click" runat="server">Item 1</asp:LinkButton>
                </td>
                <td>
                    <asp:LinkButton id="LinkButton2" onclick="LinkButton2_Click" runat="server">Item 2</asp:LinkButton>
                </td>
                <td>
                    <asp:LinkButton id="LinkButton3" onclick="LinkButton3_Click" runat="server">Item 3</asp:LinkButton>
                </td>
            </tr>
        </tbody>
    </table>
    <asp:Label id="Label1" runat="server">Label</asp:Label>
    </form>
</body>
</html>



OnCommand, for asp:linkbutton

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    lblMessage.Text = "Press a button!"
End Sub
Sub SubmitBtn1_Click(Sender As Object, E As EventArgs)
    lblMessage.Text = "You pressed the alone button."
End Sub
Sub SubmitBtn2_Click(Sender As Object, E As CommandEventArgs)
    lblMessage.Text = "The button you clicked: " _
        & e.rumandName _
        & ", has the following arguement: " _
        & e.rumandArgument & "."
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Link Button Controls Sample Page</TITLE>
</HEAD>
<BODY>
<form runat="server">
<Font Face="Tahoma" Size="+1">
<asp:Label 
    id="lblMessage" 
    Text="Press a button!"
    runat="server"
/>
<BR><BR>
<asp:LinkButton 
    id="butAlone"
    text="Alone Button"
    OnClick="SubmitBtn1_Click" 
    runat="server"
/>
<BR><BR>
<asp:LinkButton 
    id="butAdd"
    text="Add Record"
    CommandName="Add"
    CommandArgument="None"
    OnCommand="SubmitBtn2_Click" 
    runat="server"
/>
<asp:LinkButton 
    id="butEdit"
    text="Edit Record"
    CommandName="Edit"
    CommandArgument="None"
    OnCommand="SubmitBtn2_Click" 
    runat="server"
/>
<asp:LinkButton 
    id="butSortA"
    text="Sort A-Z"
    CommandName="Sort"
    CommandArgument="A"
    OnCommand="SubmitBtn2_Click" 
    runat="server"
/>
<asp:LinkButton 
    id="butSortD"
    text="Sort Z-A"
    CommandName="Sort"
    CommandArgument="D"
    OnCommand="SubmitBtn2_Click" 
    runat="server"
/>
</Font>
</Form>
</BODY>
</HTML>