1

我尝试使用 ASP.NET 和 VB 创建动态 Web 解决方案。

我的网页如下所示:

<div id="menu" style="position:relative;text-align:center">
        <asp:PlaceHolder runat="server" id="PHimg1" />
        <asp:PlaceHolder runat="server" id="PHimg2" />
        <asp:PlaceHolder runat="server" id="PHimg3" />
        <br />
        <asp:PlaceHolder runat="server" id="PHimg4" />
        <asp:PlaceHolder runat="server" id="PHimg5" />
        <asp:PlaceHolder runat="server" id="PHimg6" />
        <br />
        <asp:PlaceHolder runat="server" id="PHimg7" />
        <asp:PlaceHolder runat="server" id="PHimg8" />
        <asp:PlaceHolder runat="server" id="PHimg9" />
</div>

VB部分是这样的:

Public Class index
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Load_Main()
End Sub

Protected Sub Load_Main()
    Dim img1 As New ImageButton()
    Dim img2 As New ImageButton()
    Dim img3 As New ImageButton()
    Dim img4 As New ImageButton()
    Dim img5 As New ImageButton()
    Dim img6 As New ImageButton()

    img1.ImageUrl = "./resources/EssenTrinken.png"
    img1.OnClientClick = "Load_Eating()"

    img2.ImageUrl = "./resources/Einkaufen2-black.png"
    img3.ImageUrl = "./resources/Special-black.png"
    img4.ImageUrl = "./resources/KosmetikWellness.png"
    img5.ImageUrl = "./resources/KulturGeschichte.png"
    img6.ImageUrl = "./resources/Unterhaltung2-black.png"

    PHimg1.Controls.Add(img1)
    PHimg2.Controls.Add(img2)

    PHimg4.Controls.Add(img3)
    PHimg5.Controls.Add(img4)

    PHimg7.Controls.Add(img5)
    PHimg8.Controls.Add(img6)
End Sub

Protected Sub Load_Eating()
    Dim img1 As New ImageButton()
    Dim img2 As New ImageButton()
    Dim img3 As New ImageButton()
    Dim img4 As New ImageButton()
    Dim img5 As New ImageButton()
    Dim img6 As New ImageButton()

    img1.ImageUrl = "./resources/zurueck.png"
    img2.ImageUrl = "./resources/Bars2.png"
    img3.ImageUrl = "./resources/Cafe.png"
    img4.ImageUrl = "./resources/FastFood2.png"
    img5.ImageUrl = "./resources/Restaurant.png"
    img6.ImageUrl = "./resources/Baeckerei.png"

    PHimg1.Controls.Add(img1)
    PHimg2.Controls.Add(img2)

    PHimg3.Controls.Add(img3)
    PHimg4.Controls.Add(img4)

    PHimg5.Controls.Add(img5)
    PHimg6.Controls.Add(img6)
End Sub

End Class

我希望如果我单击其中一个图像按钮,则首页的孔外观会动态变化,而无需加载新的孔页。所以我为此定义了几个 PlacHolder。

如果我运行 web 应用程序,但如果我按下按钮,则该功能不起作用。

我检查了来源并找到了这个标签:

<input type="image" name="ctl03" src="./resources/EssenTrinken.png" onclick="Load_Eating();" />

这个解决方案有什么问题?

4

2 回答 2

1

添加到您的 Page_load 检查IsPostBack

if Not IsPostBack Then
   Load_main()
End If

这将阻止您的 page_load 方法重新创建每个按钮并重置 Image_click 事件的值

于 2013-03-31T22:45:55.743 回答
1

如果您尝试从后面的代码中执行此操作,则需要添加 Click 事件处理程序:

Protected Sub Load_Main()
    Dim img1 As New ImageButton()    
    AddHandler img1.Click, AddressOf Img1Click
    'rest of your code

然后有一些功能可以满足您的需求:

Private Sub Img1Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
    'some code
End Sub

请注意,这将导致回发。您希望如何在不重新加载页面且不使用 JavaScript 的情况下动态更改首页,这令人困惑......

于 2013-03-31T23:03:12.160 回答