Custom Object used with Profile Object

I have a custom object that inherits from CollectionBase and it does not successfully bind to GridViews or DropdownLists. My understanding is that that is because GridViews and Dropdownlists implement IEnumerable, therefore, if I want my custom object to bind to the gridview or the dropdownlist, I need to inherit from IEnumerable also, right?

Whatever the answer is, I'm trying to learn what I need to do to get my custom object to bind to a gridview and a dropdownlist.

I don't think this has anything to do with it, but I'm setting the value of a Profile object (ASP.NET 2.0) equal to the custom object. 

School.Teachers tlTeachers = new School.Teachers();

tlTeachers.Add(new School.Teacher("Buck"));

Profile.Teachers = tlTeachers;

Code like this works:

Label1.Text = Profile.Teachers.Count.ToString();            // The number of items in the profile object

Label1.Text = Profile.Teachers[1].FirstName.ToString();// The value of an item at a particular index position

But this does not:

GridView1.DataSource = Profile["Teachers"]; // Populate the GridView1 from the Profile Object

GridView1.DataBind();

0
kloepper
2/10/2006 6:53:43 AM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

3 Replies
906 Views

Similar Articles

[PageSpeed] 22
Get it on Google Play
Get it on Apple App Store

Can you further clarify what do you mean by "it's not working"? Do you get any specific error messages?

I have code as follows:

//CLASSES

