使用带有UpdatePanel的Javascript播放声音
本文关键字:Javascript 播放声音 UpdatePanel | 更新日期: 2023-09-27 18:22:28
我有下面的Asp.net代码:
问题是,当Timer每隔30秒滴答作响,GridView填充时,javascript代码不会执行!!并且不播放声音。
我也尝试过SoundPlayer Class,但不在服务器端工作,有人能为我提供解决方案吗?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="OrdersPage.aspx.cs" Inherits="Orders_" Async="true" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Gazar | Orders Page</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<link rel="Stylesheet" href="StyleSheet.css" />
<script type="text/javascript">
function ShowProgress() {
document.getElementById('UpdateProgress1').style.display = "inline";
}
</script>
</head>
<body>
<form id="form1" class="center_it" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" EnablePageMethods="true" runat="server"></asp:ToolkitScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<table class="MainTable">
<tr>
<td style="text-align: left;" colspan="2">
<img id="Img2" src="~/images/gazzarlogo.png" alt="GazarBanner" runat="server" class="BannerImage" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="30pt" ForeColor="White"
Text="الطلبـــــات"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Text="History" CssClass="Buttons" />
</td>
<td>
<asp:Button ID="Exit" Text="خروج" runat="server" OnClick="Exit_Click" CssClass="Buttons" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:GridView ID="GridViewOrders" runat="server" Style="margin-left: auto; margin-right: auto;"
OnRowDataBound="GridViewOrders_RowDataBound" PageSize="30" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="0px" CaptionAlign="Top"
CellPadding="4" ForeColor="Black" HorizontalAlign="Center" EnableModelValidation="True"
GridLines="Vertical" Width="1100px" Font-Names="Tahoma" Font-Size="10pt" Font-Bold="False"
OnRowCommand="GridViewOrders_RowCommand" AutoGenerateColumns="False">
<RowStyle BackColor="#F7F7DE" Wrap="True" />
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#CCCC99" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="Open" ItemStyle-Width="60px">
<ItemTemplate>
<asp:Button ID="ApproveBtn" Text="Open" Width="60" Height="30" runat="server" OnClientClick="ShowProgress()" />
</ItemTemplate>
<ItemStyle Width="70px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" ItemStyle-Width="70px">
<ItemTemplate>
<asp:Label ID="OrderID" CommandName="Select" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "ID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="70px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" ItemStyle-Width="150px">
<ItemTemplate>
<asp:Label ID="Name" CommandName="Select" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "Name") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Address" ItemStyle-Width="300px">
<ItemTemplate>
<asp:Label ID="Address" CommandName="Select" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "Address") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="300px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Number" ItemStyle-Width="100px">
<ItemTemplate>
<asp:Label ID="Number" CommandName="Select" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "Number") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Sent Date" ItemStyle-Width="100px">
<ItemTemplate>
<asp:Label ID="SentDate" CommandName="Select" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "Sent Date") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Recieved Date" ItemStyle-Width="100px">
<ItemTemplate>
<asp:Label ID="RecievedDate" CommandName="Select" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "Recieved Date") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="# Success" ItemStyle-Width="50px">
<ItemTemplate>
<asp:Label ID="SuccessNo" CommandName="Select" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "# Success") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="50px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="# Fake" ItemStyle-Width="50px">
<ItemTemplate>
<asp:Label ID="FakeNo" CommandName="Select" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "# Fake") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="50px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Restaurant" ItemStyle-Width="50px">
<ItemTemplate>
<asp:Label ID="Restaurant" CommandName="Select" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "Restaurant") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="50px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Branch" ItemStyle-Width="50px">
<ItemTemplate>
<asp:Label ID="Branch" CommandName="Select" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "Branch") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="50px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
<div class="container">
<div class="LoadingDiv">
<span class="LoadingLabel">Check Orders ...</span>
<img class="LoadingImg" src="Images/ajax-loader.gif" alt="Loading ..." />
</div>
</div>
</ProgressTemplate>
</asp:UpdateProgress>
<asp:Timer ID="Timer1" runat="server" Interval="30000" OnTick="Timer1_Tick" />
</form>
</body>
</html>
后面的代码是:
protected void Timer1_Tick(object sender, EventArgs e)
{
orderDB.LogState("Time Tick");
if (Session["UserID"] != null)
{
orderDB.LogState("Session Alive");
FillDataGrid(int.Parse(Session["UserID"].ToString()));
orderDB.LogState("End fill Grid View");
}
else
{
Response.Redirect("~/login.aspx");
}
}
private void FillDataGrid(int UserID)
{
Orders = orderDB.GetOrdersByUser(UserID);
orderDB.LogState("Select: " + DateTime.Now.ToString() + "'t" + Request.UserHostAddress + "'t" + Session.SessionID + "'t" + Session["UserID"].ToString());
if (Orders == null)
Response.Redirect("~/login.aspx");
DataTable Table = new DataTable();
Table.Columns.Add("ID", System.Type.GetType("System.String"));
Table.Columns.Add("Name", System.Type.GetType("System.String"));
Table.Columns.Add("Address", System.Type.GetType("System.String"));
Table.Columns.Add("Number", System.Type.GetType("System.String"));
Table.Columns.Add("Sent Date", System.Type.GetType("System.String"));
Table.Columns.Add("Recieved Date", System.Type.GetType("System.String"));
Table.Columns.Add("# Success", System.Type.GetType("System.String"));
Table.Columns.Add("# Fake", System.Type.GetType("System.String"));
Table.Columns.Add("Restaurant", System.Type.GetType("System.String"));
Table.Columns.Add("Branch", System.Type.GetType("System.String"));
for (int i = 0; i < Orders.Count; i++)
{
object[] myRow = new object[10];
myRow[0] = Orders[i].GetId();
myRow[1] = Orders[i].GetOwner();
myRow[2] = Orders[i].GetAddress().getAllAddress();
myRow[3] = Orders[i].GetPhoneNumber();
myRow[4] = Orders[i].GetOriginalTime().ToString();
myRow[5] = Orders[i].GetTime().ToString();
myRow[6] = Orders[i].NoOfSuccess; //success Orders;
myRow[7] = Orders[i].NoOfFake; //fake Orders
myRow[8] = Orders[i].GetResName();
myRow[9] = Orders[i].GetBranchName();
Table.Rows.Add(myRow);
}
orderDB.LogState("Table Rows Count: " + Table.Rows.Count);
GridViewOrders.DataSource = Table;
GridViewOrders.DataBind();
string sSelectedAudio = Server.MapPath("home.wav");
if (GridViewOrders.Rows.Count != 0)
{
playSound(sSelectedAudio);
orderDB.LogState("Sound Played: " + GridViewOrders.Rows.Count);
}
else
{
orderDB.LogState("Empty Grid View: " + GridViewOrders.Rows.Count);
}
}
private void playSound(string path)
{
string PlaySound = "<embed src='"" + path + "'" autostart='"true'" hidden='"true'"></embed>";
Response.Write(PlaySound);
}
可能是Response.Write
导致了问题。尝试使用占位符控件,然后将HtmlGenericControl
添加到其中,并为嵌入标记添加标记,即
HtmlGenericControl sound = new HtmlGenericControl("<embed src='"" + path + "'"
autostart='"true'" hidden='"true'"></embed>");
ucPlaceHolder.Controls.Add(sound);
确保占位符控件位于正在使用的UpdatePanel的ContentTemplate
内