如何将数据从控件传递到另一个控件?

时间:2014-01-30 21:58:28

标签: asp.net vb.net

我有一个asp Literal元素,我想传递给一个对象控件来设置它的数据属性,以便从数据库中提取文件名。这就是我得到的:

<asp:FormView ID="Formview1" runat="server" DataSourceID="AccessDataSource1">
<ItemTemplate>
    <object type="video/x-ms-wmv" data='<%= strFileName %>'
        width="450" height="380">
        <!-- this param is required for anyone using IE--><param name="src" value='<%= Filename %>' />
        <param name="autostart" value="false" />
        <param name="controller" value="true" />
    </object>
</ItemTemplate>
</asp:FormView>
代码背后的代码:

Partial Class VideoPlayer
Inherits System.Web.UI.Page
Protected strFileName As String

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim con As New OleDbConnection
    Dim dbProvider As String
    Dim dbSource As String
    Dim vidID As Integer = Integer.Parse(Request.QueryString("ID"))

    dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
    dbSource = "Data Source = |DataDirectory|/webvideos.mdb"

    con.ConnectionString = dbProvider & dbSource

    con.Open()
    Dim strSQL As String = "SELECT * FROM Videos WHERE ID=" & vidID
    strFileName = "videos/TrainingVideos/" & Eval("Filename")

    con.Close()
End Sub

End Class

有什么想法吗?我希望它很简单,哈哈。

3 个答案:

答案 0 :(得分:1)

我没有处理过“%pound”数据绑定标记,但您可以这样做:

<object type="video/x-ms-wmv" data='<%# "videos/TrainingVideos/" & Eval("Filename") %>'
        width="450" height="380">
        <!-- this param is required for anyone using IE-->
        <param name="src" value='<%# "videos/TrainingVideos/" & Eval("Filename") %>' />
        <param name="autostart" value="false" />
        <param name="controller" value="true" />
    </object>

如果不是 - 您可以在页面加载中将文件名+路径加载到服务器端的受保护的全局范围变量(例如strFilepath)中,或者声明一个返回文件名+路径的受保护函数(你可能想要缓存,如果你要调用它很多) - 然后这样做:

<object type="video/x-ms-wmv" data='<%= getFilepath() %>'
            width="450" height="380">
            <!-- this param is required for anyone using IE-->
            <param name="src" value='<%= getFilepath() %>' />
            <param name="autostart" value="false" />
            <param name="controller" value="true" />
        </object>

然后在后面的代码中出现类似的内容:

Protected Function getFilepath() As String    
  Return "file path/filename"
End Sub

Protected Dim strFilepath As String
Public Sub Page_Load()
    strFilePath = "file path/filename"
End Sub

以及<%= strFilepath %>

有意义吗?

答案 1 :(得分:0)

检查是否有效:

 <asp:Literal ID="VidPath1" runat="server" Text='<%# "videos/TrainingVideos/" & Eval("Filename") %>' Visible="false" />

    <object type="video/x-ms-wmv" data='<%# me.VidPath1.Text %>'
        width="450" height="380">
        <!-- this param is required for anyone using IE-->
        <param name="src" value='<%# me.VidPath1.Text %>' />
        <param name="autostart" value="false" />
        <param name="controller" value="true" />
    </object>

编辑:

测试下面的代码,它可以工作。

<asp:Literal ID="VidPath1" runat="server" Text="Test Me Literal" Visible="true" />
<asp:Label Text='<%# me.VidPath1.Text %>' runat="server" /></p>

请尝试使用标签,看看它是否合适。如果是,那么如果它不起作用,你可能需要更正路径。

<强> EDIT2:

.vb部分:

Public Class _Default
    Inherits Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        DataBind()

    End Sub
End Class

设计师部分:

<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="WebApplication2._Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

    <div class="jumbotron">
        <h1>ASP.NET</h1>
        <p class="lead"> 
            <asp:Literal ID="VidPath1" runat="server" Text="Test Me Literal" Visible="true" />
            <asp:Label Text='<%# me.VidPath1.Text %>' runat="server" /></p>
        <p><a href="http://www.asp.net" class="btn btn-primary btn-large">Learn more &raquo;</a></p>
    </div>
</asp:Content>

答案 2 :(得分:0)

假设您从该数据绑定模板中提取<object>元素并将其单独放置,您应该能够执行以下操作:

为属性(VideoPath)分配路径,然后您可以将该值绑定到任何您想要的位置(条形图可能在您的示例中的模板化控件中)

请注意,你的问题的“文字”控制部分有点模糊。你的意思是你想在文字中显示路径吗?你能否在你的例子中展示它的表现方式。欢呼声。

警告:这里生锈的VB ..希望它是正确的。

Partial Class VideoPlayer
Inherits System.Web.UI.Page
Protected strFileName As String

Protected Property VideoPath as String

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim con As New OleDbConnection
    Dim dbProvider As String
    Dim dbSource As String
    Dim vidID As Integer = Integer.Parse(Request.QueryString("ID"))

    dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
    dbSource = "Data Source = |DataDirectory|/webvideos.mdb"

    con.ConnectionString = dbProvider & dbSource

    con.Open()
    Dim strSQL As String = "SELECT * FROM Videos WHERE ID=" & vidID

    VideoPath = "videos/TrainingVideos/" & Eval("Filename")

    con.Close()
End Sub

End Class

您可以使用&lt;%= VideoPath%&gt;然后将字符串绑定到任何你想要的地方

<object type="video/x-ms-wmv" data='<%= VideoPath  %>'
    width="450" height="380">
    <!-- this param is required for anyone using IE-->
    <param name="src" value='<%= VideoPath %>' />
    <param name="autostart" value="false" />
    <param name="controller" value="true" />
</object>

Alternativley你应该能够在那些我认为没有任何问题的标签上添加runat =“server”(即对象和param元素)并在服务器端代码中为值分配正确的属性)

相关问题