ASP.NET DropDownList not retaining selected item on postback - asp.net

I have an ASP DropDownList that gets populated on the Page_Load event, after i select an item and hit a button the selected item gets cleared and the first item in the DropDownList gets selected. (The DropDownList is only populated when the page is not postback)
if (!IsPostBack)
{
List<Country> lCountries = new List<Country>();
List<CompanySchedule> lCompanySchedules = new List<CompanySchedule>();
this.Load_Countries(lCountries);
this.Load_Schedules(lCompanySchedules);
if (personnelRec == null)
{
personnelRec = new Personnel();
}
if (Request.QueryString["UA"] != null && Convert.ToInt32(Request.QueryString["UA"].ToString()) > 0)
{
userAccount.ID = Convert.ToInt32(Request.QueryString["UA"].ToString());
App_Database.Snapshift_Select_Helper.SNAPSHIFT_SELECT_PERSONNEL_APP_ACCOUNT(ref userAccount);
}
this.imgEmployeePicture.ImageUrl = "./images/Employees/nophoto.gif";
if (Request.QueryString["EI"] != null && Convert.ToInt32(Request.QueryString["EI"].ToString()) > 0)
{
this.Load_PersonnelRec(Convert.ToInt32(Request.QueryString["EI"].ToString()));
}
else
{
this.lblChangeDirectionHead.Enabled = false;
this.lblChangeDirections.Enabled = false;
this.lbSchedules.Disabled = true;
}
}

The page lifecycle does the following (plus other steps irrelevant to your question):
OnInit
Populate controls from ViewState (during postback)
Set the selected values (during postback)
Page_Load
You need to have ViewState enabled so it can populate the list before it "selects" the item. In this case, make sure you don't repopulate in Page_Load and lose the selected value. Do something like if (!IsPostback) { // Populate }
Otherwise, you have to populate the list manually in the OnInit event on every page request. Page_Load is too late in the lifecycle, so the selected item is lost.
Edit:
The DropDownList must also have valid values set (separate from the text displayed in the browser). This is done through the DataValueField property. Each value must be unique, otherwise only the first duplicate item will ever be selected. If you look at the HTML source in your browser, you should have:
<select>
<option value="unique_value1">Displayed text1</option>
<option value="unique_value2">Displayed text2</option>
</select>
The unique values are used for selecting the right item on the server side.

Are u using a Master Page? If so, remember to put the EnableViewState on true in the master page.

Related

Losing DropDownList items on post back

I have an webforms ASPX page, which has a dynamically added user control. The user control has two DropDownLists, one is in an UpdatePanel as its items depend on the selection in the first DropDownList.
The problem is that if you do not change the value of the first DropDownList the value of the second DropDownList does not get saved. If you do change the value of the first DropDownList then it works fine.
This is how it works briefly...
The first time the page loads, the previous values are set. This happens in the main ASPX page Page_Load event where the user control is dynamically added and the initial values are set through a property of the user control. The property sets the selected value of the first DropDownList, triggers the SelectedIndexChanged event which populates items in the second DropDownList with choices based on the first DropDownList selection, and then selects the previous value of the second DropDownList.
Then in the main ASPX page, the user control is dynamically added again on post back in the PreLoad event.
Viewstate is fully enabled throughout.
I have debugged and on post back the second DropDownList has no Items. Even during the UpdatePanel partial post back the items collection is empty. However, if the first DropDownList is changed then the Items collection is correct - it's only if the second DropDownList is last populated on the initial load that the problem happens.
Here's the relevant parts of the code...
Any help greatly appreciated.
ASPX page:
protected void Page_PreLoad(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
PopulateTemplateOptions();
}
}
protected void PopulateTemplateOptions(bool init = false)
{
//this is where the control is dynamically added
//If not post back then initial values are set by passing values to
//user control's TemplateOptions property
}
User control:
public string TemplateOptions
{
set
{
//this is where the initial values are set
}
}
protected void ddlEnquirySubjectDefault_SelectedIndexChanged(object sender, EventArgs e)
{
//this is where items are added to the second drop down list based on the selection in the first one.
}
(These are the only parts that are relevant, but there's another 7,000 lines of code I didn't include.)
Here are two of my ideas:
1) This line might be wrong in your code: if (Page.IsPostBack)
It must be if (!Page.IsPostBack), although I never used Page_PreLoad event so I am not sure if it is right.
2) Try to do what you want in Page_Init event instead. I use it on my projects, without problem.

Asp Checkboxlist Loses Values After PostBack

