单击按钮时避免页面刷新?

时间:2012-12-11 01:14:56

标签: c# asp.net

我有这个.net页面,我有一个媒体播放器控件和一个按钮。按钮功能是调用存储过程。当用户点击按钮时,我需要页面不重新加载和停止视频。

有办法做到这一点吗?

这是代码

watching.aspx

<div id="video" runat="server">
    <div id="myElement">
        Loading...
    </div>
</div>
<asp:Button ID="btnWatchlist" runat="server" Text="add to watchlist" onclick="btnWatchlist_Click" />

watching.aspx.cs

public partial class Watching : System.Web.UI.Page
{
    private int movieId = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
         movieId = Int32.Parse(Request.QueryString["id"]);
         Movie movie = MovieAccess.GetMovieDetails(movieId);
         startVideo(movie);      
    }
    private void startVideo(Movie movie)
    {
        string moviePath= "data/videos/"+movie.srcPath;
        String script = "<script type='text/javascript'>jwplayer('myElement').setup({file: '" + moviePath + "',image: '43.jpg', \"width\": 800,\"height\": 450,});</script>";
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "video", script, false);
    }
    protected void btnWatchlist_Click(object sender, EventArgs e)
    {
        MovieAccess.AddWatchlistMovie(movieId, User.Identity.Name);
    }
}

3 个答案:

答案 0 :(得分:0)

您可以向服务器进行客户端调用(使用javascript或流行的jQuery库)。谷歌上有很多教程。

答案 1 :(得分:0)

Button放入UpdatePanel

答案 2 :(得分:0)

protected void Page_Load(object sender, EventArgs e)
    {
         if(!IsPostBack)
         {
           movieId = Int32.Parse(Request.QueryString["id"]);
           Movie movie = MovieAccess.GetMovieDetails(movieId);
           startVideo(movie);    
         }  
    }