Changing ViewState after event requires additional refresh - c#

I have an Asp:ListBox with OnSelectedIndexChange="OnSIC" and AutoPostback="true".
I want to keep track of the selected index after the postback to show some data that depends on the index.
My idea is to save the selected index in ViewState.
protected void OnSIC(object sender, EventArgs e)
{
ViewState["idx"] = listBox.SelectedIndex;
}
However, after the page is refreshed, Page_Load does not yet have the updated ViewState["idx"] value. An additional refresh is required to make Page_Load get the new index value.
I understand that postback events happen after Page_Load, so I tried moving the code that gets ViewState["idx"] in Page_PreRender, but I get the same behavior.
How can I correctly store the listbox's selected index value, after a OnSelectedIndexChanged event is fired, and use it on the very next page refresh (that happens because of the event postback)?

Related

Determine whether the selected index was changed or an autopostback event was triggered, in sections of the code-behind other than the event handler

I have an ASP DropDownList control with the AutoPostBack property set to true. When the user changes the selection, the form posts back as expected. I want to know how to determine, in the code-behind, whether the page posted back for this specific reason.
I know that I can define an event handler like this...
protected void MyDropDownList_SelectedIndexChanged(object sender, EventArgs e) {
// Run some code if the selection for the "MyDropDownList" control was changed
}
...but what I want to know is how to check whether the form posted back because the selected index was changed outside the event handler.
Specifically, in the Page_Load() method, I have an if (IsPostback) {} section, and I want this section not to execute if the postback was caused by changing the selection in the DropDownList. So, in pseudocode, I want something like:
if (IsPostback && (! <DropDownList's selection was changed, causing an autopostback>)) {
I tried defining a global boolean variable and setting it to true in an event handler, then checking it in Page_Load(), like this:
public partial class MyWebApp : System.Web.UI.Page {
[...]
static bool selectedIndexChanged = false;
[...]
protected void DomainDropDownList_SelectedIndexChanged(object sender, EventArgs e) {
selectedIndexChanged = true; // Set this flag to true if selected index was changed
}
[...]
protected void Page_Load(object sender, EventArgs e) {
[...]
if (IsPostBack && selectedIndexChanged == false) {
[...]
}
[...]
This didn't work for a reason I presume experienced ASP.NET developers will easily spot: the event handler executes after Page_Load(), regardless of the order of the code.
I also tried to see if the control's selectedIndexChanged event can be used as a boolean condition to determine if the event triggered, like this
if (IsPostBack && !MyDropDownList.SelectedIndexChanged) {
but Visual Studio gives me the following error:
The event 'System.Web.UI.WebControls.ListControl.SelectedIndexChanged' can only appear on the left hand side of += or -="
A search on the error message led to this answer, but that doesn't seem to help because it relies on the event handler, which executes after Page_Load().
In my particular use case, where there is only one DropDownList and only one other way to submit the form (the submit button), it would be equally effective to check whether the selected index was changed, whether an AutoPostBack was triggered, or whether the submit button was clicked, but I'd also like to know how to do this in a broader range of scenarios, for example if there are multiple AutoPostBack controls and/or multiple ways to submit the form other than AutoPostBack.
So, my question breaks down as follows (though some of these may be essentially the same question, depending on what the answer is):
Is there a way to determine in general whether an AutoPostBack was triggered, as opposed to the form posting back for any other reason, such as clicking a button?
Is there a way to determine if a specific control's AutoPostBack was triggered (i.e. if there are multiple controls with AutoPostBack true, can it be determined which control caused the AutoPostBack)?
Is it possible to check in the Page_Load() method or any other code that executes before the SelectedIndexChanged event handler whether a DropDownList's selected index was changed?
If there's a better way to achieve what I'm trying to accomplish in this particular case, I'm open to suggestions, but I'd still like to know the answer(s) to the above.
During Page_Load inspect Page.Request.Form["__EVENTTARGET"]. This will contain an identifier representing the control that caused the post back. From this you should be able to determine whether the post back was caused by the control you're interested in.
if (IsPostBack && Request.Form["__EVENTTARGET"] != "<control ID>") {
...
}
Is it possible to check in the Page_Load() method or any other code that executes before the SelectedIndexChanged event handler whether a DropDownList's selected index was changed?
Not without resorting to a custom technique. The SelectedIndexChanged event fires too late during the page event lifecycle to be useful to your scenario. One option is to store the DropDownList.SelectedIndex into the Page.ViewState collection during Page.OnPreRender and then comparing this value to the new DropDownList.SelectedIndex on the post back during Page_Load.

ASP.NET listbox selectedValue in PageLoad

I have a problem with listbox. I would like to fill it with data and select Value (listbox.SelctedValue), using postback, after textbox is filled and Page.Validate() is fired. I try to use it in Page_Load. Everything works fine until I dont mark another user. It goes back to the the first one. I know its because I mark the first one again and again it in Page_Load, but how can I mark user after postback in other place? I cant use any buttons.
To be more clear, I have one text box, which causes postback after user put text there. After that I would like to check if Page.Isvalid and is yes, add that user to listbox (which also causes postbacks) and mark him. Without any buttons. How can I do it only once, using autopostback, not every PageLoad ?
Try this
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
if (Page.IsValid)
{
//Mylistbox.SelctedValue = set Your Selected Value
}
}
}

How to retain checkboxlist selected values on button click C# asp.net

Here is my button click code:
protected void ImageButton_Run_Click(object sender, ImageClickEventArgs e)
{
if (selectedFilter == "AgentID")
{
List<string> selectedValues = CheckBoxList_Options.Items.Cast<ListItem>().Where(li => li.Selected).Select(li => li.Value).ToList();
var selectedItems = CheckBoxList_Options.Items.Cast<ListItem>().Where(x => x.Selected);
}
Response.Redirect("AgentSkillMapReport.aspx");
}
Selected values are always empty even if I have selected checkboxs in chckboxlist. I think values are cleared on postback. How to retain its values. Help me out friends. Thanks in advance :)
You could use session variable
session["checkBox_selectedItems"]= CheckBoxList_Options.Items.Cast().Where(x => x.Selected);
keep in mind, that memory is a limited resource, and it depends on the number of connections at a given time. So, these kind of variables are used to store small amount of data... They are an object, wich means that can store everything you want
I have binded the data source for checkbox in my button click method. So every time i click the button it gets refreshed on postback and i am losing the selected values in my checkboxlist. I have moved my code to page_load event and i wrapped the code unded if(!ispostback) and everything works fine now. My selections are retained after button click.

Accessing RepeaterItem Controls in an EventHandler

I have a page with a repeater in it. I'm writing an event handler so that when the user clicks my WebControl button, the event handler for said button iterates through the items in the repeater using FindControl, then uses some of the controls' values. It seems though, that after the page is loaded, the repeater items populate, but when the button is clicked to post this back, as I iterate through the repeater items, I'm seeing that they're all empty. I don't completely understand the sequencing, but I'm assuming it's because my iteration code is trying to access RepeaterItems that haven't been set yet.
The repeater code is in my OnLoad method. Outside of that, I have my event handler trying to iterate through those items after being clicked. This is essentially what I was trying to do:
protected void MyButton_Click(object sender, EventArgs e)
{
foreach(RepeaterItem item in MyRepeater.Items)
{
MyLabel = (Label)item.FindControl("MyLabel");
}
}
The button is located in the FooterTemplate of the repeater.
<asp:Button runat="server" OnClick="SubmitChecklist_Click" cssclass="BlueSubmit" id="SubmitChecklist" text="Submit" />
Thanks in advance.
Edit: To clarify, the exact error I'm getting is NullReferenceException, when I try to do something, for instance, Response.Write(MyLabel.Text)
Edit: After looking into it more today, this is what I understand to be happening: The repeater is databound on postback. When I then make selections from the generated dropdownlists and hit my button, it posts back again. At this point, the repeater is databound again to it's initial values. So, if I must postback in order to get the users' selections, how can I go about this in the button's eventhandler so that I can get the selected values before that repeater gets databound again?
THe problem, it sounds like, is that you may be binding the data to your repeater on load, but not first checking to make sure it isnt a post back.
example:
You request the page. On Load Fires. You bind the data to the repeater.
You maniupulate the data in the reapter then click your button
The page refreshes with the postback, firing the onload event. The data is rebound to your repeater and all previous data entered has been nullified.
the onclick event is triggered and your code tries to retrieve values that no longer exist.
Make sure your databinding code in your onLoad event is nested within an postback check
if (!Page.IsPostBack)
{
Repeater.DataSource = Datatable;
Repeater.DataBind();
}
I've seen the same thing. I don't understand why, but the data doesn't actually get bound until after all events have fired. I ended up making my data source available at the class level and then indexing.
private DataTable myTable;
protected void Page_Load(object sender, EventArgs e)
{
//populate dataTable
if (!IsPostBack)
{
//databind to repeater
}
}
protected void Submit_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in repeater1.Items)
{
DataRow row = myTable.Rows[item.ItemIndex];
}
}
Ideal? Certainly not but it works.
Instead of relying on the IsPostBack in my OnLoad, I just seperated all of the different states by putting the databinding of the repeater inside of an event handler after the user selects the first option, rather than relying on the IsPostBack of OnLoad. It was a bit convoluted, but I think I'm doing it the right way this time.

