views:

244

answers:

1

I'm very new to .NET and this is driving absolutely crazy. I want to pull a value from my database and then assign it to a variable that I can pass into javascript. Printing the value is easy, but does nothing for me!

<asp:Repeater ID="Repeater3" runat="server" DataSourceID="SqlDataSource11">
<ItemTemplate>
     <asp:BoundField DataField="videofile" />

        <%#DataBinder.Eval(Container.DataItem, "videofile")%>

    <%=vidfile%>
</ItemTemplate>

videofile is the variable from the database, and vidfile is the variable I want to assign it to. I also tried this <%# vidfile = DataBinder.Eval(Container.DataItem, "videofile")%> and that gave me the output of 'False'.

help!

+2  A: 

Keep in mind that if your variable "vidfile" is declared in javascript, the server has no idea what that is.

Anything executing inside of ASP tags is strictly in the context of the server. If you'd like to assign a string to a variable, you want to get the server to print something directly into the rendered page.

Also keep in mind that the ASP tag <%= %> is a shortcut for "Response.Write()", which will render a string into the outputted page. The <%# %> syntax is for use when binding a control's property to a datasource, or utilizing the databinder methods.

Something like this might work for you:

<asp:Repeater ID="Repeater3" runat="server" DataSourceID="SqlDataSource11">
<ItemTemplate>
    <asp:BoundField DataField="videofile" />

    <script language="text/javascript">
        vidfile = '<%# DataBinder.Eval(Container.DataItem, "videofile", "{0}") %>'
    </script>
</ItemTemplate>
womp
You can also use the "simplified" databinding syntax of ASP.NET 2.0 and newer: <%# Eval("videoFile") %>
Eilon
The problem is I have to parse the value before it's ready for the javascript. I guess I could parse in the javascript, but would prefer to do it in .NET. '<%# vidfile = eval("videofile") %> produces the output of 'false' as well. Is that statement comparing the two?
Andrew Crefeld
ok, I got it! duhhh, after about 3 days of struggling. Thank-you for your help. Just using the Eval in the javascript was the way to go, and then parsed with Javascript. god, that feels good. :)
Andrew Crefeld