开发者

can i use a session variable as the text of a listitem in a radiobutton list?

开发者 https://www.devze.com 2023-01-15 06:13 出处:网络
I have this code <asp:RadioButtonList ID=\"rblSplitWeek\" runat=\"server\"> <asp:ListItem selected=\"true\">No Choice</asp:ListItem>

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开发者_如何学Go_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


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();


I know this is way late, but another alternative is to load the RadioButtonList's SelectedIndex property in the PageLoad() event. Then you don't have to have a small RadioValue class - which is fine of course. Here is how I did it the last time... and yes I have used the RadioValue class way too.... but here it is using your radiobuttonlist without the RadioValue class:

    protected void Page_Load(object sender, EventArgs e)
    {
        //Reload the last radio button selected.
        if (Session["rblSplitWeekIndex"] != null)
        {
            rblSplitWeek.SelectedIndex = ((int)Session["rblSplitWeekIndex"]);
        }

...

I save my radio button list selected index in a MasterPage which has a search scope radio button list and a search text box. So since a MasterPage killed my radio button list (and also the text which is not shown here), I had to load it when needed. Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消