...

View Full Version : Please help me define a variable in the HTML of my .NET page



AlexanderBlade
11-29-2006, 08:23 PM
Hi everyone,

I am trying to retrieve a value from a datalist control and store it in a variable. The tag that I am using to get the value is in the ItemTemplate area of my control. It looks like this:

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

My question: is there a way to create a variable that I can use in my HTML area of my .Net program that can then be used else ware on the page. My goal is to send the variable to a javascript after loading it with a value from the DataItem.

Your help would be greatly appreciated.

nikkiH
11-29-2006, 09:56 PM
You're only expecting one row, not anything repeated?
Or ...?

This sounds more like a classic ASP way of looking at things than a .NET way of looking at things. Can you be more clear about what you're trying to do?

onItemDataBound fires for repeaters.
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.onitemdatabound.aspx

AlexanderBlade
11-30-2006, 08:42 PM
The control that Iím using has some characteristics like a repeater control but it is a DataList control. It does repeat rows like a repeater control but what I want to do is this. When my user clicks on one image, I want to send the path or name of that image to a JavaScript. Inside the JavaScript, I should be able to have the image loaded in JavaScript at this point. Iím getting my images, name and path from my DataList control

This is the line of code that Iím using to send the name as a parameter to javascript but it is sent as a string that looks like thisÖ

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

instead of the actual name of the image. I am hoping that I can dump the name of the image in a variable and send the variable instead. Hereís the line of code that Iím using to do this.

<asp:HyperLink id=Hyperlink1 runat="server" NavigateUrl='JavaScript:openPopup("largerImage.aspx?name=<%# DataBinder.Eval(Container.DataItem, \"Name\")%>")' ImageUrl='<%# "../images/" + DataBinder.Eval(Container.DataItem, "ImagePath") %>'>
</asp:HyperLink>

nikkiH
11-30-2006, 11:31 PM
That looks close, but I think you have your quotes a bit off.
Try this.



<asp:HyperLink
id="Hyperlink1"
runat="server"
NavigateUrl="javascript:OpenPopup('largerImage.aspx?name="+<%# DataBinder.Eval(Container.DataItem, "Name")%>+"');"
ImageUrl="../images/" + <%# DataBinder.Eval(Container.DataItem, "ImagePath") %>
/ >


Assuming OpenPopup is a defined javascript function.

AlexanderBlade
12-01-2006, 09:46 PM
Hi I got a "Server tag not well formed" error. I am working in VB.NET so I took our the ";" that was in one of the tags. Here is what i have now and I'm still getting the error. I also tried removing the slash that's at the end of the DataBinder.Eval(). Still the same error. Hrere's what I have now:

<asp:HyperLink id="Hyperlink1"runat="server" NavigateUrl="javascript:OpenPopup('largerImage.aspx?name="+<%# DataBinder.Eval(Container.DataItem, "Name")%>+"') "ImageUrl="../images/" + <%# DataBinder.Eval(Container.DataItem, "ImagePath") %>/></asp:HyperLink>

I've tried a number of things like replacing the quotes with &quote; but still the same error.

I really appreciate you help,

Alex

nikkiH
12-01-2006, 10:29 PM
Ditch the end tag, I used an end slash. Or remove the end slash.

/>
OR
</asp:whatever>

Not both.


<asp:HyperLink
id="Hyperlink1"
runat="server"
NavigateUrl="javascript:OpenPopup('largerImage.aspx?name="+<%# DataBinder.Eval(Container.DataItem, "Name")%>+"');"
ImageUrl="../images/" + <%# DataBinder.Eval(Container.DataItem, "ImagePath") %>
/>
OR

<asp:HyperLink
id="Hyperlink1"
runat="server"
NavigateUrl="javascript:OpenPopup('largerImage.aspx?name="+<%# DataBinder.Eval(Container.DataItem, "Name")%>+"');"
ImageUrl="../images/" + <%# DataBinder.Eval(Container.DataItem, "ImagePath") %>
></asp:HyperLink>


You want that last semi-colon, it's for the javascript, not the asp code.

AlexanderBlade
12-02-2006, 05:48 AM
Hi nikkiH,

I'm sorry but I'm still getting the same error. I'm using Visual Studio Version 1.1. Do you think that this has anything to do with the problems with qoutes? I've tried putting single quotes around the tags but still same error.

nikkiH
12-04-2006, 03:43 PM
You can always fall back to a normal anchor. That's all a hyperlink really is, unless you needed to script it at runtime.

I think I got peeved too, because this is from my project LOL


<ItemTemplate>
<a href='ReportListing.aspx?OfficeCode=<%# DataBinder.Eval(Container.DataItem, "OFFICE_CODE") %>'><%# DataBinder.Eval(Container.DataItem, "OFFICE_CODE") %></a>
</ItemTemplate>


Another, with a script call.

<ItemTemplate>
<a href="#"
onclick='openEditWin(<%# DataBinder.Eval(Container.DataItem, "REPORT_ID") %>); return false;'>[edit]</a>
</ItemTemplate>

AlexanderBlade
12-04-2006, 08:15 PM
Hi nikkiH, thanks for your help. I guess I will have to resort to a normal href. I was trying to have my user simply click on the image and show them an enlarged version of the image. Iíve figured out a way to navigate to a new page with this method but not to a new window. Iíll keep trying. If it fails Iíll add a href below the image with the line ďclick here for a larger imageĒ.

nikkiH
12-05-2006, 04:57 PM
A normal href is really just a link control, to the asp page.

Don't kill yourself over it, unless you just want to play just for grins or for your own satisfaction.
You can give a link an id and a runat=server and it ends up rendered as a server control. (HtmlAnchor)

For example, you can have this control:
<a href="#" runat="server" ID="linkTest" />

And in page_load, this is perfectly fine and works.
linkTest.InnerText = "Page_Load fired ";



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum