简体   繁体   中英

how to add Button Click Event in asp.net with vb

I have problem with onclick event. Here is an example which is similar to my project

HTML

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Example.aspx.vb" Inherits="Example" %>
<html>
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        <br />
        <asp:Label ID="label1" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>

VB

Partial Class Example
    Inherits System.Web.UI.Page
    Dim No_of_Animals As Integer = 6 ' Number of Animals
    Dim Cage(No_of_Animals) As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        label1.Text = " Animal released:  " & Session.Item("AnimalReleased")
        Cage(0) = "Cow"
        Cage(1) = "Bat"
        Cage(2) = "Dog"
        Cage(3) = "Cat"
        Cage(4) = "Snake"
        Cage(5) = "Pig"
        Dim html As New StringBuilder()
        html.Append("<table>")
        For Animal = 1 To No_of_Animals
            html.Append("<tr><td>Cage " & Animal & " : </td>")
            html.Append("<td> " & Cage(Animal - 1) & "</td>")
            html.Append("<td><button runat=""server"" OnServerClick=""DisplayAnimal(" & Animal - 1 & ")"">Release</button></td></tr>")
        Next
        html.Append("</table>")
        PlaceHolder1.Controls.Add(New Literal() With {.Text = html.ToString()})
    End Sub
    Public Sub DisplayAnimal(ByVal CageNo As Integer)
        Session.Item("AnimalReleased") = Cage(CageNo)
    End Sub
End Class

After I click a button the page will just refresh but session.item("AnimalRelease") was never given a value.

I'm suspecting onclick is not functioning well.

根据MSDN,OnServerClick应该全部小写:“ onserverclick”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM