Timer in UPdatePanel changes selectedIndex of RadioButtonList - asp.net

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, which 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.

Related

ListView in UpdatePanel not Refreshing

I have a ListView that's inside a UpdatePanel, UpdateMode = Conditional. It's a really large Listview, lots of templates, so I'm not showing details.
<asp:UpdatePanel ID="updListView" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:ListView ID="lstvScanPreview" runat="server" OnItemDataBound="lstvScanPreview_ItemDataBound">
...
</ContentTemplate>
</asp:UpdatePanel>
I also have a radiobutonlist outside of the update panel with a OnSelectedIndexChanged of
protected void rgbShowIssues_SelectedIndexChanged(object sender, EventArgs e)
{
if (rgbShowIssues.SelectedIndex == 0)
lstvScanPreview.DataSource = previewData.Data.Where(S => S.IssueType != ScanIssues.None);
else
lstvScanPreview.DataSource = previewData.Data;
updListView.Update();
}
A breakpoint set inside this method does get hit, but the listview doesn't refresh. Other controls also have events with code-behind that call updListView.Update(), and these do work. If I trigger one of these other events after I clicking on the rgbShowIssues radiobuttonlist, then when the update does occur, I can see the changes I would have expected had the UpdatePanel refreshed when expected.
Any ideas why this isn't working? Thanks.

Why do my dynamically added controls loose their values after Postback?

To ask my question I have created an aspx file containing a Button and a DataList with an SqlDataSource:
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:DataList ID="DataList1" runat="server" DataKeyField="a"
DataSourceID="SqlDataSource1" >
<ItemTemplate>
a:
<asp:Label ID="aLabel" runat="server" Text='<%# Eval("a") %>' />
<br />
b:
<asp:Label ID="bLabel" runat="server" Text='<%# Eval("b") %>' />
<br />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:probaConnectionString %>"
SelectCommand="SELECT [a], [b] FROM [PROBA_TABLE]"></asp:SqlDataSource>
In my code behind I add TextBoxes to the Items of the DataList. I add to every Item a TextBox in the Page_Load, and another TextBox in the Button Click eventhandler as well.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
foreach (DataListItem item in DataList1.Items)
{
item.Controls.Add(new TextBox());
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (DataListItem item in DataList1.Items)
{
item.Controls.Add(new TextBox());
}
}
}
}
This works fine except one thing. When I click the Button, the TextBoxes which were created in the Page_Load keep their Text value, but the TextBoxes which were created in the Button1_Click lose their Text values. My real problem is more complicated than this, but I think solving this would help me a lot.
Each control that should receive data from page ViewState should be instantiated in Init or Load event handlers, because ViewState is persisted to controls BEFORE Click, Change and the rest control events (those events are triggered when ViewState changes are detected, so ViewState must be read before Click event is fired).
So the process should look like:
OnInit (static controls get created)
Static control content is deserialized from ViewState
OnLoad (create dynamic controls, in your case textboxes that you created in last Postback)
Dynamic control content is deserialized from ViewState
Click, Change and other events are fired according to changes detected comparing POST data and ViewState data
Suggestions:
You can use hidden fields to save additional status information, and then in OnLoad you can read that info to recreate dynamically created controls.
Also, you should explicitly set ID property of your textboxes so that values can be properly persisted back, don't rely on ASP.Net.
the http by default is stateless that means after your request is processed the server keeps no data or info of the request
but the values in the form need to be persisted in special cases when there is an error
suppose you fill up a long form and then post it back to the server only to get an error message and all the filled up values are gone. wouldn't that be annoying
so what asp.net does behind the scenes that it keeps a string in the page hidden that has information about all the server controls and their ids
so when you post a form back the Page class is created and the values that are posted back and binded in the specific controls because the Page class is being created in every request the pageLoad event is run and controls created in the PageLoad are then present values corresponding to their ids are put into them unlike the controls that are being created on button click till the button_click event is run that viewstate has already been deseralized and values are filled into them

Unable to update label in update panel from user control ASP.net

