如果我进行PostBack检查,整个RadioButtonList会在索引更改时消失。
<asp:RadioButtonList ID="PollOptionsRadioButtonList" runat="server" AutoPostBack="True"
EnableViewState =“true”/&gt;
protected void Page_Load(object sender, EventArgs e)
{
PollOptionsRadioButtonList.SelectedIndexChanged += PollOptionsRadioButtonList_SelectedIndexChanged;
if (!Page.IsPostBack)
{
RenderPollOptions(poll);
}
}
private void RenderPollOptions(Poll poll)
{
foreach (string option in poll.Options)
{
var optionItem = new ListItem();
optionItem.Text = option;
optionItem.Value = poll.Options.IndexOf(option).ToString();
PollOptionsRadioButtonList.Items.Add(optionItem);
}
}
protected void PollOptionsRadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
{
var x = 2;
}
POLL CLASS
[EPiServerDataStore(AutomaticallyCreateStore = true, AutomaticallyRemapStore = true)]
public class Poll: IDynamicData
{
public Identity Id { get; set; }
public string Question { get; set; }
/// <summary>
/// Stores all answes to this poll
/// </summary>
public List<PollAnswer> Answers { get; set; }
public DateTime Created { get; set; }
public List<string> Options { get; set; }
public Poll()
{
Answers = new List<PollAnswer>();
Options = new List<string>();
}
}
答案 0 :(得分:3)
由于某种原因,EnableViewState默认为false!
<%@ Page EnableViewState="true" >
解决了这个问题。
答案 1 :(得分:2)
您可能还需要添加AutoPostback
属性:
PollOptionsRadioButtonList.AutoPostback = True;
答案 2 :(得分:0)
您在每个回发中添加项目,将其包装在PostBack
- 检查:
protected void Page_Load(object sender, EventArgs e)
{
PollOptionsRadioButtonList.SelectedIndexChanged += PollOptionsRadioButtonList_SelectedIndexChanged;
if(!IsPostBack)
RenderPollOptions(poll);
}
答案 3 :(得分:0)
我已经处理过您的代码,错误仅在添加项目时。所以我有 IsPostBack 方法()
尝试这样
<asp:RadioButtonList ID="PollOptionsRadioButtonList" runat="server" AutoPostBack="True"
EnableViewState="true" />
protected void Page_Load(object sender, EventArgs e)
{
PollOptionsRadioButtonList.SelectedIndexChanged += PollOptionsRadioButtonList_SelectedIndexChanged;
if (!IsPostBack)
{
RenderPollOptions(poll);
}
}
private void RenderPollOptions(Poll poll)
{
foreach (string option in poll.Options)
{
var optionItem = new ListItem();
optionItem.Text = option;
optionItem.Value = poll.Options.IndexOf(option).ToString();
PollOptionsRadioButtonList.Items.Add(optionItem);
}
}
protected void PollOptionsRadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
{
var x = 2;
}