【发布时间】:2015-05-01 06:47:14
【问题描述】:
我一直在阅读帖子和链接,以及有关视图状态、状态包、回发和页面创建的文章。
我是一个相当不错的业余爱好者,但这个人并没有陷入困境,有人可以给我举个例子来说明如何做到这一点吗?
好的,这就是我想用这个例子做的事情
创建一个表,其中左列有链接按钮
当每个链接按钮被点击时,它会在右栏中打开一堆图像按钮
然后每个 Imagebutton 都会弹出一条消息
问题是imagebutton没有触发消息
这里是前端:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="simple.aspx.vb" Inherits="simple" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>simple</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Table id="MainTable" runat="server">
<asp:TableRow>
<asp:TableCell ID ="cell1" BackColor="LightBlue" />
<asp:TableCell ID ="cell2" BackColor="LightBlue" />
</asp:TableRow>
</asp:Table>
</form>
</body>
</html>
这里是代码:
Partial Class simple
Inherits System.Web.UI.Page
Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
Dim NewTableButton As LinkButton = New LinkButton
NewTableButton.Text = "Text Created from 1st SQL Query"
AddHandler NewTableButton.Click, AddressOf LoadContacts
cell1.Controls.Add(NewTableButton)
End Sub
Protected Sub LoadContacts(sender As Object, e As System.EventArgs)
Dim TextPassedfromLinkButton As String = sender.text.ToString
Dim imgGear As New ImageButton
imgGear.ImageUrl = "../graphics/gear.jpg"
imgGear.AlternateText = "Text Created from 2nd SQL Query using the " _
& TextPassedfromLinkButton & " as a query parameter"
AddHandler imgGear.Click, AddressOf message
cell2.Controls.Add(imgGear)
End Sub
Protected Sub message(ByVal sender As Object, ByVal e As System.EventArgs)
Dim strText As String = sender.text.ToString
Dim alertString As String = "alert('" & strText & "');"
ScriptManager.RegisterStartupScript(Me, [GetType](), "showalert", alertString, True)
End Sub
End Class
这是点击应该弹出消息的按钮后的视图源
<table id="MainTable">
<tr>
<td id="cell1" style="background-color:LightBlue;"><a href="javascript:__doPostBack('ctl03','')">Text Created from 1st SQL Query</a></td>
<td id="cell2" style="background-color:LightBlue;"><input type="image" name="ctl04" src="../graphics/gear.jpg" alt="Text Created from 2nd SQL Query using the Text Created from 1st SQL Query as a query parameter" /></td>
</tr>
</table>
所以与此同时,我得到了这个工作(更新 10:49 EST)
Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
Dim NewTableButton As LinkButton = New LinkButton
NewTableButton.Text = "Text Created from 1st SQL Query"
'AddHandler NewTableButton.Click, AddressOf LoadContacts
AddHandler NewTableButton.Click, Function() LoadContacts(NewTableButton.Text)
cell1.Controls.Add(NewTableButton)
If IsPostBack Then
LoadContacts(NewTableButton.Text)
End If
End Sub
Private Function LoadContacts(strText As String) As Integer
Dim TextPassedfromLinkButton As String = strText
Dim imgGear As New ImageButton
imgGear.ID = "uniquename"
EnableViewState = False
imgGear.ImageUrl = "../graphics/gear.jpg"
imgGear.AlternateText = "Text Created from 2nd SQL Query using the " & TextPassedfromLinkButton & " as a query parameter"
AddHandler imgGear.Click, AddressOf message
cell2.Controls.Add(imgGear)
Return 0
End Function
唯一的问题是我得到 2 个相同的 imgGears
duplicate buttons http://reinsmith.net/graphics/simple1.png message displayed http://reinsmith.net/graphics/simple2.png
【问题讨论】:
标签: asp.net vb.net postback viewstate