Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
484 views
in Technique[技术] by (71.8m points)

asp.net - Event won't fire to dynamically added control

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.

This is the code that adds the controls

    Dim SavePath As String = "e:ftprootimagesTechNet"
    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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...