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

Discussion in 'ASP .Net Datagrid Control' started by Nathan Sokalski, Jun 13, 2005.

  1. 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.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
     
    Nathan Sokalski, Jun 13, 2005
    #1
    1. Advertising

  2. Nathan Sokalski

    Brock Allen Guest

    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.

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > 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.
    >
     
    Brock Allen, Jun 13, 2005
    #2
    1. Advertising

  3. Nathan Sokalski

    Hemang Shah Guest

    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.

    "Nathan Sokalski" <> wrote in message
    news:...
    >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.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
     
    Hemang Shah, Jun 13, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathan Sokalski
    Replies:
    2
    Views:
    663
    Hemang Shah
    Jun 13, 2005
  2. jobs
    Replies:
    0
    Views:
    1,774
  3. Mister B
    Replies:
    8
    Views:
    604
    Nick Keighley
    Aug 26, 2010
  4. Earl Teigrob
    Replies:
    1
    Views:
    446
    Shravan
    Mar 1, 2004
  5. AzamSharp
    Replies:
    2
    Views:
    203
Loading...

Share This Page