如何使html5视频在单击UpdatePanel asp.net中的按钮时不重新加载。

huangapple go评论58阅读模式
英文:

How to make html5 Video not reload when Button is clicked in UpdatePanel asp.net

问题

I have a repeater that displays video lists and a like button. I leave it inside the UpdatePanel. However, when I click the "Like" button, it seems that my video reloads from the beginning, the feeling that the video flashes up looks very annoying.

How do I not have to reload the video when the Like button event occurs?

Here is the code I use:

<asp:UpdatePanel ID="up__" runat="server">
    <ContentTemplate>
        <asp:Repeater ID="rpt_one" runat="server" ClientIDMode="Static" OnItemCommand="rpt_one_ItemCommand">
            <ItemTemplate>
                <video ID="datavideo" autoplay playsinline muted loop="true" controls class="overflow-hidden" disablepictureinpicture controlslist="nodownload noplaybackrate ">
                    <source type="video/mp4" src='<%# Eval("Video") %>'/>
                </video>
                <asp:Button ID="bt_like" ClientIDMode="Static" CommandArgument='<%# Eval("ID") %>' CommandName="like" runat="server" Text="&#xf004;" />
            </ItemTemplate>
        </asp:Repeater>
    </ContentTemplate>
</asp:UpdatePanel>

Looking forward to everyone's help. Thank you!

英文:

I have a repeater that displays video lists and a like button. I leave it inside the UpdatePanel. However, when I click the "Like" button, it seems that my video reloads from the beginning, the feeling that the video flashes up looks very annoying.

How do I not have to reload the video when button Like event?

如何使html5视频在单击UpdatePanel asp.net中的按钮时不重新加载。

Here is the code I use:

&lt;asp:UpdatePanel ID=&quot;up__&quot; runat=&quot;server&quot;&gt;
	&lt;ContentTemplate&gt;
		&lt;asp:Repeater ID=&quot;rpt_one&quot; runat=&quot;server&quot; ClientIDMode=&quot;Static&quot; OnItemCommand=&quot;rpt_one_ItemCommand&quot;&gt;
			&lt;ItemTemplate&gt;
				&lt;video ID=&quot;datavideo&quot; autoplay playsinline muted loop=&quot;true&quot; controls class=&quot;overflow-hidden&quot; disablepictureinpicture controlslist=&quot;nodownload noplaybackrate &quot;&gt;
					&lt;source type=&quot;video/mp4&quot; src=&#39;&lt;%# Eval(&quot;Video&quot;) %&gt;&#39;/&gt;
				&lt;/video&gt;
				&lt;asp:Button ID=&quot;bt_like&quot; ClientIDMode=&quot;Static&quot; CommandArgument=&#39;&lt;%# Eval(&quot;ID&quot;) %&gt;&#39; CommandName=&quot;like&quot; runat=&quot;server&quot; Text=&quot;&amp;#xf004;&quot; /&gt;
			&lt;/ItemTemplate&gt;
		&lt;/asp:Repeater&gt;  
	&lt;/ContentTemplate&gt;
&lt;/asp:UpdatePanel&gt;

Looking forward to everyone's help. Thank you!

答案1

得分: 0

你正在经历的问题是由ASP.NET中UpdatePanel的工作方式所引起的。默认情况下,在UpdatePanel内发生回发时,UpdatePanel内的整个内容都会被重新渲染,包括视频元素,这会导致视频重新加载。

为了防止在点击“Like”按钮时视频重新加载,你可以使用JavaScript,并单独处理按钮点击事件,而不依赖于UpdatePanel。

以下是使用JavaScript和jQuery实现所需行为的代码更新版本:

&lt;asp:Repeater ID=&quot;rpt_one&quot; runat=&quot;server&quot; ClientIDMode=&quot;Static&quot; OnItemCommand=&quot;rpt_one_ItemCommand&quot;&gt;
    &lt;ItemTemplate&gt;
        &lt;video autoplay playsinline muted loop=&quot;true&quot; controls class=&quot;overflow-hidden&quot; disablepictureinpicture controlslist=&quot;nodownload noplaybackrate &quot;&gt;
            &lt;source type=&quot;video/mp4&quot; src=&#39;&lt;%# Eval(&quot;Video&quot;) %&gt;&#39;/&gt;
        &lt;/video&gt;
        &lt;asp:Button ID=&quot;bt_like&quot; ClientIDMode=&quot;Static&quot; CommandArgument=&#39;&lt;%# Eval(&quot;ID&quot;) %&gt;&#39; CommandName=&quot;like&quot; runat=&quot;server&quot; Text=&quot;&amp;#xf004;&quot; 
            OnClientClick=&#39;&lt;%# &quot;handleLikeClick(&quot; + Eval(&quot;ID&quot;) + &quot;); return false;&quot; %&gt;&#39; /&gt;
    &lt;/ItemTemplate&gt;
