views:

313

answers:

3

I have a problem and i would like to learn the correct way to solve this.

I have a Data Objeckt

class LinkHolder {
    public string Text;
    public string Link;
}

I would like to present to the user a RadioButton list that uses the LinkHolder.Text value as descriptive text. Then on the postback, i would like to do a

Server.Transfer( LinkHolder.Link )

on the corresponding Link.

I am unsure what is the best/most correct way to do this. Any hints would be appreciated.

A: 

Your method should work. I think you should use accessors in your class though

class LinkHolder {
    public string Text { get; set;}
    public string Link { get; set;}
}

Bind your RadioButtonList to a f.ex. List<LinkHolder>

Why would you use a radiobuttonlist instead of just listing out your Links as Hyperlinks instead of having to use Server.Transfer?

TT
A: 

the Class LinkHolder was just a short-form example. I do have accessors in the real class. The radiobuttons was a design choice, so im going with them.

In my tests, it would print the name of the class, and not the Text Property, when i used List<LinkHolder> as a DataSource. I don't know how/if you can specify a property the RadioButtonList should print.

It is also my understanding that if I use List<LinkHolder> as a Datasource I will not get a LinkHolder object back, when i ask for SelectedItem. I will instead get a ListItem.

Any good workaround to that?

Jesper Jensen
+1  A: 

You need to set DataTextField and DataValueField on your RadioButtonList. Then the correct values should show up.

You can try to cast the selectedItem into a LinkHolder.

TT