开发者

Timer in UPdatePanel changes selectedIndex of RadioButtonList

开发者 https://www.devze.com 2023-01-25 04:03 出处:网络
I have a situation where I have a page in ASP.NET. In this page I have a RadioButtonList, which contains 5 solutions to a question. The RadioButtonList is feeded by an object, which has these solution

I have a situation where I have a page in ASP.NET. In this page I have a RadioButtonList, which contains 5 solutions to a question. The RadioButtonList is feeded by an object, which has these solutions.

I have a timer, which runs every second, to update loads of graphical controls. Everything works, BESIDES the RadioButtonList selection.

This is what happends:

When I select an item in the RadioButtonList and the timer tick, the selectedIndex of the RadioButtonLIst value is 0.

This means it selects the FIRST item in the list. However, IF I click an item, whi开发者_如何转开发ch has a "Yes" value in it (the value field can either have "No" or "Yes", it will stay at this item.

First of all, I have NO idea why the timer re-select my RadioButtonList selection, as any other Page_Load event does nothing. And even if that makes sense, I have no idea why it just re-selects SOME of the answers..

I have the following HTML code:

<asp:Timer ID="AssignmentTimer" runat="server" Interval="1000">
</asp:Timer>


<asp:UpdatePanel ID="FightUpdatePnl" runat="server" UpdateMode="Always" ChildrenAsTriggers="True">
   <Triggers>
      <asp:AsyncPostBackTrigger ControlID="AssignmentTimer" EventName="Tick"/>
   </Triggers>

   <ContentTemplate>
      <asp:Panel ID="AssignmentDiv" runat="server" CssClass="FightDiv">
      </asp:Panel>
   </ContentTemplate>
</asp:UpdatePanel>

I have the following code behind:

protected void Page_Load(object sender, EventArgs e)
{
    SetupPage();
}

private void SetupPage()
{
   RadioButtonList list = new RadioButtonList();

   list.DataSource = question.PossibleSolution;
   list.DataTextField = "Content";
   list.DataValueField = "IsAnswer";
   list.DataBind();

   AssignmentDiv.Controls.Add(list);
}

So, to summarize, my problem is...

When the timer ticks, the RadioButtonList re-select item 0. This is however not consistent, and sometimes it doesn't re-select. I'd rather it didn't re-select at all! :)


I'm not sure if there are not other possible causes, but you should not bind your RadioButtonList in Page_Load on postbacks. Otherwise the selection get lost.

if (!IsPostBack) {
   SetupPage();
}

Do that only when the page get first loaded and when you change its datasource(f.e in event-handler where you update the questions).

  • You are adding the RadioButtonList dynamically to the page. Ok, this have to be done on every postback(in Page_Init if possible). But it don't need to be rebound to the datasource on every Postback, because the Viewstate will save the selection on Postbacks. So split the (re)creation and adding of the RadioButtonList from the RadioButtonList.DataBind. The first have to be done on every postback and the second only on first load and when the source has changed(f.e. in an event handler).
  • Set the ID of the RadiobuttonList. Otherwise the ViewState will not be loaded after postback. The ID should be unique. If you are adding more than one RadioButtonList use a counter or such a thing.
0

精彩评论

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