Bound DropDownList of "unused" items plus current row item in EditItemTemplate of GridView? - asp.net

I have 2 tables with unique IDs that can be mapped to each other (one table comes from a database outside of my application but has information about the same entities I'm tracking). I want to provide a UI for linking "my" records with the "other" ones, so I can store the "other" IDs in my table for the matching records. In a nutshell, the key UI element is a dropdownlist showing the available (ie, not-yet-linked) records from the "other" table.
I have a gridview for displaying "my" records in each row, plus a column showing some info from the linked record in the "other" table, if any. The data are about buildings and properties, so it might look like this:
Building1 Dallas TX Building1_Dallas_TX
Building2 Memphis TN Bldg2_Memphis_TN
Building3 Denver CO
Building4 Seattle WA
Building5 Boston MA Building5_Boston_MA
where the first 3 columns show some information from "my" table, and the last one shows the matching record from the "other" table, where the records have already been linked, else a blank where no link has been established. My actual gridview has several more columns from "my" table, but this is enough to illustrate the need.
When Edit (not shown) is clicked for a row, I want to be able to edit all 4 columns. The first 3 are textboxes; the last column should be a dropdownlist showing all of the unlinked records from the "other" table, plus an "Unassigned" option (as the first item in the list) so records can be "unlinked" or simply left unassigned, plus (for those rows that already have a link established) the linked item (it's "used" but it needs to be in there so they can continue to use it).
I can populate the dropdownlist correctly in code-behind in the gridview RowEditing event (except for the Unassigned item which I add as a ListItem in .aspx with the AppendDataBoundItems option set to true), and I can set the correct SelectedValue on the ddl's DataBound event, but on postback when I click to Update the row, the ddl has no items in it anymore (except for the Unassigned item) so the link is always cleared. If I set a link manually by directly editing my table, everything looks fine going into Edit mode -- the ddl has the correct items in it and it pre-selects the correct item, but all is lost on the Update postback. I have tried re-binding the ddl at various stages of the page cycle but clearly I don't quite understand that cycle well enough yet because I cannot figure out how to get the user-selected item to update my table.
I've also tried populating the ddl with a SqlDataSource SelectCommand, but have had no luck including the ID of currently-linked records in the other database (even though it's available in "my" table and I set it as a GridView DataKey and add it as a ControlParameter in the SelectParameters for the SqlDataSource. Without the currently-linked record in the ddl item list, I can't keep existing links.
I'll hold off sending a code sample for now -- this message is already too long! -- in hopes that the solution is obvious from what I've described... I don't think my aim is weird, but I'm open to being persuaded otherwise.
Many thanks in advance!
Chris

Ok, I'm not going into details with code because maybe you already done what I'm about to suggest.
For the last column, the drop down list, you have to execute an SQL statement to grab you the unlinked items. Use the "WHERE xx NOT IN". For the "Unassigned" option, just add an item to the list in the same code scope as above.
Next step is to handle the GridView's editing events manually. Insert, Update and Delete.
I think that way you won't have problems.

Related

asp.net webform Repeater, Update a set of textbox, Add to the List / Array

I am working on old webform asp.net application which uses Repeater.
The Repeater is a set of Name and address textbox fields. I have 3 sets of Name and Address information, basically, 3 sets of records in the object list to bind.
On the UI, I changed/update one of the Name and Address. I noticed in the list, which it iterate or loops through the repeater control
foreach (RepeaterItem item in this.NameAddressRepeaterControl.Items)
I see that an extra recod is added to the items.
Question:
I am used to fixed textboxes. When I update the textbox, I write code to take Exactly what is filled in the textbox and populate the DTO object to pass to the data layer to perform database transaction to update the database records.
When the new updated record is added to the Repeater Control list, I don't know which records is updated and which is the new records.
I am checking out OnItemDataBound and OnItemCommand to see if there is a way to get the old value from one of the field and also record the value of the new value. Is this possible?
The form contains 1 Save button and it will loop through the Repeater.Items to see what Name/Address to extract, but the new and old company exist in this list.
Instead of looping through the RepeaterControl.Items, Is there a way to extract from directly the visible Repeater control? If there are 3 repeater Name/Address control, is there a way to get all the info from each of the 3 sets of Repeater controls? The Repeater wraps around a user control, NameAddressCtrl.
I prefer not to replace the Repeater controls with fixed textboxes.
Any help is greatly appreciated.
Thanks.
I reviewed the code several time and add additional code to keep track what was add and changed in the repeated and only passed the information to be saved to the db. The solution was to write additional code. I did not change anything with the repeater behavior. Thanks for the comments and help.

Data copied between rows on form's datagrid

Within AX 2009, I have, through compare and compile, added two new controls within a datagrid on a form, a Real edit and a combobox. I have compiled with no issues. The Allow Edit property is set to Yes on both controls.
However, on the form, if I edit one row, whether typing a new number with Real edit or combobox, and don't hit Save but hit the Down Arrow key, the data I typed on the previous records is duplicate in the next record and so on until I release the Down Arrow key, rather than just setting the focus on a new record.
The table where these fields were created doesn't exhibit this behavior. The focus simply moves to the next record and what was typed will not carry over to the next record. Only the form does this...
Has anyone seen this behavior before with AX forms?
You may have omitted to specify the data source on the grid itself?
Or if the new controls are based on Edit methods on the data source, have you got the data source parameter in the method signature?
see http://msdn.microsoft.com/en-us/library/aa637541(AX.10).aspx

ASP.NET DropDownList - How do I handle missing values?

I have a list of values in a SQL Table which are used to popluate a DropDownList, having a unique Integer as the value of each item and a String as the visible text (via SqlDataSource). There is also a third field in the database which is a flag to indicate whether the list item is active or not (inactive items are not shown in the DropDownList)
Selections made in the dropdown are stored in the database as their integer value (as part of a dataset making up the overall record), not the text value.
Over time, the items in the DropDownList may be removed by marking the items as inactive. However, there is still a need to open old records which may have had a now-inactive item as part of it's data...
My question is, what's the best way to ensure that the missing value included in the dropdown for the old record?
The two methods that spring to mind are to either:
Populate DropDownList with only the currently active items and, when loading a record, catch when the app tries to select a value that doesn't exist, go back to the db to see what it should be (the text value) and manually add it into the dropdown.
or...
Populate DropDownList with all list items (both active and inactive), load the record and then programatically remove all the inactive items (execpt for any that are now selected).
Neither of these seem particularly efficient, so I was wondering whether there is a best practice for this kind of thing?
there are so many optimum ways to do that sort of things, i am defining here a couple of them, use any of following if your Drop down list items count is less than 200 , lets say drop down list is of Products
1)
i) Load all Products records in drop down list and hide the inactive ones by setting visible=false
i) When you load a user record than look for its drop down list value if its visible than select it and enjoy, if its not visible than make it visible by setting its property visible=true and select it and also set its index or id in a flag to change its visibility(visible=false) again after your/users required operation performed.
2)
i) load only active Product records in drop down list ii) while loading a user record also load its product details(name, id, inactive_status) using Joins in sql.
iii) check in that user record if item is inactive then add its record in drop down list as you have all its details now with user details record else just select it.
IMPORTANT NOTE: if you drop down list has items in millions than use ADVANCE SEARCH techniques
The first thing I would do is question your business logic - should you be able to make an item inactive if it is being used as a foreign key in an active row elsewhere? If you make it inactive should it not remove all foreign keys as well?
To answer your question though I would go with a variation on the second idea but filtering in the page like that is probably slower than doing directly with SQL so I guess you have something like this at the moment to populate the dropdown
SELECT * FROM Table WHERE Active = 1
You should already have your record and the foreign key value so I would change it to this
SELECT * FROM Table WHERE Active = 1 OR PrimaryKey = [YourForeignKey]
Then you will always have the selected item but should also be fairly efficient.

