Joseph
Joseph

Reputation: 676

How to pass data from a control to another control?

I have a asp Literal element that I want to pass to an object control to set its data property so that it pulls a filename from a database. Here's what I got:

<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>

code behind:

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

Any idea? I hope it is simple, lol.

Upvotes: 1

Views: 264

Answers (3)

jwwishart
jwwishart

Reputation: 2895

Assuming that you extract the <object> element out of that data bound template and put it just by itself you should be able to do as follows:

The property (VideoPath) is assigned the path, then you can bind that value anywhere you want (bar maybe in templated controls like you have in your example)

Please note that the 'Literal' control part of you question is a little vague. Do you mean you want to display the path in the literal also? Can you please show in your example how it would be represented. Cheers.

Warning: Rusty VB here.. hopefully it is correct.

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

You can use <%= VideoPath %> then to bind the string wherever you want

<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 you should be able to add runat="server" on those tags I would think without any issue (i.e. both the object and param elements) and assign the correct attributes the value in server side code)

Upvotes: 0

mikey
mikey

Reputation: 5160

I've not dealt with the "percent pound" databind tags, but you may be able to do this:

<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>

If not - you can load your filename + path into a protected global-scope variable (e.g. strFilepath) on the server side in the page load, or declare a protected function that returns the filename + path (which you may want to cache if you are going to be calling it a lot) - then do it like so:

<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>

and then something like this in the code behind:

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

or

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

along with <%= strFilepath %>

make sense?

Upvotes: 1

Ashish Charan
Ashish Charan

Reputation: 2387

Check if this works:

 <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>

Edit:

Tested the below code, it works.

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

Please try with a label, see if it gets proper value. If yes, then possibly you need to correct the path if it doesn't work.

Edit2:

The .vb part:

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

The designer part:

<%@ 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>

Upvotes: 0

Related Questions