I have a dropdown list which is populated on page_load using a linq query.
If I change the value in the dropdown then click a button to run an update query on the record, the original value is still there, if I step through in debug mode I can see the selected value is not changing at all
Here is how Im binding the data to dropdown
dlBookingRef.DataSource = d.BookingRef();
dlBookingRef.DataMember = "booking";
dlBookingRef.DataBind();
and here is the line in the function which gets the data from the form
item.booking_ref = dlBookingRef.SelectedValue;
Any idea why it's retaining its original value?
thanks
Put the binding code in if(!IsPostBack), it looks like that in button event as page load gets called due to that your dropdown list gets reset, so bind the dropdown only when page is not posted back:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
dlBookingRef.DataSource = d.BookingRef();
dlBookingRef.DataMember = "booking";
dlBookingRef.DataBind();
}
}
Related
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
}
}
}
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.
I have this button event:
protected void AddDetails_Click(object sender, EventArgs e)
{
DataSetTableAdapters.SelectFriendsTableAdapter sft = new DataSetTableAdapters.SelectFriendsTableAdapter();
try
{
sft.AddFriend(current, newFriend, false);
}
catch
{
error.Text = "Something happened. Bummer!";
}
}
in the try section, I'm adding entries in the Database. In the page there are Labels / Textboxes with the corresponding values.
Everything works fine. However, I need to refresh the page in order to see the changes after I click on this submit button.
I have added if(!IsPostBack) at the beginning of the PageLoad code, but I still need to visit another page / come back to see the changes.
Any ideas?
Thanks.
Thank you for your replies. I'm using a ListView:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSetTableAdapters.SelectFriendsTableAdapter sdt = new DataSetTableAdapters.SelectFriendsTableAdapter();
DataTable tab = sdt.SelectFriends();
ListView1.DataSource = tab;
ListView1.DataBind();
}
}
, so the ListView content should get updated.
If you don't want to refresh the page and you're using asp.net webforms you can use an UpdatePanel and place all your controls inside of it, etc... That will keep the changes made in the form / display when you submit the info. I'm assuming the form is on submitting the changed/new data as you didn't specifically state how the data was being updated/changed.
If you don't want to use an update panel, then when the page is posting back you will have to set the values for the UI controls that you want to update with the values from the 'newFriend' object (I'm assuming that has the changed values).
Found it!
The solution for this issue is to add this code at the end of your submit button click event:
Server.Transfer("currentpage.aspx");
I have an asp.net CheckBox, now I want to reload page after check or uncheck and use CheckBox.Checked information to choose sql query for gridview. I have put code like this in Page_Load method:
if (CheckBox1.Checked)
{
query = "select ...";
}
But nothing happen. I set AutoPostBack also. Tried to use event. Don;t know how this system works:/
EDIT:
Checkbox works ok, but the problem is in something different. After I click checkbox, in Page_Load method I will use my query to setup SqlDataSource. Looks like page is reloaded, but gridview is not refreshed. When i click on gridview's column mame (to sort this column), gridview is refreshed by new sql query. So i need to think how to refresh grid view after click check box.
It seems that you are not using IsPostBack property on page load event. If you not use this your CheckBox will be reset on every page load
Try this way
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Here do your stuff.
}
}
I have a ListBox which I am adding ListItems to in a codebehind. The problem I'm having is the ListBox is not seeing the selected items. I have the ListBox being populated dynamically depending on what the user selects from a DropDownList, so the DropDownList has AutoPostBack set to true. I think this is somehow causing the problem.
My SelectedIndexChanged method, which is used whenever an item in the DropDownList is selected, calls a method called PopulateListBox. Here's what those methods looks like:
protected void SelectedIndexChanged(object sender, EventArgs e)
{
string typeStr = type.SelectedItem.Text;
MyType = Api.GetType(typeStr);
PopulateListBox();
}
private void PopulateListBox()
{
listbox.Items.Clear();
foreach (PropertyInfo info in MyType.GetProperties())
listbox.Items.Add(new ListItem(info.Name));
}
For what it's worth, here are the DropDownList and ListBox:
<asp:DropDownList runat="server" ID="type" width="281px" OnSelectedIndexChanged="SelectedIndexChanged" AutoPostBack="true" />
<asp:ListBox runat="server" ID="listbox" width="281px" height="200px" selectionmode="Multiple" />
What I am trying to do is add a List of strings (strings being the selected items) as a session variable upon clicking a submit button. The button redirects to a new page after the List has been added to the session. Going through in debugger, the List of strings is empty at the point where I add it to the session.
listbox.GetSelectedIndices() returns nothing.
Update
I can access the selected items if I do not make a change in the DropDownList. The ListBox is initially populated on page load, and if I make selections they are recognized. If I select something from the DropDownList and the ListBox is repopulated, the selections are not recognized.
My Page_Load method does only two things. It initializes my Api variable and calls PopulateDropDown, which looks like this:
private void PopulateDropDown()
{
foreach (Type t in Api.GetAllTypes())
type.Items.Add(new ListItem(t.Name));
string typeStr = type.Items[0].Text;
Type = Api.GetType(typeStr);
PopulateListBox();
}
The problem is that you call PopulateDropDown() on every single Page_Load(), which calls PopulateListBox(), which clears the listbox and repopulates it. By clearing the listbox, you clear the selection.
You need to replace your call to PopulateDropDown() in the Page_Load() with the following code. The issue that I think you don't realize is that the page is loaded on every postback -- and that in the page life cycle, the page load occurs before the event. So by selecting a drop down item, you execute the Page_Load() event first (which indirectly executes the LoadListBox method, clearing the selection). The following code will populate the drop down list the first time the page loads only. Keep it the same wherever else you are using the load dropdown method:
protected void Page_Load(object sender, EventArgs e)
{
// Do your API code here unless you want it to occur only the first
// time the page loads, in which case put it in the IF statement below.
if (!IsPostBack)
{
PopulateDropDown();
}
}
The IsPostBack returns a boolean indicating whether the server side code is running because the page is loading for the first time ("false") or as a post back ("true").
As I said elsewhere, keep in mind that a listbox with potential for multiple selected values must be handled differently than one with potential for a single selection. Don't reference listbox.SelectedItem, but rather:
foreach (ListItem item in lbFullNames)
{
if (item.Selected)
{
// TODO: Whatever you are doing with a selected item.
}
}
I have also found that if you disable the ListBox server-side, then use client side code to enable the list box using code like the following, then you cannot get the selected items server side.
$('.css-class-assigned-to-listbox').attr('disabled', '');
The fix is simply to make sure it is enabled server-side (the default), then disable it (see blow) or enable it (see above) using client-side code.
$('.css-class-assigned-to-listbox').attr('disabled', 'disabled');