views:

8

answers:

1

I have this code

<asp:RadioButtonList ID="rblSplitWeek" runat="server">  
                   <asp:ListItem selected="true">No Choice</asp:ListItem>
                    <asp:ListItem Text = "First" Value = "Session('s_price_1')"></asp:ListItem>
     <asp:ListItem Text = "Second"></asp:ListItem>
  </asp:RadioButtonList>

But keep getting an error when trying to put the session variable in

Many thanks

Jamie

+1  A: 

Unfortunately, data binding syntax (<%# %>) is not supported in this context, and literal binding syntax (<%= %> or <%: %>) do not produce the desired results when assigning values to server controls.

Here are a few alternate approaches:

1. Bind to a data source:

If you created a collection of objects containing text and value, you could easily set the DataSource, DataTextField, and DataValueField properties of the radio button list. Because the data source would be populated in code-behind, access to session variables is trivial.

For example, in markup:

<asp:RadioButtonList ID="rblSplitWeek" runat="server"
    DataTextField="Text"
    DataValueField="Value" />

And in code-behind:

public class RadioValue
{
    public string Text { get; set; }
    public string Value { get; set; }
}

// ...

var values = new RadioValue[]
{
    new RadioValue { Text = "No Choice" },
    new RadioValue { Text = "First", Value = Session["s_price_1"].ToString() },
    new RadioValue { Text = "Second" }
}
rblSplitWeek.DataSource = values;
rblSplitWeek.DataBind();

2. Assign the value from code-behind

If you declare the list item with text but without value, you can set the value from script.

For example, in markup:

<asp:RadioButtonList ID="rblSplitWeek" runat="server">  
    <asp:ListItem selected="true">No Choice</asp:ListItem>
    <asp:ListItem Text = "First" />
    <asp:ListItem Text = "Second" />
</asp:RadioButtonList>

And in code-behind:

rblSplitWeek.Items.FindByText("First").Value = Session["s_price_1"].ToString();
kbrimington