简体   繁体   English

OnClientClick不适用于asp.net和vb.net

[英]OnClientClick does not work with asp.net and vb.net

I try to create a dynamic web solution with ASP.NET and VB. 我尝试使用ASP.NET和VB创建动态Web解决方案。

My webpage looks like this: 我的网页看起来像这样:

<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>

The VB part like this: 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

I want that if I click on one of the imagebutton the hole appereance of the frontpage dynamicly changes without loading the hole page new. 我希望如果单击imagebutton之一,则首页的孔外观会动态变化,而无需重新加载孔页。 So I defined several PlacHolder for that. 因此,我为此定义了几个PlacHolder。

If I run the webapp but the functionality is not working if I hit the button. 如果我运行该Web应用程序,但是如果我按下该按钮,则该功能不起作用。

I checked the source and found this tag: 我检查了源并找到了这个标签:

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

What is wrong with this solution? 该解决方案有什么问题?

Add to your Page_load a check for IsPostBack IsPostBack的支票添加到您的Page_load

if Not IsPostBack Then
   Load_main()
End If

This will prevent your page_load method to recreate every button and resetting the value for the Image_click event 这将防止您的page_load方法重新创建每个按钮并重置Image_click事件的值。

If you are trying to do this from the code behind, then you need to add the Click event handler: 如果您尝试从后面的代码中执行此操作,则需要添加Click事件处理程序:

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

and then have some function that does what you want: 然后具有一些您想要的功能:

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

Note, this will cause a postback. 请注意,这将导致回发。 It is confusing on how you expect to dynamically change the front page without reloading the page and without using JavaScript... 在您期望如何动态更改首页而不重新加载页面且不使用JavaScript的情况上,这令人困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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