I have a checkboxlist control that I need to check the values of when a Submit button is clicked by the user onscreen(C#).
This checkboxlist is part of a user control that I am referencing within my page markup.
However, when I check the values of the checkboxlist within the code of the submit button, all of the values are gone (i.e it says there are no items at all in the checkboxlist control).
Anyone know why this would be happening? I am doing the exact same thing in code in another place with another checkboxlist user control and it works perfectly.
I don't have my exact code to hand but below is a simplified version of what I am doing.
Basically I am binding data to the Checkboxlist only when it is NOT a postback on the usedr control.
USER CONTROL WHICH CONTAINS ONLY THE CHECKBOXLIST CONTROL Page_Load()
If(!IsPostBack)
{
foreach(var item in myVals)
{
ListItem i = new ListItem();
i.Text = item.Text;
i.Value = item.Value;
i.Selected = false;
myCheckBoxListControl.Add(i);
}
}
Now I have a submit button function which checks the values in the checkboxlist...
SubmitButton_Click()
{
foreach(ListItem item in myCheckBoxListControl.Items)
{
// process each one here. The code never gets in here as there are never any items in the checkboxlist
}
}
Anyone know why the CheckboxList has lost all of its items by the time the submit button function gets executed? The checkboxlist has EnableViewState set to true.

want asp dropdownlist selection to make panel visible

I have an asp dropdownlist that I would like to make a panel visible if the selection contains a certain word- how would this be possible?
lets say you have dictionary like this
List<string> words = new List<string>();
words.Add("foo");
then onchange event of drop down list
string selectedText = ddlPanel.SelectedText;
foreach(var w in words)
{
if ( w.Contains(selectedText)
{
pnl.Visible = true;
}
}
<select onchange="if (this.options[this.selectedIndex].value.indexOf('foo') != -1) document.getElementById('panel').style.display = 'block'">
Place the panel you're looking to hide within an update panel, and use the dropdownlist change as a trigger to the updatepanel. On the update, check the dropdownlist's value and set the visibility.
Either that, or if you know the ID of the panel you can manually use javascript and bind the change event to a function that checks the values and shows/hides the panel accordingly.
Create dropDownlist with two items, "visible" and "not visible" or whatever suits you and make sure to set the autopostback property to true.
Then in vb write the following on page load:
If ddlMydropdown.Text = "visible" then
panelId.Visible = true
else
panelId.Visible = false
End If
If you code in c#, you can convert this vb code to c# over at developerfusion

How to get the latest selected value from a checkbox list?

I am currently facing a problem. How to get the latest selected value from a asp.net checkbox list?
From looping through the Items of a checkbox list, I can get the highest selected index and its value, but it is not expected that the user will select the checkbox sequentially from lower to higher index. So, how to handle that?
Is there any event capturing system that will help me to identify the exact list item which generates the event?
If I understood it right, this is the code I'd use:
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
int lastSelectedIndex = 0;
string lastSelectedValue = string.Empty;
foreach (ListItem listitem in CheckBoxList1.Items)
{
if (listitem.Selected)
{
int thisIndex = CheckBoxList1.Items.IndexOf(listitem);
if (lastSelectedIndex < thisIndex)
{
lastSelectedIndex = thisIndex;
lastSelectedValue = listitem.Value;
}
}
}
}
Is there any event capturing system that will help me to identify the exact list item which generates the event?
You use the event CheckBoxList1_SelectedIndexChanged of the CheckBoxList. When a CheckBox of the list is clicked this event is called and then you can check whatever condition you want.
Edit:
The following code allows you to get the last checkbox index that the user selected. With this data you can get the last selected value by the user.
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
string value = string.Empty;
string result = Request.Form["__EVENTTARGET"];
string[] checkedBox = result.Split('$'); ;
int index = int.Parse(checkedBox[checkedBox.Length - 1]);
if (CheckBoxList1.Items[index].Selected)
{
value = CheckBoxList1.Items[index].Value;
}
else
{
}
}
Below is the code which gives you the Latest selected CheckBoxList Item.
string result = Request.Form["__EVENTTARGET"];
string [] checkedBox = result.Split('$'); ;
int index = int.Parse(checkedBox[checkedBox.Length - 1]);
if (cbYears.Items[index].Selected)
{
//your logic
}
else
{
//your logic
}
Hope this helps.
Don't know about you, but as a user i wouldn't want the page to post back every time a checkbox item was checked.
This is the solution i would go with (jQuery):
Declare a server-side hidden field on your form:
<asp:HiddenField ID="HiddenField1" runat="server" EnableViewState="true" />
Then wire up client-side event handlers for the checkboxes to store checkbox clicked:
$('.someclassforyourcheckboxes').click(function() {
$('#HiddenField1').val($(this).attr('id'));
This is a lightweight mechanism for storing the ID of the "latest" checkbox clicked. And you won't have to set autopostback=true for the checkboxes and do an unecessary postback.
You dont HAVE to use jQuery - you can use regular Javascript, but, why do more work? =)
Then when you actually do the postback (on a submit button click i assume), just check the value of the hidden field.
Unless of course you WANT to postback on every checkbox click, but i can't envision a scenario in which you'd want this (maybe you're using UpdatePanel).
EDIT
The HTML of a checkbox list looks like this:
<input type="checkbox" name="vehicle" value="Bike" /> I have a bike
So, you can access three things:
Vehicle = $(this).attr('name');
Bike = $(this).attr('value');
I have a bike = $(this).html();
If you're trying to access the databound value, try the second technique.
Give that a try.

Have to click button twice in asp.net (after autopostback textbox)

On a page I have:
<asp:TextBox runat="server" ID="EmailTextBox" AutoPostBack="true" OnTextChanged="EmailTextBox_Changed" />
<asp:Button runat="server" ID="SearchButton" OnClick="AddButton_Click" Text="add" />
In EmailTextBox_Changed, it counts up how many emails can be found, before running the search.
The problem is, when you type something in EmailTextBox, and click on the button, you have to click twice to get the actual results up. This is because the first click is doing the "AutoPostBack" part from the text box, and then you have to click again to make the actual click postback to happen.
Without removing the "AutoPostBack=true", how can I stop it needing two clicks in these circumstances?
I was looking for an answer to this issue as well. I ended up removing all autopostback=true and doing all the actions with JavaScript, same as you.
However, one of the things I experimented with before the JavaScript was something to maintain control focus after a postback. I noticed the hidden field I used to store the name of the control that had the last focus DID have the name of the search button (mine is a save button). So, while I'm still not sure how to get the 'search' function to fire 'automatically' like it should, which is basically to chain the postback events from both the textbox AND the button together one after another, I CAN know that the user clicked that save button before the postback happened (or tried to).
So, what you have on postback is your textbox event firing, and then the Page_Load method, or whatever page cycle method you want to use, where you can check to see what the last control to have focus was. With this, there are several ways you could implement a work around.
Off hand, you could add code in every event that fires from a control autopostback, like the textbox and the search button, to check the name of the focus control. If the control that had focus last is NOT the control's autopostback function we are running, we can set a page level bool called 'Run_Controls_Method' to TRUE, else, set it to false. This way we know we should run the control that had last focus postback method.
On page load, you could do something like:
if (Run_Controls_Method && hdfFocusControl.Value != "")
{
switch(hdfFocusControl.Value)
{
case "btnSearch":
btnSearch_OnClick(null, null);
break;
case etc.
}
}
The way I implement the hdfHasFocus is:
HTML:
<input id="hdfHasFocus" runat="server" type="hidden" />
HTML code behind:
protected void Page_PreRender(object sender,EventArgs e)
{
if (IsPostBack != true)
{
//Add the OnFocus event to all appropriate controls on the panel1 panel.
ControlManager.AddOnFocus(this.Controls,hdfHasFocus,true);
//other code...
}
ControlManager.SetFocus(this.Controls,hdfHasFocus.Value,true);
}
ControlManager.cs related code:
/// <summary>
/// Adds the onfocus event to the UI controls on the controls in the passed in control list.
/// </summary>
/// <param name="controls">The list of controls to apply this event.</param>
/// <param name="saveControl">The control whose .value will be set to the control.ID of the control which had focus before postback.</param>
/// <param name="Recurse">Should this method apply onfocus recursively to all child controls?</param>
public static void AddOnFocus(ControlCollection controls, Control saveControl, bool Recurse)
{
foreach (Control control in controls)
{
//To make the .Add a bit easier to see/read.
string action = "";
//Only apply this change to valid control types.
if ((control is Button) ||
(control is DropDownList) ||
(control is ListBox) ||
(control is TextBox) ||
(control is RadDateInput) ||
(control is RadDatePicker) ||
(control is RadNumericTextBox))
{
//This version ignores errors. This results in a 'worse case' scenario of having the hdfHasFocus field not getting a
// value but also avoids bothering the user with an error. So the user would call with a tweak request instead of
// and error complaint.
action = "try{document.getElementById(\"" + saveControl.ClientID + "\").value=\"" + control.ClientID + "\"} catch(e) {}";
//Now, add the 'onfocus' attribute and the built action string.
(control as WebControl).Attributes.Add("onfocus", action);
}
//The 'onfocus' event doesn't seem to work for checkbox...use below.
if (control is CheckBox)
{
//This version ignores errors. This results in a 'worse case' scenario of having the hdfHasFocus field not getting a
// value but also avoids bothering the user with an error. So the user would call with a tweak request instead of
// and error complaint.
action = "try{document.getElementById(\"" + saveControl.ClientID + "\").value=\"" + control.ClientID + "\"} catch(e) {}";
//In case there is already an attribute here for 'onclick' then we will simply try to add to it.
action = action + (control as WebControl).Attributes["onclick"];
//Now, add the event attribute and the built action string.
(control as WebControl).Attributes.Add("onclick", action);
}
//You don't seem to be able to easily work the calendar button wiht the keyboard, and it seems made for
// mouse interaction, so lets set the tab index to -1 to avoid focus with tab.
if (control is CalendarPopupButton)
{
(control as WebControl).Attributes.Add("tabindex", "-1");
}
//We also want to avoid user tab to the up and down spinner buttons on any RadNumericTextBox controls.
if (control is RadNumericTextBox)
{
(control as RadNumericTextBox).ButtonDownContainer.Attributes.Add("tabindex", "-1");
(control as RadNumericTextBox).ButtonUpContainer.Attributes.Add("tabindex", "-1");
}
//Recursively call this method if the control in question has children controls and we are told to recurse.
if ((Recurse) && (control.HasControls()))
{
AddOnFocus(control.Controls, saveControl, Recurse);
}
}
}
/// <summary>
/// Searches the ControlCollection passed in for a match on the ID name string passed in and sets focus on that control if it is found.
/// </summary>
/// <param name="controls">The collection of controls to search.</param>
/// <param name="FocusToID">The ID of the control to set focus on.</param>
/// <param name="recurse">Recursively search sub-controls in the passed in control collection?</param>
/// <returns>True means keep processing the control list. False means stop processing the control list.</returns>
public static bool SetFocus(ControlCollection controls, string FocusToID, bool recurse)
{
//Return if no control ID to work with.
if (string.IsNullOrEmpty(FocusToID) == true)
{ return false; }
//If we get here and don't have controls, return and continue the other controls if applicable.
if (controls.Count <= 0)
{ return true; }
foreach (Control control in controls)
{
//If this is the control we need AND it is Enabled, set focus on it.
if (((control is GridTableRow) != true) && //GridTableRow.ClientID throws an error. We don't set focus on a 'Row' anyway.
(control.ClientID == FocusToID) &&
((control as WebControl).Enabled))
{
control.Focus();
//return to caller. If we were recursing then we can stop now.
return false;
}
else
{
//Otherwise, see if this control has children controls to process, if we are told to recurse.
if ((recurse) && (control.HasControls()))
{
bool _continue = SetFocus(control.Controls, FocusToID, recurse);
//If the recursive call sends back false, that means stop.
if (_continue != true)
{ return _continue; }
}
}
}
//We are done processing all the controls in the list we were given...
// If we get here, then return True to the caller. If this was a recursive call, then
// the SetFocus in the call stack above will be told to continue looking since we
// didn't find the control in question in the list we were given.
return true;
}
In fact, you don't have to click on the button to make the first event happen.
Just 'leave' the textbox, i.e. with 'tabbing' out of it to make the AutoPostBack happen.
If you want to do both in a single postback just remove the Button and do the things you do in AddButton_Click also in the Textbox_Change event.
Making it a client side check was the solution to this...there doesn't seem to be a way to prevent it otherwise
Write below code in Page_Load event to prevent twice click
BtnSaveAndPrint.Attributes.Add("onclick", "return confirm('Are you sure you Want to Save & Print?');")
You could avoid this by not doing it server side and using Javascript. You also didn't post your page load event. Are you checking if it post back or not ?
Another way you could do this is the event that happens on the click of the button can be called from the TextChanged event and get rid of the button all together.
I had the same problem, I decided to move the click event code to the page load event and execute it in case of postback. And not to use a click event at all.
protected void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack)
{
// put code here
}
}
instead of :
public void ButtonClick(object sender, EventArgs e)
{
//...
}

Resources