Changing variable in datasource query - c#

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.

Related

ComboBox Event works with ListBox but not with GridView

The function should be simple:
I select an item, which is added to a list, then i manage that item using the list.
As first try i put some buttons next to the listbox and it worked fine.
But the objective is to add two buttons in each row, so i replaced the listbox with a gridview.
What's the problem? The event SelectedIndexChanged of the ComboBox (which is used to add the item to the list) does trigger once but not the second time.
The page correctly goes through page load, but the combobox's event is ignored.
Code here:
https://pastebin.com/Bmg2qjTf
I think i must say the combobox is filled using an SQL: the first time the SQL Query is executed and applied to the ComboBox's DataSource and in a Session, on PostBacks instead it gets refilled thru the Session variable.
Solution: Adding a txtNumSpedizione.Text = "";
Troubleshooting:
I tried to make both (listbox and gridview) work at the same time and this way it worked, also the gridview was getting all the selections.
So i figured out the difference between before and after was in the Text field "reset".

How to I get the currently selected check value from CheckedListBox?

I have a CheckedListBox control with 6 checkboxes to choose from. I am trying to detect the following:
User checks the third index and changes the value from unchecked to checked.
Internal: Call an event - Grab the checked value of that third index.
That seems to suggest add a SelectedIndexChanged event. That doesn't tell me the user changed the value.
There's another: SelectedValueChanged.
I don't know if that tells me anything at all either, as the function is:
*_SelectedValueChanged(object sender, EventArgs e)
EventArgs is kind of useless to get this required information.
Again, I simply need to get the checked value of the item the user just selected. I'm not interested in gathering all selected items. Just the current one selected. Thanks.
In order to get the displayed value (content) of the selected item you can use something like the following:
Console.WriteLine(checkedListBox1.Items[checkedListBox1.SelectedIndex].ToStr‌​ing());
Or a shorter version:
Console.WriteLine(checkedListBox1.SelectedItem.ToString());
To determine if the selected item is checked or not, you can use something like the following:
Console.WriteLine(checkedListBox1.CheckedItems.Contains(checkedListBox1.SelectedItem));
This will be checking if the content of the selected item can be found among the checked items. That could lead to a wrong result if your CheckedListBox has duplicates. To avoid this, you might check for the index instead of the value, like this:
Console.WriteLine(checkedListBox1.CheckedIndices.Contains(checkedListBox1.SelectedIndex));
Edit: An even better solution I just found, is to use the GetItemChecked method. Something like the following would work perfectly:
Console.WriteLine(checkedListBox1.GetItemChecked(checkedListBox1.SelectedIndex));
Hope that helps :)

How to disable combox dropdown when user enters text into the combox

I have a simple combobox in c# forms which is populated from an array.
I have set AutoCompleteMode to SuggestAppend and AutoCompleteSource to ListItems. This allows me to filter through the list quickly by typing a string into the combobox and matching items are displayed as I type along. This works great.
However, when the drop down list is open and I start typing, the filtered list appears on top of the dropdown list but I cannot select from the filtered list but only from the drop down.
How to disable drop down list while open as soon as user enters a character into the combobox.
Currently only have one method for the combobox
private void SelectJobDropdown_SelectedIndexChanged(object sender, EventArgs e)
{
//plenty of code here
}
I have tried adding other methods for the combobox such as KeyPress or Keydown but none seems to be working for as I'm very likely doing something wrong
Using Visual Studio 2015
If I understood you correctly you don't like the overlapping list over the old drop down. Since you type letters into the ComboBox I would suggest to use the comboBox1_TextUpdate event. This nice line of code should fix your problem:
private void comboBox1_TextUpdate(object sender, EventArgs e)
{
comboBox1.DropDownStyle = ComboBoxStyle.Simple;
Setting the ComboBox.DropDownStyle property, which
specifies whether the list is always displayed or whether the list is displayed in a drop-down[...]
to ComboBoxStyle.Simple, which
Specifies that the list is always visible and that the text portion is editable. This means that the user can enter a new value and is not limited to selecting an existing value in the list.
will remove the original dropdown (long list) and only the filtered results remain.

How to save the drop down list selected index in asp.net?

i want that after clicking a button saves the index i have selected in order to storage that number to my database. My drop-down-list is in a comment below.
The drop-down-list is filled by using a query. And so far what it does, is that after clicking the button it storages the first index (which is 0), even when i want to save another index.
I have done some research, and so far i cannot solve it, hope someone can help me out.
Code for Dropdown list:-
<asp:DropDownList ID="ddlCategory" runat="server" Height="25px" Width="428px" Font-Size="Large"> </asp:DropDownList>
here's how i fill the drop-down-list
ClsUtil clsutil = new ClsUtil();
DataSet ds = clsutilerias.FillDDL(Category);
ddlCategory.DataTextField = ds.Tables[0].Columns["Name"].ToString();
ddlCategory.DataValueField = ds.Tables[0].Columns["Category_ID"].ToString();
ddlCategory.DataSource = ds.Tables[0];
ddlCategory.DataBind();
The DropDownList has properties. You need to set SelectedIndex, SelectedItem or SelectedValue properly (and in the correct event in relation to when you do the binding etcetera).
Edit: Maybe you updated your question, or maybe I misunderstood, but either way it seems now that you don't want to set the SelectedIndexperhaps as much as get the SelectedIndex. Even so, if you read the documentation and look at the examples provided in the links, you should have enough information to know what to do.
Basically, one of the <option> elements in the <select> on the client (the HTML code) will be selected when the data is posted back to the server. If your <asp:DropDownList> is AutoPostBack="True" you can go for a callback in the OnSelectedIndexChanged event, but there are other ways too, with or without auto-post-back. (You don't really need the view state, or even the <asp:DropDownList> at all, but can trigger a POST by submitting the form data in any way that suits your needs or preferences, and then read any values your interested in server-side.)
It seems that you found your solution in checking IsPostback in the Page, and not re-populate the list if the value is true. That's good. If you don't need to data-bind every time you render the page (even after a POST), that's a viable and common solution.
Another option, of course, would be to read the posted data in any event that happens before you do the data-binding. You may also want to handle the value of SelectedIndex to re-select the correct option in your drop-down after having populating it again, if you don't like having ASP.NET doing it for you.
When you are clicking the button, the entire page is refreshing that why you got the first index all the time
The best solution is :
if(IsPostBack == false)
{
//Save your data code here
}
You have other tricky solution which is :
put the dropboxmenu inside an update panel control
While binding datasource to combo, mention DataMember and DataValue property for combo box so that selecting elements from combo you get selectedValue as DataValue.
Well i have solved my problem, my problem was that when i clicked the button, the pageLoad method executed again, and there was where i had the code to fill my drop-down-list. so i just added:
if(!IsPostBack)
{
//here goes the code to fill the Drop down list
}

asp.net 2nd combobox not taking selectedvalue

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.

Categories