事件不会触发到动态添加的控件

事件不会触发到动态添加的控件

本文介绍了事件不会触发到动态添加的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据服务器文件夹中显示的视频数量将htmlvideo控件动态添加到我的Web表单中.这部分工作正常.所有视频都会显示并可以播放.我在代码中添加了"onended"事件作为属性和函数,但此事件不会触发.我知道,由于这些控件是在事实之后添加的,因此我必须添加一个侦听器,但只是不知道如何.

I'm dynamically adding htmlvideo controls to my web form based on the number of videos present on the server folder. This part works fine. All the videos show up and can be played. I add the 'onended' event as an attribute and the function in my code behind, but this event won't fire. I'm aware that since these controls are added after the fact I have to add a listener, but just don't know how.

这是添加控件的代码

    Dim SavePath As String = "e:\ftproot\images\TechNet\"
    Dim Directory As New DirectoryInfo(SavePath)
    Dim allFiles As IO.FileInfo() = Directory.GetFiles("*.mov")
    Dim VidCtr As Integer = 1
    For Each singlefile In allFiles
        Dim myVid As New HtmlVideo
        myVid.Src = "https://www.rawauto.com/images/TechNet/" & singlefile.Name
        myVid.Attributes.Add("height", 140)
        myVid.Attributes.Add("runat", "server")
        myVid.Attributes.Add("type", "video/mp4")
        myVid.Attributes.Add("controls", "controls")
        myVid.Attributes.Add("onended", "VidPlayed")
        myVid.Attributes.Add("id", "Vid" & VidCtr)
        Panel1.Controls.Add(myVid)

        Dim myLbl As New Label
        myLbl.Text = Replace(UCase(singlefile.Name), ".MOV", "")
        myLbl.Width = 250
        myLbl.CssClass = "VidStyle"
        myLbl.Font.Name = "calabri"
        myLbl.Font.Bold = True
        LPanel.Controls.Add(myLbl)
    Next

这是我在用户观看完视频后尝试触发的功能:

This is the function I'm trying to fire once the user has finished watching the video:

Protected Sub VidPlayed(sender As Object, e As EventArgs)
    Dim Tech As New SqlConnection("server=RAW-OTT; Initial Catalog=TechNet; Integrated Security=True;")
    Dim vid As HtmlVideo = sender
    Dim vidurl As String = vid.Src
    VidName = Replace(vidurl, "https://www.rawauto.com/images/TechNet/", "")
    If Len(VidName) > 50 Then
        VidName = Mid(VidName, 1, 50)
    End If
    Dim SqlStr As String = "Select * From TechTube Where Video = '" & VidName & "'"
    Dim ttA As New SqlDataAdapter(SqlStr, Tech)
    Dim ttT As New DataTable
    ttA.Fill(ttT)
    If ttT.Rows.Count = 0 Then
        SqlStr = "Insert Into TechTube Values ('" & VidName & "', 1, 0)"
        Dim tCmd As New SqlCommand(SqlStr, Tech)
        Tech.Open()
        tCmd.ExecuteNonQuery()
        Tech.Close()
    Else
        SqlStr = "Update TechTube Set Hits = Hits + 1 Where Video = '" & VidName & "'"
        Dim tCmd As New SqlCommand(SqlStr, Tech)
        Tech.Open()
        tCmd.ExecuteNonQuery()
        Tech.Close()
    End If
    RateLabel.Visible = True
    RatingBox.Visible = True
End Sub

推荐答案

这是任何动态控件的旧ViewState问题,与视频无关.

This is an old ViewState problem for any dynamic control, and has nothing specific to do with video.

请记住,每个回发将从头开始重建整个页面 ,包括动态控件.如果您仍然希望在回发后看到这些控件(包括所有服务器事件),则必须将它们重新添加到页面中.此外,如果要在此回发期间为控件触发事件,则需要还原控件的ViewState,并且要使ViewState还原,必须在运行之前,将控件添加回到重建的页面上. Page_Init或Page_PreInit可以很好地工作.

Remember, every PostBack rebuilds the entire page from scratch, including your dynamic controls. If you still want to see these controls after a postback (which includes all server events), you must re-add them to the page. Additionally, you need a control's ViewState restored if you want an event fired for the control during this PostBack, and for the ViewState to restore the control must be added back to the reconstructed page before the Page_Load event runs. Page_Init or Page_PreInit can work well for this.

最后,在这里考虑性能影响.您真的要在每次用户互动时重新构建整个页面吗,或者是时候学习使用javascript处理这些事情了,也许只有一个Web api只需接收一个javascript请求而无需重新构建整个页面是否在您的服务器和用户的浏览器中导致整个页面循环?

Finally, consider the performance implications here. Do you really want to rebuild the entire page on every user interaction, or is it perhaps time to learn to use javascript to process these things, with maybe a web api that only has to receive a javascript request without causing an entire page cycle both on your server and in the user's browser?

在许多其他场合中,只有几次被问及得到了答案:

Just a few of the many other times this has been asked and answered:

  • Dynamic Event Handler not Firing
  • dynamically added buttons not firing click event c#
  • dynamically created button click event not firing
  • Dynamically Added DropDownlists Are Not Firing SelectedIndexChanged Event
  • ASP.NET: Viewstate and programmatically adding user controls
  • Click events on Array of buttons
  • VB ASP dynamic button click event not hitting handler event

这篇关于事件不会触发到动态添加的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 20:54