Click here to Skip to main content
15,907,326 members
Home / Discussions / ASP.NET
   

ASP.NET

 
QuestionMultiple use of one SqlDataSource Control Pin
Quecumber2565-Jan-07 3:50
Quecumber2565-Jan-07 3:50 
AnswerRe: Multiple use of one SqlDataSource Control Pin
ednrgc5-Jan-07 4:24
ednrgc5-Jan-07 4:24 
GeneralRe: Multiple use of one SqlDataSource Control Pin
Quecumber2565-Jan-07 4:46
Quecumber2565-Jan-07 4:46 
GeneralRe: Multiple use of one SqlDataSource Control Pin
aransiola5-Jan-07 5:53
aransiola5-Jan-07 5:53 
Questionaccount validation in asp.net Pin
Guy Harwood5-Jan-07 3:03
Guy Harwood5-Jan-07 3:03 
AnswerRe: account validation in asp.net Pin
Mark J. Miller5-Jan-07 3:30
Mark J. Miller5-Jan-07 3:30 
GeneralRe: account validation in asp.net Pin
Guy Harwood5-Jan-07 3:33
Guy Harwood5-Jan-07 3:33 
QuestionDatabinding a ListBox to List Pin
JambeDuSinge5-Jan-07 2:22
JambeDuSinge5-Jan-07 2:22 
Hi
The gist of my problem is that I want to bind an ASP.NET ListBox to a List< T > and have the Value field populated by a property of an object in the List and the Text field populated by the result of the call ToString() on the object in the List (the format of which is dictated by the DataTextFormatString propery of the ListBox):

I have two classes
PersonInfo - has properties PersonId / FirstName / LastName. This class implements IFormattable allowing customized string representation of any instantiated object.

PersonInfoList which derives from List< PersonInfo > (so stores objects of type PersonInfo).

I want to use PersonInfoList as the DataSource to a ListBox:

PersonInfoList pil = new PersonInfoList();<br />
ListBox1.DataSource = pil;<br />
ListBox1.DataTextFormatString = "{0:l, f}";  //formats output as [LastName], [FirstName]<br />
ListBox1.DataBind();


By not specifying the DataTextField or DataValue field, the IFormattable ToString() method is called on each PersonInfo object in the PersonInfoList and both the Text and Value properties are populated with [LastName], [FirstName] as expected.

The problem is that I want the Value field to contain PersonId and the Text field to contain the Person's name (formatted) as above.

If I add PersonId to the ListBox1.DataValueField and do not specify a value for ListBox1.DataTextField, the PersonId is used for both Text and Value.

The only way that I have found around this is to add a Me property the PersonInfo class:

public PersonInfo Me<br />
{<br />
get{ return this; }<br />
}


and then set

ListBox1.DataTextField = "Me";

YUCK! This produces the desired output, but there has to be a better way to do this! Confused | :confused:

Any ideas would be much appreciated.

Chris
AnswerRe: Databinding a ListBox to List Pin
minhpc_bk5-Jan-07 16:26
minhpc_bk5-Jan-07 16:26 
GeneralRe: Databinding a ListBox to List Pin
JambeDuSinge5-Jan-07 20:36
JambeDuSinge5-Jan-07 20:36 
GeneralRe: Databinding a ListBox to List Pin
minhpc_bk5-Jan-07 21:14
minhpc_bk5-Jan-07 21:14 
GeneralRe: Databinding a ListBox to List Pin
JambeDuSinge5-Jan-07 22:56
JambeDuSinge5-Jan-07 22:56 
QuestionPassword Encryption function Pin
Arindam Tewary5-Jan-07 1:13
professionalArindam Tewary5-Jan-07 1:13 
AnswerRe: Password Encryption function Pin
Imran Khan Pathan5-Jan-07 1:57
Imran Khan Pathan5-Jan-07 1:57 
AnswerRe: Password Encryption function Pin
enjoycrack5-Jan-07 15:51
enjoycrack5-Jan-07 15:51 
AnswerRe: Password Encryption function Pin
Vasudevan Deepak Kumar5-Jan-07 22:07
Vasudevan Deepak Kumar5-Jan-07 22:07 
QuestionSubmit the form when Hits ENTER key Pin
Jay_se5-Jan-07 0:08
Jay_se5-Jan-07 0:08 
AnswerRe: Submit the form when Hits ENTER key Pin
enjoycrack5-Jan-07 0:11
enjoycrack5-Jan-07 0:11 
GeneralRe: Submit the form when Hits ENTER key Pin
Jay_se5-Jan-07 0:45
Jay_se5-Jan-07 0:45 
GeneralRe: Submit the form when Hits ENTER key Pin
Marcus J. Smith5-Jan-07 2:28
professionalMarcus J. Smith5-Jan-07 2:28 
GeneralRe: Submit the form when Hits ENTER key Pin
Jay_se5-Jan-07 2:54
Jay_se5-Jan-07 2:54 
GeneralRe: Submit the form when Hits ENTER key Pin
Marcus J. Smith5-Jan-07 4:04
professionalMarcus J. Smith5-Jan-07 4:04 
GeneralRe: Submit the form when Hits ENTER key Pin
Jay_se5-Jan-07 19:34
Jay_se5-Jan-07 19:34 
Questionexecute an .exe file Pin
suveenmohan4-Jan-07 23:47
suveenmohan4-Jan-07 23:47 
AnswerRe: execute an .exe file Pin
enjoycrack4-Jan-07 23:52
enjoycrack4-Jan-07 23:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.