asp.net 2nd combobox not taking selectedvalue - c#

I'm loading a record to an aspx page. I have two comboxes. (AJAX comboboxes in this case) The second Loads based on the id from the first.
When I pass a key to the page in the query string I retrieve data into a class, then I populate the page fields from the class. When I set the carrier.selectedvalue = class.1value the selection shows correctly. When I set product.selectedvalue = class.2value the selection does not take. (The product has nothing selected)
If I drop down the list of the product combobox the correct data is loaded based on the the first combox (carrier).
I have tried two methods:
1) query product sql dataset (2nd dataset) based on selected value from the carrier combox
2) Load ALL products (2nd dataset) then FILTER the products based on the value from the carrier combox
Both methods load the 2nd combox with the values I need. Neither method helps me get the product combobox to show the selected value once the page renders.
Note that I'm doing all this in page_load
-Thanks in advance for looking.

The problem is that you are doing everything in Page_Load. Do things in the following order in the following events to prevent your issue:
Page_Init: Populate Carrier Items
Page_Init: Set Carrier selected value
Page_Load: Clear Items Product.Items.Clear()
Page_Load: Populate Product Items
Page_Load: Set Product Selected Items
See this guide for how to effectively use the Page events: http://attemptsatprogramming.blogspot.com/2011/03/practical-guide-to-aspnet-event-model.html

Gthompson83 put me on the right track. It was a databinding issue. I moved the set of the product combobx (the second combobox) to the databound event like so:
protected void cboProduct_DataBound(object sender, EventArgs e)
{
// Set the Product cbo
cboProduct.SelectedValue = c.Product_ID.ToString();
}
That is all it took. The class is still populated in the Page_Load event based on values from the query string. Once the DataBound event is fired i use the values I've placed in the class to set the value of the product combobox.
Some helpful info is here: Databinding events for data-bound controls
On the page linked above there is a good section on "Nested Data-Bound Controls".
Note that I did not follow that example completely... I did not do the databind on the second combobox programmatically... just catching the DataBound event was enough to allow me to set the selectedvalue.

Related

Changing variable in datasource query

I have two dropdowns, where the second dropdown's options depend on what was selected in the first one. Both are bound to SqlDataSource with different queries. The SqlDataSource query of the second dropdown has a variable that is linked to the option selected in the first dropdown.
And so far this works - only on the default value. When the program is started up, the second dropdown options are correct according to the first dropdown having the first (default) item selected.
However, when I select a different item in the first list, the items in the second dropdown don't update.
I've tried several solutions. First, I made a SelectedIndexChanged method that fires when the first dropdown selection is changes. Something like this:
protected void DropDown1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDown2.DataMember = DropDown1.SelectedValue;
}
This doesn't work. I feel like when a datasource is bound, a refresh should be automatic, and require minimal coding. What am I overlooking?
The solution was to check the "Enable AutoPostBack" property of the first dropdown. This causes a postback to the server to occur if the selection is changed.

Is it possible to re-DataBind a DropDownList upon postback triggered by SelectedIndexChanged event?

Let's say I have a DropDownList of product categories and a ListView of products based on the category selection in the DropDownList. When a user visits the page, there is a possibility of conconrency issue as new product belonging to a new category may be added to the inventory as the user is browsing.
When the user selects a different category to view(a SelectedIndexChanged event) and causes a postback, I want the DropDownList to update the list of categories to include the new category being concurrently added and at the same time still able to make a change of selected index. The DropDownList does not seem to support this updating while postback. So how do I handle this concurrent issue?
You need to bind and rebind regardless of checking postback dropdown in Page_Init event. It will not cause any change in selected index of the dropdown and you can continue your work properly, the page init method called before loading view state. means your dropdown index will be selected after this method and you can access it in dropdown_selectedIndexChanged method.
For more information on ASP.Net page life cycle
Edit 1: have a look on sample code
protected void Page_Init(object sender, EventArgs e)
{
// here you bind your dropdown
// don't check IsPostBack
DropdownList1.DataSource = db.GetCategories();
DropdownList1.DataTextField = "TextField";
DropdownList1.DataValueField = "ValueField";
DropdownList1.DataBind();
}
ASP.Net loads controls viewstate after Page_Init and before Page_Load event, so DropDownList1's selectedIndex will not be affected, and you got desired results.
just check if the page-call is a postback with IsPostBack and then call dataBind() on your dropdownlist
Of course you can very well change the data source after the post-back. You can even refresh the data in it by calling DataBind() method on the DropDownListbox.
Another way of doing this if you can't bind in Page_Init for some reason (as in my situation):
var selected = ddList.SelectedValue;
ddList.DataSource = DBHelper.GetCategories();
ddList.DataBind();
ddList.SelectedValue = selected;
You'll still need to handle situation when selected category was deleted.

