如何使用vb在asp.net中添加Button Click事件

时间:2015-03-09 18:29:22

标签: html asp.net vb.net

我遇到onclick事件的问题。这是一个类似于我的项目的例子

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

单击按钮后,页面将只刷新,但session.item("AnimalRelease")从未给出值。

我怀疑onclick效果不佳。

1 个答案:

答案 0 :(得分:0)

根据MSDN,OnServerClick应该全是小写:&#34; onserverclick&#34;。