namespace School
{
    [Serializable]
    public class Teachers : List<Teacher>
    {

    }
    [Serializable]
    public class Teacher
    {
        private string firstName;
        private string lastName;
        public Teacher(string firstName, string lastName)
        {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public string FirstName
        {
            get
            {
                return firstName;
            }
        }

        public string LastName
        {
            get
            {
                return lastName;
            }
        }

    }
}

//USAGE

School.Teachers t = new School.Teachers();

t.Add(new School.Teacher("LastName1", "FirstName1"));

t.Add(new School.Teacher("LastName2", "FirstName2"));

Profile.Teachers = t;

GridView1.DataSource = Profile["Teachers"];

GridView1.DataBind();

And it works fine. Did you also note to add [Serializable] attribute to the Teacher and Teachers classes? And did you specify the profile in web.config as

<properties >

<add name="Teachers" type="School.Teachers" serializeAs="Binary"/>

</properties>

 


Thanks,

Teemu Keiski
Finland, EU
0
joteke
2/10/2006 8:30:33 PM

joteke:

No, there are no error messages, the Grid and dropdown lists just don't fill with data.

Scott Guthrie pointed this out to me: I’m wondering if the problem you were having is related to using fields instead of properties on your Teacher class.  Databinding only works with properties – so that might have been why it wasn’t working.

Below is first my code and under that is what Scott rewrote. His code works, mine does not work for the IList components.

I'm actually not clear as to what the difference is between my code and his (Fields versus Properties/Accessors)??Sad [:(]

 

My Code

#region Teacher Object++++++++++++++++++++++++++++++++++++++++++++++++++++++++

[Serializable()]public class Teacher

{

// private members

int id;

string firstName;

string lastName;

School.Classes classes = new School.Classes();

DateTime startDate;

// empty constructor

public Teacher()

{}

// Partial Constructor

public Teacher(string FirstName)

{

this.firstName = FirstName;

}

// Full Constructor

public Teacher(int Id, string FirstName, string LastName, School.Classes Classes, DateTime StartDate)

{

this.id = Id;

this.firstName = FirstName;

this.lastName = LastName;

this.classes = Classes;

this.startDate = StartDate;

}

// Public Accessors

public int Id

{

get {return this.id;}

set {this.id = value;}

}

public string FirstName

{

get {return this.firstName;}

set {this.firstName = value;}

}

public string LastName

{

get {return this.lastName;}

set {this.lastName = value;}

}

public School.Classes Classes

{

get {return this.classes;}

set {this.classes = value;}

}

public DateTime StartDate

{

get {return this.startDate;}

set {this.startDate = value;}

}

}

#endregion

 

[Serializable()]

public class Teachers : CollectionBase

{

public Teachers()

{}

public Teacher this[int index]

{

set

{

List[index] = value;

}

get

{

return (Teacher)List[index];

}

}

public int Add(Teacher value)

{

return List.Add(value);

}

public int IndexOf(Teacher value)

{

return List.IndexOf(value);

}

public void Insert(int index, Teacher value)

{

List.Insert(index, value);

}

public void Remove(Teacher value)

{

List.Remove(value);

}

public bool Contains(Teacher value)

{

return List.Contains(value);

}

}

 

 

 

Scott's rewrite

============================================================================

using System;

using System.Collections;

public class Teacher {

private string name;

public string Name {

get { return name; }

set { name = value; }

}

public Teacher(string name) {

this.Name = name;

}

public Teacher() { }

}

[Serializable()]

public class Teachers : CollectionBase {

public Teachers() { }

public Teacher this[int index] {

set {

List[index] = value;

}

get {

return (Teacher)List[index];

}

}

public int Add(Teacher value) {

return List.Add(value);

}

public int IndexOf(Teacher value) {

return List.IndexOf(value);

}

public void Insert(int index, Teacher value) {

List.Insert(index, value);

}

public void Remove(Teacher value) {

List.Remove(value);

}

public bool Contains(Teacher value) {

return List.Contains(value);

}

}

 

========================================================================

 

 

 

0
kloepper
2/10/2006 11:00:50 PM

Hi,

databinding (DataBinder.Eval) uses reflection under the covers to get information out of the objects. Basically it means inspecting the type of the data object at runtime, and then querying the data from properties and it is querying only for normal or indexed properties.

using properties such as this

public string Name {

   get { return name; }

    set { name = value; }

}

instead of field like this

public string name;

Is the key thing with data-binding, otherwise you'd need to cast to the type of your data object and access the field yourself (in code) in case you wouldn't use properties.


Thanks,

Teemu Keiski
Finland, EU
0
joteke
2/11/2006 7:41:42 AM
Reply:

Similar Artilces:

Custom Datasource Controls Vs. Object Datasource Controls
Hi folks,I am a web developer developing pages in the project. I need to integrate the front end pages with the backend API. Active directory is being used as the database system for our project. I only use the API and the methods offered by it to interact with the data.For all the operations on the data, I have methods that I can call from the API (viz. Create(), Delete(), Modify() etc.). For connecting the data to my frontend data controls (viz. GridView, DropDownList etc.) I can use Object Datasource Control. I also read some articles about Custom Datasource controls. Now, I am having a d...

Instantiating Custom Control
Hello all, first time poster here. I have created a custom control that I am trying to add on my page programmatically.  If I write the asp markup for the control on the .aspx file, it works fine.  However, when I try to add text to the control's properties using c#, it throws the object reference error mentioned above.  For example: CustomControl mycontrol = new CustomControl();      mycontrol.bodytext = "lorem ipsum....";PlaceHolder1.Controls.Add(mycontrol);---------------- Bodytext is a property of the CustomControl clas...

Custom Profile Provider
I have written a custom profile provider that pulls from a web service. When I run into Visual Studio it works like a champ but when I publish the website to the server I get the following error when the GetPropertyValues method is called: Message : Object reference not set to an instance of an object.Source : App_CodeHelp link : Data : System.Collections.ListDictionaryInternalTargetSite : System.Configuration.SettingsPropertyValueCollection GetPropertyValues(System.Configuration.SettingsContext, System.Configuration.SettingsPropertyCollection) Any ideas on why this is occurring wo...

Object reference not set to an instance of an object on Designer for custom control
 HelloI am fairly new to .NET and have a problem - I hope someone can help me.I'm using VS2005 / .NET 2.0. I have a website that must support multiple languages. In a class library I created an interface and controls (like the button control, literal, checkbox etc.) that use this interface and overwrite the render function to give additional multilingual functionality to these controls. (I got my idea from the following website: http://www.codeproject.com/KB/aspnet/localization_websites.aspx).  After compiling the class library and including the created DLL into my website, I p...

detailsview and objects (and objects inside objects inside objects)
hi there!as I was browsing through the posts related to the detailsview I couldn't find someone with the problem I'm facing so I decided to post here...well, I was able to bound an object datasource into my gridview and detailsview, it deletes and shows the data very well, BUT inside the object, there's another object, for example, I have bound a class called Province and inside the Province class there's the Country class indicating the data from the related country, to edit and/or insert I have placed a drop down list so the user selects the country from a drop down list, everything seems ...

Anyway to know when a custom color has been used on an object besides opening object?
Morning Y'all: Back to bother again ;). We recently got new machines, so when I reloaded PB, I re-added my custom colors. Problem is my boss added some custom colors to some objects in our app I didn't know about. I got them but in the future, is there a way I can grab those custom colors and save them so I can put them back easily? I looked in my pb.ini and there's a colors section - is that it? Thanks, -- Evita Chapa Senior Systems Analyst Command Technologies, Inc. I don't think that there's an easy way to tell whether an object has used a...

Using <%= object %> in custom control
 I am writing a custom server control that will output dynamic data based on a form the user creates. The form will have some data objects exposed that they can use to display data. I did a simple test case to see if using <%= object %> would work but it displays '<%= object %>' instead of running as server code. Here is the line I put in the RenderContents() method of my control.output.Write("Job ID: <%= WorkItem.JobID %>"); Any ideas on how to make it treat this as server-side code? Thanks.   output.Write("Job ID: " +...

Property returns "Object" object that contains custom objects
Hi, I had some difficulties on returning the property of a custom JavaScript Object. Assuming I had a JSObject ObjA that was created using JS_NewObject with my own JSClass ObjA_class. And ObjA_class has one property getObjBs that will return a JavaScript standard "Object" object. And I want this "Object" object has properties that names are same as each ObjB object's property "id" , and the value of each properties is ObjB object. e.g. There are 2 ObjB objects: student1 and student2. The ObjB has a propperty "id": student1.id...

Object Datasource and Custom Object
Hey, I have an object datasource that populates a detailsview.  I know how to update the changes using a BLL method like so: public static UpdateUser(string Username, string Surname); However I would like to use the objects update() method ive created that essentially calls: public static UpdateUser(User user); The question is, how do I access the fields that are contained within the details view?  im guessing .FindControl() but i dont have the ID.  the other way I suppose is to itterate through all the controls within in it?  there must be a better way though? The ...

Profile Object and Applucation Object
For my website, I want to track the number of people that access the site per day, the amount of people that access each page per day, and the amount of time each user stays on the site, anonymous and authenticatied. Can someone point me in the right direction to do this. For which do I use the profile object and the application object. application: Number of people per day (check date, update into database as necessary) Number of people that access each page/day (same as above)   Session: amount of time each user stays on the site (use Session_End to log the time)   I wou...

Using "Business Objects" as a Data Source dor DataWindow.NET Objects
Hi: I have developed a "Business Object" layer which gets information from the DataBase. I would like to use these objects as a Source for the DataWindow.NET objects. It is possible? DO I have to use External Source? Some example? Thanks in Advance! Gus dw.net doesn't support this: I will be glad if someone comes up with an generic work around to bind. sybase is snoozing on this one. thanks vsv "DataWindows.NET" <gussabina@yahoo.com> wrote in message news:4740cbfa$1@forums-1-dub... > Hi: > I have developed a "Business Ob...

Custom Object using using SessionStateStoreProviderBase
Hi, I am storing the  custom User Object in Session  using   SessionStateStoreProviderBase class with Oracle as Database. Structure of User Object is  follows User Contains Object of Genric List  of Type Role Object Role Object contains  Genric List  of Type Permission Object I am getting the following Exception. Does  any one has idea about this exception. i am scracthing my head for 2 days Any help would be greatly apperiacted System.Web.HttpException: The session state information is invalid and might be corrupted. at System.W...

dynamically creating custom visual objects on custom visual objects
I'm trying to create a cvo on another cvo using the following script: uo_xyz luo luo = create uo_xyz luo.show() However the show() is returning a -1. Both objects are inherited from the userobject class. Any thoughts on why the show() isn't working? Is the a better why to do this? Development Environment: Windows XP PowerBuilder 9 Build 5507 Thanks in advance Eric You can NOT CREATE a userobject, only OpenUserObject it... -- This is a FAQ, read Help, then search www.groups.google.com/advanced_group_search pbm_thisusuallydoesnothelp:-)) ...