My code is as follows.
<div class="table">
<asp:UpdatePanel runat="server" ID="labelPanel" UpdateMode="Conditional" >
<ContentTemplate>
<asp:Label Text="" runat="server" ID="Cost"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<uc1:ucPartsListing ID="ucPartsListing" runat="server" />
</div>
Now the usercontrol ucPartsListing itself has 2 update panels. There is an event fired from the user control to parent aspx for some conditions.
In that event, I am trying to set the label value which is present in aspx file. I am calling update manually from code-behind. Yet it doesn't work. Where am I going wrong ?
public partial class PartsEnquiry : BaseAuthPage
{
protected void Page_Load(object sender, EventArgs e)
{
ucPartsListing.OnQuotePartsItemSelect += new ascx.ucPartsListing.QuotePartsItemEventHandler(ucPartsListing_OnQuotePartsItemSelect);
}
void ucPartsListing_OnQuotePartsItemSelect(string price)
{
Cost.Text = price; //This is not working !
labelPanel.Update();
}
Set a breakpoint on your "void ucPartsListing_OnQuotePartsItemSelect(string price)" method and see if it gets hit at all.
I'm not sure what the user control is that you are using, but whatever control it is that is supposed to fire the event, try setting its AutoPostBack property to True.
I think you are out of luck with your current structure.
When the UpdatePanel inside you user control is fired in the browser, it will update the part of the page that is inside itself. You cannot update controls that are outside of the executing UpdatePanel.
Manually calling the Update() method on the outer UpdatePanel will not help since on the client it is still one of the inner UpdatePanels that is receiving the output back and updating the html tree.
To get it to work you will have to somehow trigger the outer UpdatePanel which will be able to update the Cost label.

Listbox.SelectedIndex is not changing, after some asp property changes, What is wrong?

I have a form with a dropdownbox two listboxes and two buttons on it.
I removed a "select" button as I just used the DropDownList1_SelectedIndexChanged, but the event would not fire until I used the suggestion from:
Why DropDownList.SelectedIndexChanged event does not fire?
It involved changing the AutoPostBack='true' and EnableViewState="true" properties
So now the DropDownList works but with the two listboxes, the SelectedIndex stays as -1 and does not change even when selected.
Listbox code:
<asp:ListBox ID="ListBox1" runat="server" EnableViewState="true"
Height="207px" Width="168px"></asp:ListBox>
DropDownList Code:
<asp:DropDownList ID="DropDownList1" runat="server" EnableViewState="true"
AutoPostBack="true" style="font-weight: 700; margin-left: 26px">
Button click event code:
If (ListBox1.SelectedIndex < 0) Then 'No user selected
MsgBox("Please select a user to add from the Listbox on the left.", vbCritical, "ListBox2 Validation Error")
Else
The MsgBox now always says no user has been select but there is a user select, very weird.
Any ideas? I think it has something to do with post backs, page_load or the selectedindex changed event of the listbox, it worked perfectly before I made the modifications.
The problem was the postback and that the code to populate the listboxes was in a dropbox_load subroutine. This meant that the listbox would repopulate everytime something changed on the page and hence the selecteditem index would revert back to -1.

Controls within .ascx are not displaying their new value on postback

This seems like an elementary issue but it has me stumped.
I have a main page which loads a custom control (.ascx) on page_load.
This custom control has two fields. One of them being a dropdownlist and the other being a text box. When the dropdownlist changes value it triggers a post back, some logic is executed server side and a value is generated for the textbox. I stepped through the code and the value is created and assigned correctly. However, when the page is rendered the value does not change.
If I change the textbox value to "QQQ" and trigger the postback, "QQQ" stays in the textbox so I can verify viewstate is working.
Is there any reason why the generated value is not being displayed in the form on postback. This process works fine on the initial page load.
.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
string ascxPath = #"~/_CONTROLTEMPLATES/TRC_BITS.ascx";
TRC_BITS control = Page.LoadControl(ascxPath) as TRC_BITS;
phForm.Controls.Add(control);
}
.ascx
<asp:TextBox ID="message" runat="server" TextMode="MultiLine" /><br/>
<asp:DropDownList ID="year" runat="server" AutoPostBack="true">
<asp:ListItem Text="2011">2011</asp:ListItem>
<asp:ListItem Text="2012">2012</asp:ListItem>
<asp:ListItem Text="2013">2013</asp:ListItem>
</asp:DropDownList>
.ascx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
year.SelectedValue = DateTime.Now.Year.ToString();
}
if (year.SelectedValue == 2012)
message.Text = "ABC";
else
message.Text = "XYZ";
}
Because you're adding these controls to the page dynamically, you need to assign an ID to the user control. Make sure to assign the same ID to the controls every time the page is posted back, and the fields will be repopulated from ViewState.
Also, as Shai suggested, it would be more appropriate if you loaded the controls during OnInit instead of Page_Load. The situation is a little different with user controls, but in general you want to add your dynamic content before the LoadViewState method is executed.
If you're looking for something to take the pain out of persisting dynamic content, I would suggest taking a look at the DynamicControlsPlaceHolder.
Because you are adding the controls dynamically, you need to add them during the page's oninit event.
Try it, believe me. Go for it. Yalla.

Resources