ViewState, UserControl and IsPostback

Basically, I have a drop down list and a dynamically added user control. The user control loads a grid view depending on the choice that was made in the drop down list. The drop down list is not part of the user control.
Now, the question is, how do i simulate (isControlPostback = false) every time the user changes the selection in the drop down list? It looks like ViewState remembers the control.
Inside my user control I have:
protected bool IsUserControlPostBack
{
get
{
return this.ViewState["IsUserControlPostBack"] != null;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsUserControlPostBack)
{
ViewState.Add("IsUserControlPostBack", true);
//load stuff in the grid view and bind it
}
}
When the user changes the selection on the drop down list, i have a javascript confirm box, and the page posts back. So OnSelectedIndexChanged event for drop down list doesn't get triggered. I would like to remove to do something like this every time the selected index changes:
ViewState.Remove("IsUserControlPostBack");
You can make changes to the control in prerender event. When this event is fired all other actions are made.
Or you can do public property in user control and when setting required to value react on appropriately.
The ViewState you access in your user control is not the same one you access on the page. If you need your page to communicate with your user control, I suggest you add a public method on your user control for this purpose.
If, for some reason, you prefer
something similar to your ViewState
approach, you can try Context.Items.
Note that Context.Items is not
preserved between requests.
Add the control to the page sometime before OnLoad. E.g. OnInit. Between OnInit and OnLoad, the viewstate is loaded and postback events are run.
For anyone who is interested to know the answer:
I ended up implementing a public property inside user control and load the control inside the server drop down list SelectedIndexChanged event rather than OnInit. This eliminated the need for explicit Viewstate use.

Categories

Resources