ASP.NET listbox bound to IList<CustomObj>

58 views
Skip to first unread message

rbr

unread,
Aug 27, 2008, 5:02:10 PM8/27/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
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

Cerebrus

unread,
Aug 28, 2008, 2:18:59 AM8/28/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
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());
}

---
Reply all
Reply to author
Forward
0 new messages