Dynamic gridview based on dropdown

I've got a bit of an chicken and an egg.
I need to bind a gridview to a set of database values. The gridview is dynamic and the columns, cells are created at run time.
As such, I need to re-bind the grid on every postback in the pre-init, init events after very post back.
However, the data used to populate the grid uses a value from a dropdown box on the same page. As such, the value of dropdown is not accessible through viewstate until after the init event (i.e. the selected index is always the first item in the list until after on init).
How can I get access to the value of the drop down in time to rebind the grid before the pageload event?
If the Gridview always has the same number of columns, you could have the dynamic query generate column names as aliases, and then just rebind the Gridview on dropdown change?
you can use and HtmlHidden control for store the value you need. It doesn't depend by the viewstate and it's available into the page init

Master-Detail via ComboBox in C#?

I have a form showing the details of a client (various controls), along with their orders (in a DataGridView).
I am trying to add a ComboBox to let the user select one of the client's orders and display the items associated with it in a separate DataGridView.
However, I cannot work out which DataSource/DataBindings I need for either the ComboBox or the items DataGridView - please can anyone give me a few pointers?
Orders will be the data-source for the ComboBox - OrderId will be the Value field while Order Number or Order Date will be the text field. Items for that order will be data source for the items DataGridView. This grid needs to be bound in the combo-box's selection change event (set auto postback true for the combo box). Hope this helps.
Psuedo code for selection change event would be
protected void Orders_SelectedIndexChanged(object sender, EventArgs e)
{
var orderId = int.Parse(Orders.SelectedValue);
// Get items for this order from data store
var items = ...
// Bind with items grid
OrderItems.DataSource = items;
OrderItems.DataBind();
}
Orders is name of combo-box having orders while OrderItems is gridview to display items.
This seems to be already answered.
For example here:
How to get or set data from ComboBox in DataGridView
Or even better, just search for "DataGridView combobox" in StackOverflow and you will find many topics which cover every aspect of that problem.

SelectedIndexChanged fires a detailview grid but how to catch it's info

This is how i have it setup:
DAL: a dbml file for the context
BLL: All my BL rules
Types: I made type for every object that i return to my presentation layer.
Now that i made types and use this structure i find it hard to understand how to manipulate data. Normally i could just bind my datacontext to a grid and just activate all the datagrid's possibilities without having to worry about anything.
Now for example when i want to enable sorting, i have to write my own logic to return teh right rows to the grid etc.
I have a multiview with two datagrids. The first datagrid is an overview grid and has an option to select an entry. When i select the entry i switch to my second view and I want to show the details for the selected object there.
However... I bound a List to the first grid and now I don't know how to find out what entry is selected. I have an event handler for the SelectedIndexChanged.
I want to catch the right details for the 2nd datagrid there.
assuming that your objects from the BLL have an Id property, apply a the name of the property as a datakey to the first GridView. In the aspx page, add the following property to the gridview
DataKeyNames="Id"
On the SelectedIndexChanged, get the Id of your selected object using the following code:
string id = myGrid.SelectedDataKey.Value;
Now that you have your Id, you can query your needed object from the DB.

Categories