ASP.Net ObjectDataSource Data Binding - asp.net

I have two classes:
public class Contact{
public string contact { get; set; }
public string contact_type { get; set; }
}
public class Person{
public string first_name { get; set; }
public string last_name { get; set; }
Contact phone
}
In my asp.net application I need to see list of persons in a gridview.
I use ObjectDataSource to bind List. But only two columns: first_name and last_name are presents in the gridview. It is important to see contact of person.
In other words, how to bind subclass Contact to the same gridview.
What is the best way to do this? I don't like idea to use DataTable.
Thanks!

I think your best bet is to use template field and eval to access inner object fields:
<asp:TemplateField HeaderText="Contact">
<ItemTemplate>
<%# Eval("phone.contact") %>
</ItemTemplate>
</asp:TemplateField>
If you are using auto generated columns feature, keep it, declared columns will be appended after generated ones.

Related

Make a form field selection values dependent on another field value

I am editing the AP301000 screen, Bills and Adjustments, in a customization project.
I added two new fields to the form, which are both in the APRegisterExt class.
1. PONbr
2. ReceiptNbr
When a user selects a PO number, I want the ReceiptNbr selection values to be restricted to only the receipt records which have the same PO number.
I tried using a PXSelector attribute on ReceiptNbr, but because the PONumber is in an extension class, I cannot use Current<> to access this field.
Any ideas?
On your second CustomField's Selector attribute definition use Current<> statement for filtering, see below:
#region UsrCustomField1
[PXDBInt]
[PXUIField(DisplayName="CustomField1")]
[PXSelector(typeof(Search<DAC.Field>),
typeof(DAC.Field),
typeof(DAC.Field),
SubstituteKey= typeof(DAC.Field))]
public virtual int? UsrCustomField1 { get; set; }
public abstract class usrCustomField1 : IBqlField { }
#endregion
#region UsrCustomField2Dependent
[PXDBInt]
[PXUIField(DisplayName="CustomField2Dependent")]
[PXSelector(typeof(Search<DAC.Field, Where<DAC.Field, Equal<Current<UsrCustomField1>>>>),
typeof(DAC.Field),
typeof(DAC.Field),
SubstituteKey= typeof(DAC.Field))]
public virtual int? UsrCustomField2Dependent { get; set; }
public abstract class usrCustomField2Dependent : IBqlField { }
#endregion
Then on your ASPX file make sure you have added CommitChanges=True and AutoRefresh=true properties, see below:
<px:PXSelector runat="server" ID="CstPXSelector2" DataField="UsrCustomField1" CommitChanges="True" AutoRefresh="True" />
<px:PXSelector runat="server" ID="CstPXSelector1" DataField="UsrCustomField2Dependent" CommitChanges="True" AutoRefresh="True" />

ASP.NET MVC How to validate a posted value comes from a list

I have a drop down list on a form. A malicious user could arrange to post a value that isn't on the list.
A sample view model would be:
public class CountryViewModel {
[Required]
public int CountryId { get; set; }
public IList<Country> CountryList { get; set; }
}
CountryId will be posted, CountryList will be populated at the start of the relevant controller before Model.IsValid is called. When Model.IsValid is called, I'd like an error if there is no Country in the list with a matching CountryId. The country list comes from a database, so an enum won't work.
I can solve this using IValidateableObject or CustomValidationAttribute, but is there a built in way in Data Annotations?

Using Eval in aspx when Binding data to repeater control asp.net

i have class bound to repeater as a list here is structure of class
public String Name { get; set; }
public String Description { get; set; }
public List<ProductRecord> ProductList { get; set; }
i can get Eval("Name") on aspx page. How do i go about accessing ProductList properties in Eval e.g ProductRecord has Name property
Thank you in advance
You need to tell the Eval which one of the multiple ProductRecord you want. The problem is that you want to navigate a 1 to many relationship and get a value from the many. You have to make it a 1 to 1 relationship. For this you could either specify a single element like:
Eval("ProductList[0].Name")
Another option would be to put ProductList on a new Repeater, so you can show all the ProductList's properties for your structure. Or concatenating all values into a single value for display.

Html.ListBoxFor error problem asp.mvc 3

I have something like this in my code and I am getting error: Exception Details: System.ArgumentException: Value cannot be null or empty.
Parameter name: name . What am I doing wrong ? Thanks for help
#model IEnumerable<NHibernateFluentProject.Patient>
#Html.ListBoxFor(model => model, new SelectList(Model,"ID", "FirstName"));
#Html.ListBoxFor is used for your strong typed viewmodel. which could help to bind to your property. First part will take a lambda expression for a single item as a default seleced for your listbox, second part will take the item collections to dispaly all the listbox items.
For example: you have following two classes.
public class HospitalViewModel
{
public string SelectedPatient { get; set; }
public IEnumerable<Patient> AllPatients { get; set; }
}
public class Patient
{
public int Id { get; set; }
public string FirstName { get; set; }
}
From you view, you should do something like
#model HospitalViewModel
#Html.ListBoxFor(model => model.SelectedPatient, new SelectList(Model.AllPatients,"Id", "FirstName"));
OR if you only want to bind all your patients to a listbox, then use Html.ListBox instead
#model IEnumerable<Patient>
#Html.ListBox("ListBoxName", new SelectList(Model,"Id", "FirstName"));
You need to pass a lambda expression containing the property to bind the listbox to.

Databinding to the DataGridView (Enums + Collections)

I'm after a little help with the techniques to use for Databinding. It's been quite a while since I used any proper data binding and want to try and do something with the DataGridView. I'm trying to configure as much as possible so that I can simply designed the DatagridView through the form editor, and then use a custom class that exposes all my information.
The sort of information I've got is as follows:
public class Result
{
public String Name { get; set; }
public Boolean PK { get; set; }
public MyEnum EnumValue { get; set; }
public IList<ResultInfos> { get; set; }
}
public class ResultInfos { get; set; }
{
public class Name { get; set; }
public Int Value { get; set; }
public override String ToString() { return Name + " : " Value.ToString(); }
}
I can bind to the simple information without any problem. I want to bind to the EnumValue with a DataGridViewComboBoxColumn, but when I set the DataPropertyName I get exceptions saying the enum values aren't valid.
Then comes the ResultInfo collection. Currently I can't figure out how to bind to this and display my items, again really I want this to be a combobox, where the 1st Item is selected. Anyone any suggestions on what I'm doing wrong?
Thanks
Before you bind your data to the grid, first set the DataGridViewComboBoxColumn.DataSource like this...
combo.DataSource = Enum.GetValues(typeof(YourEnum));
I generally do this in the constructor after InitializeComponent(). Once this is set up you will not get an exception from the combo column when you bind your data. You can set DataGridViewComboBoxColumn.DataPropertyName at design time as normal.
The reason you get an exception when binding without this step is that the cell tries to select the value from the list that matches the value on the item. Since there are no values in the list... it throws an exception.

Resources