Google Groups Home
Help | Sign in
ASP.NET listbox bound to IList<CustomObj>
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
rbr  
View profile
 More options Aug 28, 8:02 am
From: rbr <ryankbr...@gmail.com>
Date: Wed, 27 Aug 2008 14:02:10 -0700 (PDT)
Local: Thurs, Aug 28 2008 8:02 am
Subject: ASP.NET listbox bound to IList<CustomObj>
Hello,

I have a multi-part question.

Part 1: I have a listbox on a user control that I need to display a
particular property of a custom object in for each object in an
IList.
(I know... huh?) Here is the idea with some pseudo
-code.

I have a method that returns an IList<CustomObj>. The CustomObj looks
like the following:

        [Serializable]
        public partial class CustomObj
        {

                protected DateTime _createDate;

                protected DateTime _lastModifyDate;

                protected string _objName;

                protected int _objId;

                protected int _createByUserId;

                protected int _lastModifyUserId;

                [XmlElement(ElementName = "CreateDate")]
                public DateTime CreateDate
                {
                        get
                        {
                                return _createDate;
                        }
                        set
                        {
                                _createDate = value;
                        }
                }

                [XmlElement(ElementName = "LastModifyDate")]
                public DateTime LastModifyDate
                {
                        get
                        {
                                return _lastModifyDate;
                        }
                        set
                        {
                                _lastModifyDate = value;
                        }
                }

                [XmlElement(ElementName = "ObjName")]
                public string Name
                {
                        get
                        {
                                return _objName;
                        }
                        set
                        {
                                _objName = value;
                        }
                }

                [XmlElement(ElementName = "ObjId")]
                public int BrandId
                {
                        get
                        {
                                return _objId;
                        }
                        set
                        {
                                _objId = value;
                        }
                }

                [XmlElement(ElementName = "CreateByUserId")]
                public int CreateByUserId
                {
                        get
                        {
                                return _createByUserId;
                        }
                        set
                        {
                                _createByUserId = value;
                        }
                }

                [XmlElement(ElementName = "LastModifyUserId")]
                public int LastModifyUserId
                {
                        get
                        {
                                return _lastModifyUserId;
                        }
                        set
                        {
                                _lastModifyUserId = value;
                        }
                }

                public enum ObjFieldEnum
                {

                        CreateDate = 1,
                        LastModifyDate = 2,
                        ObjName = 3,
                        ObjId = 4,
                        CreateByUserId = 5,
                        LastModifyUserId = 6

                }

        }

I first need to bind my listbox.datatextfield to the
CustomObj.ObjName
field. Which I believe I have accomplished but would be interested in
some feedback.

Secondly, and more importantly. I need to take all items that have
been selected in the listbox and extract the associated ObjId for
those items. This is where I have gotten stuck.

Any advice would be greatly appreciated.

Thanks in advance.

rbr


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Cerebrus  
View profile
 More options Aug 28, 5:18 pm
From: Cerebrus <zorg...@sify.com>
Date: Wed, 27 Aug 2008 23:18:59 -0700 (PDT)
Local: Thurs, Aug 28 2008 5:18 pm
Subject: Re: ASP.NET listbox bound to IList<CustomObj>
Quick and Dirty:

Note that I have created a constructor for your CustomObj class as
follows :

public CustomObj(int oID, string oName)
{
  this.objId = oID;
  this.objName = oName;

}

Page code:
---
 IList<CustomObj> myList;

 protected void Page_Load(object sender, EventArgs e)
 {
  if (!Page.IsPostBack)
  {
   myList = new List<CustomObj>();
   myList.Add(new CustomObj(1, "One"));
   myList.Add(new CustomObj(2, "Two"));
   myList.Add(new CustomObj(3, "Three"));
   myList.Add(new CustomObj(4, "Four"));
   Bind();
  }
 }

 private void Bind()
 {
  ListBox1.DataSource = myList;
  ListBox1.DataTextField = "Name";
  ListBox1.DataValueField = "BrandId";
  ListBox1.DataBind();
 }

 protected void Button1_Click(object sender, EventArgs e)
 {
  StringBuilder sb = new StringBuilder(50);
  foreach (ListItem li in ListBox1.Items)
  {
   if (li.Selected == true)
   {
    sb.Append("Text - " + li.Text + ", ");
    sb.Append("Value - " + li.Value + ";  ");
    sb.Append("<br />");
   }
  }
  Response.Write(sb.ToString());
 }

---

On Aug 28, 2:02 am, rbr <ryankbr...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google