如何使用javascript从GridView获取URL?

时间:2011-12-10 06:34:13

标签: c# javascript asp.net

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="ID" DataSourceID="SqlDataSourceVideo" >
    <Columns>
        <asp:BoundField DataField="VideoUrl" HeaderText="VideoUrl" 
            SortExpression="VideoUrl" />
        <asp:BoundField DataField="ID" HeaderText="ID" 
            SortExpression="ID" InsertVisible="False" ReadOnly="True" />
        <asp:BoundField DataField="Video_Name" HeaderText="Video_Name" 
            SortExpression="Video_Name" /> 
        <asp:CommandField ShowDeleteButton="True" />
    <asp:TemplateField>
        <ItemTemplate>
        <asp:Button ID="ButtonPlay" runat="server" CommandName="Play"
            CommandArgument='<%# DataBinder.Eval(Container.DataItem,"VideoUrl") %>'
            Text="Play" OnClientClick="playVideo()"></asp:Button>

播放网址的JavaScript代码:

<script type="text/javascript" language="javascript">
    function playAudio(URL){
        if (URL != "")
        {
            document.Player.filename = URL;
            document.getElementsByName("mediaPlayer").src=URL; 
            document.getElementsByName("mediaPlayer").play();
            document.Player.showcontrols = true;
            document.Player.height = 40;
            document.Player.play();    
        }
    }
</script>

有谁能告诉我如何从gridview获取URL值?提前谢谢。

3 个答案:

答案 0 :(得分:1)

试试这个,

<asp:Button ID="ButtonPlay" runat="server" CommandName="Play" 
     CommandArgument='<%# DataBinder.Eval(Container.DataItem,"VideoUrl") %>' 
     Text="Play" 
     OnClientClick='<%# DataBinder.Eval(Container.DataItem,"VideoUrl", "playVideo('{0}')") %>'>
</asp:Button>

答案 1 :(得分:1)

我会将你的BoundField更改为TemplateField

<asp:TemplateField SortExpression="VideoUrl" HeaderText="Video Url">
    <span class='videoUrl<%# DataBinder.Eval(Container, "RowIndex") %>'><%# Eval("VideoUrl") %></span>
</asp:TemplateField>

然后,从第三行获取网址:

var span = document.getElementsByClassName("videoUrl3")[0];
var url = span.textContent ? span.textContent : span.innerHTML;

答案 2 :(得分:0)

OnClientClick='<%# EVAL("VideoUrl", "return playAudio({0})") %>'/> 

在你的按钮中使用它。希望它有所帮助。