Use Custom Objects as Extender Control Properties
I thought this post - http://pietschsoft.com/Blog/Post.aspx?PostID=1377 - was very useful in further developing a custom ajax control extender.  Thanks to Chris Pietschmann for posting his work. I expanded his work to override the ConvertFrom method to JSON deserialize a string back to the custom object.  In this example, I am using a Person object.  Please refer to Chris's article for code and adapt the code with the changes I made below in bold italics: GenericTypeConverter class:namespace SandboxClass { public class GenericTypeConverter<T> :...

Web resources about - Custom Object used with Profile Object - asp.net.presentation-controls

Profiles - Wikipedia, the free encyclopedia
I've certainly enjoyed working with Rick... I think it's useful and important to change the people you work with. You get so stuck in certain ...

Profile - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Study: Comments On Facebook Profile Pictures Affect Perceived Attractiveness
Comments left on Facebook profile pictures “strongly affect the level of perceived attractiveness of the profile owner physically, socially, ...

Unemployed Gamblers From Oregon? Report Profiles Web Bots
Reid Tatoris, co-founder of digital security company Are You a Human, doesn't like the term "ad fraud" when it comes to describing bots. "I think ...

May The Force (Temporarily) Be With Your Facebook Profile Picture
Create a temporary profile picture on Facebook, you shall, to honor the release of Star Wars: The Force Awakens . The social network extended ...


Every Day You Were Hustling—Was Your LinkedIN Profile?
... the “set it and forget it” tactic. Essentially, this means people use LinkedIn to post information about their job, and then leave their profile ...

Sunday Profile: Andrea Bocelli - Videos - CBS News
Tenor Andrea Bocelli has sold more than 85 million albums worldwide. The singer who leaves audiences smiling even when they're crying has a secret: ...

Slap a lightsaber on your Facebook profile to celebrate Star Wars
Choose Kylo Ren's fierce lightsaber or Finn's cool-blue Jedi weapon for a customized Star Wars Facebook profile picture.

How to boost your LinkedIn profile with keywords
You're a motivated, passionate and highly creative IT professional. But guess what? So is everyone else. To read this article in full or to leave ...

Resources last updated: 12/25/2015 4:50:47 AM