Expand/Collapse GridView rows of the same type

I have a gridview with some boundfields and templatefields. My data are sales Unit-Price-Store, right now the user selects a store (from a DropDownList) and the Grid is all Unit-Price for that store.
Is there a way to make put a button next to each unit so that when the user clicks it, that row expands to include a new row for each store? IE, when I'm looking at Store 1 and I click the button I also get to see Unit-Price info for Stores 2,3,4 (but just for this item)?
I used jQuery. Basically, I tagged that column and then did an each statement. If value.innerHTML != Store $(this).closest('tr').hide(); Else assign it a function on click that looks at all 'tr' where Unit matches but Store doesn't and slideToggle()

Easiest method to build where clause from checked items in DataList of PreviousPage

I have a selection list that is generated dynamically, it lists a number of checkboxes that are based on an end-user editable table, as such I have no idea what data, or how much, might be contained in this table and how many checkboxes or what they might contain, other than the primary keys of the table.
The user will select the checks they wish to see, and then control is passed to another page via PostBackUrl. The second page has to figure out which records to show (build it's where clause) based on the checkboxes checked in the previous page.
So, my problem is several-fold. First, asp:CheckBoxes don't have values. This can be worked around by a number of methods. Right now, i'm using a placeholder and dynamically creating the checkboxes in the ItemDataBound event of the DataList. I set the ID to "CheckboxKey1Key2" (where Key1 and Key2 are the primary keys of the check items).
Second, I have to walk through the controls of the PreviousPage to dig out all these values. That in itself is also a pain, but doable.
Now, my thinking is to build the where clause of my Linq2Sql query based on the keys I got from decoding the checked checkbox names. This all seems like a lot of jumping through hoops for something that shouldn't be this difficult. Am I missing something? Does anyone have any better solutions?
Make a class with a structure for your values that will be useful and easy for you to use on the result page. Then when the user clicks whatever it is they click to go to the result page, loop through the DataList, create a collection from the checked items, and you will only need to grab one object instead of everything, when you need to format your query.
Then when you get to the result page, loop through the collection and build the query in the loop. Pretty easy and not much code.

Resources