&lt;/asp:Repeater&gt;

&lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
   function handleLikeClick(videoId) {

        // Set the CommandArgument of the button to the videoId
        button.setAttribute(&quot;data-command-argument&quot;, videoId);

        // Make an AJAX call to trigger the server-side event
        $.ajax({
            type: &quot;POST&quot;,
            url: &quot;YourPage.aspx/LikeButtonClicked&quot;,
            data: JSON.stringify({ videoId: videoId }),
            contentType: &quot;application/json; charset=utf-8&quot;,
            dataType: &quot;json&quot;,
            success: function(response) {
                // Handle the response from the server, if needed
            },
            error: function(error) {
                // Handle any errors that occur during the AJAX call
            }
        });
        
        // Prevent postback
        return false;
    }
&lt;/script&gt;

using System.Web.Services;

public partial class YourPage : System.Web.UI.Page
{
    [WebMethod]
    public static void LikeButtonClicked(string videoId)
    {
        // Access the videoId parameter passed from the client-side
        // using the CommandArgument property of the button
        var button = HttpContext.Current.Request.Params[&quot;__EVENTTARGET&quot;];
        var videoId = ((Button)button).CommandArgument;
        
        // Example: Update the database to increment the like count
        UpdateLikeCount(videoId);
    }
    
    private static void UpdateLikeCount(string videoId)
    {
        // Update the like count for the given videoId in the database
    }
}

希望这能正常工作!

英文:

The issue you're experiencing is due to the nature of how the UpdatePanel in ASP.NET works. By default, when a postback occurs within the UpdatePanel, the entire content inside the UpdatePanel gets re-rendered, including the video element, which causes it to reload.

To prevent the video from reloading when the "Like" button is clicked, you can use JavaScript and handle the button click event separately without relying on the UpdatePanel.

Here's an updated version of your code that uses JavaScript and jQuery to achieve the desired behavior:

&lt;asp:Repeater ID=&quot;rpt_one&quot; runat=&quot;server&quot; ClientIDMode=&quot;Static&quot; OnItemCommand=&quot;rpt_one_ItemCommand&quot;&gt;
    &lt;ItemTemplate&gt;
        &lt;video autoplay playsinline muted loop=&quot;true&quot; controls class=&quot;overflow-hidden&quot; disablepictureinpicture controlslist=&quot;nodownload noplaybackrate &quot;&gt;
            &lt;source type=&quot;video/mp4&quot; src=&#39;&lt;%# Eval(&quot;Video&quot;) %&gt;&#39;/&gt;
        &lt;/video&gt;
        &lt;asp:Button ID=&quot;bt_like&quot; ClientIDMode=&quot;Static&quot; CommandArgument=&#39;&lt;%# Eval(&quot;ID&quot;) %&gt;&#39; CommandName=&quot;like&quot; runat=&quot;server&quot; Text=&quot;&amp;#xf004;&quot; 
            OnClientClick=&#39;&lt;%# &quot;handleLikeClick(&quot; + Eval(&quot;ID&quot;) + &quot;); return false;&quot; %&gt;&#39; /&gt;
    &lt;/ItemTemplate&gt;
&lt;/asp:Repeater&gt;

&lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
   function handleLikeClick(videoId) {

        // Set the CommandArgument of the button to the videoId
        button.setAttribute(&quot;data-command-argument&quot;, videoId);

        // Make an AJAX call to trigger the server-side event
        $.ajax({
            type: &quot;POST&quot;,
            url: &quot;YourPage.aspx/LikeButtonClicked&quot;,
            data: JSON.stringify({ videoId: videoId }),
            contentType: &quot;application/json; charset=utf-8&quot;,
            dataType: &quot;json&quot;,
            success: function(response) {
                // Handle the response from the server, if needed
            },
            error: function(error) {
                // Handle any errors that occur during the AJAX call
            }
        });
        
        // Prevent postback
        return false;
    }
&lt;/script&gt;

using System.Web.Services;

public partial class YourPage : System.Web.UI.Page
{
    [WebMethod]
    public static void LikeButtonClicked(string videoId)
    {
        // Access the videoId parameter passed from the client-side
        // using the CommandArgument property of the button
        var button = HttpContext.Current.Request.Params[&quot;__EVENTTARGET&quot;];
        var videoId = ((Button)button).CommandArgument;
        
        // Example: Update the database to increment the like count
        UpdateLikeCount(videoId);
    }
    
    private static void UpdateLikeCount(string videoId)
    {
        // Update the like count for the given videoId in the database
    }
}

Hope it's work !

huangapple
  • 本文由 发表于 2023年5月30日 00:37:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定