Databinding: How do I use the data as a parameter for a method or function?

N

Nathan Sokalski

I am using a SortedList as my DataSource. However, one of the things I am
using the data for is to generate the URL for the HyperLinks in my DataList.
Therefore, I need to use the result of a Server.UrlEncode() method with the
data as the parameter. The code I am using to get the data from the
SortedList (which successfully gives me the expected value) is:

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

However, when I try to modify this to be used as the parameter for the
Server.UrlEncode() (or any other method and/or function), I recieve an
error. Is there a way to do what I want and still use the DataList? Thanks.
 
B

Brock Allen

You can always make a method in your codebehind file like:

string GetUrl(object row)
{
object val = DataBinder.Eval(row, "Key");
// do more work with val then return the value to be put into your HTML
}

then in your DataBinding expression do this:

<%# GetUrl(Container.DataItem) %>

This allows your GetUrl to have as much logic as it needs without cluttering
your inline ASPX databinding syntax.
 
H

Hemang Shah

What is the error you are getting ?

Try adding .ToString(); at the end of the function.

Most likely the url encode expects a string value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top