Multi-value dropdownlist - c#

Currently in asp.net, we can have only one value in a ListItem in a DropDownList. However I hope I can:
<MultiValueDropDownList id="ddl" runat="server" />
ddl.Items.Add(new MultiValueListItem { Text="text", DBValue="db", EngineValue=1 });
var dbValue = ddl.SelectedItem.DBValue;
What I'm going to do is inherit DropDownList and ListItem. Do you guys have any better solutions?
[Closed]
At last I decide to choose a lazy way. Store the values in ListItem.Attributes. And using extension methods of DropDownList to help to get the collection of values. Thanks for your help!

See if this helps:
EasyListBox
How to display multi-column in ASP Dropdown List?
asp.net : an alternative to multi-column DropDownList Gridview within dropdownlist / combobox (using the Ajax DropDownExtender)
Edit:
You can not inherit from ListItem.
See why: Extending ASP.NET DropDownList
The best solution then is to store the values inside ListItem.Attributes.

Related

Different SQL Datasource on Dropdownlist Selected Index Change

I have a Grid view and I want to use Two different Sql DataSources on Selecting different values from DropDownList i.e. On Selected Index Change.
Please help me How should I implement that???
I don't want to use Code behind to change the Data in Gridview....
Thanks in Advance
Without using code behind you cannot implement in this process so it is not possible

ASP.NET C# DataTable DropDownList Issue

I have a datatable/source binded to a drop down list in ASP.NET C#.
I am trying to return the selected drop down list value using:
dropdownlist.Text
However, this just returns the first list value. How can i get it to return the selected drop down list value?
The dropdownlist.Text property should work.
Make sure that you are not binding the list again on Page_Load. This would reset the SelectedValue to the first value.
Use code like
if(!IsPostBack)
{
//DataBind dropdownlist
}
Use DropDownList.SelectedValue property instead.
Dropdown can return only one value.
For list either you need to customize the dropdown or you need to find third party control to do that.
if You want to do it with 3rd party controls. you can try this
if you want to customize dropdown then view these
1
2

Gridview - FooterTemplate and 2 related dropdownlist

I have a gridview with multiple dropdownlist, textboxes on footer template and I wanted to add the values entered to the grid.
However, I have a condition where the second dropdownlist has to be loaded by based on what is selected in the first dropdownlist.
Is this possible? any examples would be greatly appreciated.
Have a look at this very good Example: Implementing Cascading DropDownList in ASP.NET GridView
And if you want an AJAX based method, take a look at: Ajax Cascading DropDownList With Database Example in GridView

How can I request a selected value in a combobox in ASP.net after a post?

I have a combobox on a web form. The user can select a single value in the list. When the "Save" button is pressed, it has to send the post. Amongst the combobox are a few other controls, such as textboxes. When I try to read out the information that has been posted I can't seem to find/access the selected value of the combobox. I can, however, read out the values from the textboxes just fine.
Here's the line of code I'm using to read the information:
project.CustomerId = Convert.ToInt32(
Request.Form["ctl00$MainContent$uxCustomerComboBox$HiddenField"]);
Thank you in advance.
Edit 1: This is how the combobox is built up (keep in mind it's inside of a table):
<asp:ComboBox ID="uxCategoryComboBox"
runat="server"
DropDownStyle="DropDownList"
AutoCompleteMode="SuggestAppend">
This is how I'm trying to read the actual value of the combobox:
uxCategoryComboBoxId.Value = uxCategoryComboBox.SelectedItem.Value;
Edit 2: This is how we tried to read the SelectedValue using an eventhandler, whilst debugging:
<asp: DropDownList ID="uxCategoryComboBox" runat="server" EnableViewState="true" OnSelectedIndexChanged="setIndex" AutoPostBack="true">
</asp: DropDownList>
This is the method setIndex:
protected void setIndex(object sender, EventArgs e)
{
_project[0].CategoryId = Convert.ToInt32(uxCategoryComboBox.SelectedValue);
}
If all of the controls are runat="server" with unique ID's, then you could just access it directly in code behind.
You shouldn't need to use the automatically-generated control IDs (e.g. ctl00$MainContent$uxCustomerComboBox$HiddenField) in the code-behind, or access the Request.Form object. The correct way to obtain the value of a drop-down list control is through the SelectedValue property. So I think you should just be able to do this:
project.CustomerId = Convert.ToInt32(this.uxCategoryComboBox.SelectedValue);
Edit:
I've just realised you're using a ComboBox control, not the standard DropDownList. ComboBox isn't a standard ASP.NET control (as far as I know), so it could be that having ViewState turned off is causing this problem.
Some kind of "state" is necessary for the combo-box to be correctly populated during postbacks. The standard ASP.NET controls use ControlState, so that they will work to some degree when ViewState is turned off. It could be that this ComboBox control you're using is totally reliant on ViewState, so I'd try turning that on, either for the page as a whole or just on this control.

On selected index change in Dropdownlist1 should select same value in Dropdownlist2?

I Have a page where there are two Dropdownlist and both have the same data in it but when user selects the value in Dropdownlist1 ,same value should be selected automatically in Dropdownlist2?
Thanks
Smartdev
You can do that a couple of ways. If you're using the built in asp.net postback approach then you can set the value of the dropdown box server side.
If you're wanting to do this on the front end you can use jQuery (or standard Javascript) to set the selectedIndex of the second dropdown.
I assume that's the kind of answer you are after and not just for someone to write the code for you. :)
so in your code behind OnSelectedIndexChanged, you need to rebind both DropDownList1 and DropDownList2 to the same datasource.

Categories