播放声音是用JS控制的,我想把数据库中的声音文件在GridView里显示出来,然后用户点哪个按钮就播放哪个声音,可现在只能播放第一行的,下面两行的都放不了?
这个是单独播放声音
- HTML code
<script language="javascript" type="text/javascript"> function ManageSoundControl(action) { var soundControl = document.getElementById("soundControl"); if(action == "play") { soundControl.play(); } if(action == "stop") { soundControl.stop(); } } </script><embed id="soundControl" src="upfiles/263508.mp3" loop=false autostart=false width=10 height=10></embed> <input id="btPlay" type="button" value="播放" onclick="ManageSoundControl('play')" /> <input id="btStop" type="button" value="停止" onclick="ManageSoundControl('stop')" />
我给改成GridView,然后就只能播放test表中的第一首歌,总共有三首歌,263508.mp3,测试9.26.WAV,测试录音9.26.pcm.这三首歌也全部取出来了。
- HTML code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" > <Columns> <asp:TemplateField HeaderText="图标"> <ItemTemplate> <embed id="soundControl" src="upfiles/<%#DataBinder.Eval(Container.DataItem,"soundName")%>" loop=false autostart=false width=10 height=10></embed> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="播放" > <ItemStyle Wrap="False" Width="150px" /> <ItemTemplate><input id="btPlay" type="button" value="播放" onclick="ManageSoundControl('play')" /> <input id="btStop" type="button" value="停止" onclick="ManageSoundControl('stop')" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="录音文件"> <ItemStyle Wrap="False" Width="150px" /> <ItemTemplate> <asp:Label ID="lblname" Width="150px" runat="server" Text='<%# Bind("soundName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
- C# code
public string soundName = ""; protected void Page_Load(object sender, EventArgs e) { DbCommand db_src = db.GetSqlStringCommand("select soundName,tid from test"); db.ExecuteNonQuery(db_src); GridView1.DataSource = db.ExecuteDataSet(db_src).Tables[0]; GridView1.DataBind(); }
------解决方案--------------------------------------------------------
改成按照DOM关系来取,而不是按照ID
- HTML code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" > <Columns> <asp:TemplateField HeaderText="图标"> <ItemTemplate> <embed id="soundControl" src="upfiles/<%#DataBinder.Eval(Container.DataItem,"soundName")%>" loop=false autostart=false width=10 height=10></embed> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="播放" > <ItemStyle Wrap="False" Width="150px" /> <ItemTemplate><!---注意这里----><input id="btPlay" type="button" value="播放" onclick="ManageSoundControl('play',this)" /> <input id="btStop" type="button" value="停止" onclick="ManageSoundControl('stop',this)" /><!---注意这里----> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="录音文件"> <ItemStyle Wrap="False" Width="150px" /> <ItemTemplate> <asp:Label ID="lblname" Width="150px" runat="server" Text='<%# Bind("soundName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <script language="javascript" type="text/javascript"> function FindEmbed(btn) { var tr, rst; while (tr = btn.parentNode) { if (tr.tagName == 'TR') break; } return tr.getElemenetsByTagName('embed')[0]; } function ManageSoundControl(action, btn) { var soundControl = FindEmbed(btn); if (action == "play") { soundControl.play(); } if (action == "stop") { soundControl.stop(); } } </script>