ASP.NET - ObjectDataSource: using multi-select ListBox as ControlParameter - asp.net

I have a form that contains a GridView control which is databound to an ObjectDataSource Control. There are several TextBox controls that are already being used as ControlParameters for the ObjectDataSource. I also have a custom data object that the ObjectDataSource is associated with {TypeName="myDataClass"}. The values are passed from the ObjectDataSource to myDataClass.
Now I need to also use a multi-select ListBox as a ControlParameter. When I use the SelectedValue parameter of the ListBox, the ObjectDataSource is only seeing the first selected item in the list.
Here's the question: How can I get all the multi-select ListBox values passed into my custom object "myDataClass" instead of just the first one selected? Hence the ["multi-select"]
Thanks for any help!

Multi-select list boxes are tricky. You need to loop through the items in code to build a list of selected values. So you will probably need to implement a custom parameter that does this for you.
You might end up needing to bind to the grid from code-behind, instead of doing it declaratively.

This is typical problem. multi-select list box always return the first selected value and not all the selected value. We have to loop through the entire collection and check each individual value if selected or not.

Related

How to generate a dropdown list from another?

I want to generate a drop down list from another drop down list. That is I have a dropdown of countries. When selecting a country,another dropdown must come with values as states of that specific country. How to do that in asp.net using c#?
for each country you have, add a new list item to the drop down list, with text the country and the value some id of the country. On the second drop down list, set the auto post back property to true and add an event to the on selected item change. In the event code, get the selected item and by the second ddl.
Try it!
Tip: add a hidden field on the page, and on the selected item changed event from the first ddl, set the value of the hidden field, the selected value. On the page_load event, verify if the value is string.empty and if is an id in the value. If it is, bind the second ddl.
The technology you're looking for is called a Cascading Dropdown.
If you are using WebForms then Ajax Control Toolkit then this has one built in:
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx
Otherwise you might need to do a further search if you are using MVC.
PS I'm not particularly proud of posting a link to the Ajax Control Toolkit as its not the best library out there but it is an easy drop in for what you want. If you're serious about doing a good job then I'd search for better options for cascading dropdowns.

EntityDataSource filtered by Selectedrow of a Gridview

If I have 2 entities that share a navigation property (In my case an entity called Groups and one called Requirements) and I have a Gridview that displays Groups and another Gridview that I want to display the Requirements in the navigation property of the selected Group, should I pull in all the Requirements in the Requirements DataSource and then filter in the Gridview or should I filter in the DataSource?
I tried filtering in the Requirements DataSource by doing some Linq in the OnQueryCreated event, but I realized that OnQueryCreated is called when the page is loaded and then not again. Is there another event I should use?
Basically: Should I filter in the DataSource and if so where? If I should filter somewhere else, where?

Bind to a page in ASP.NET

I am confused about something really simple in ASP.NET. I have seen many times a pattern where there is a three column table, with label, control and validator in each column, which I can put together pretty straightforwardly. However, what I don't understand is how to handle binding here. If I have a table with Customer record and FirstName, LastName and PhoneNum, I want a page that takes customerId in the query string, and binds that record to the page so that I can use Bind() as the text box values.
However, there is no DataSource property on the page to bind the record to. I know I am missing something obvious, but I can't figure it out.
Any help would be appreciated.
You need to put your controls (labels, textboxes, validators) inside a control that accepts data binding (ie. FormView, GridView, Repeater, etc.etc.) and then bind your data to that control.
One way to do this is to use a DataSource Control such as a SqlDataSource and use it in conjunction with a Data Control that will bind to the previous datasource control like a DetailsView. Then you configure your DataSource control to only bring in data for the particular CustomerId; the SqlDataSource allows you to use a QueryStringParameter as part of the select statement to the database.
Each of the above controls have wizards that will allow you to easily configure them.

Getting the selected row from a ASP.Net FormView bound to ObjectDataSource in the ItemUpdating event

I have to maintain an ASP.net application in VB.Net.
There is a page with a FormView bound to a ObjectDataSource.
I have to add some business logic on the ItemUpdating event of this FormView.
Unfortunately, some the data that I need to add this business logic is not exposed on the FormView user-interface itself, so I can not use FindControl to get the values (I could add the controls, bind them to the fields I need and set their visible property to true, but that's ugly).
So, what I would need to do is to get access to the Data Row corresponding to the currently selected item in the FormView from the code behind as it has the data I need to add my business logic code.
Unfortunately, I don't manage to get access to the row.
Thanks in advance for your help.
Try this:
Dim myData As Object = DirectCast(formview1.DataItem, DataRowView)("MyColumn")
EDIT:
If I remember correctly the DataItem is Nothing on ItemUpdating so my solution above does not work, does it? Then you have to load it from your Datasource with the given ID(CommandArgument).
Thanks, I managed to sort it using the Select method of the ObjectDataSource object.
This returned me a DataView containing the row that was currently being edited.

Displaying a gridview's footer with an empty (linqdatasource) datasource

The datasource of the datagrid is a LINQDataSource which relies on the value of a dropdownlist. In addition, I want to use the footer to insert new records - so I'd prefer not to use the empty dataset template. I am trying to find a way to always display the footer regardless if the datasource is empty or not.
EDIT:
Is there a way to create an empty/invisible object of the type in the LinqDataSource?
check the following article:
http://weblogs.asp.net/joewrobel/archive/2008/01/30/a-more-elegant-solution-to-display-gridview-header-and-footer-when-the-data-source-is-empty.aspx

Resources