Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

any idea why the listbox is not being populated? 1

Status
Not open for further replies.

mit99mh

Programmer
Sep 24, 2002
246
GB
The method returns a ListBox as I can iterate through the items and the correct items are returned.

However the web control is not populated. Any help much appreciated.

//assign listbox to return from method
ListItem forwardOption = SetForwardOptions(currentRoleId);


foreach(ListItem li in forwardOptions.Items)
{
Response.Write( &quot;<li>&quot; + li.Text );
}

private ListBox SetForwardOptions( int roleId )
{
ListBox listOptions = new ListBox();
switch( roleId )
{
//spoc
case 1:
listOptions.Items.Add( new ListItem( &quot;SPOC&quot;, &quot;1&quot; ) );
listOptions.Items.Add( new ListItem( &quot;Theme Owner&quot;, &quot;4&quot; ) );
listOptions.Items.Add( new ListItem( &quot;IMT&quot;, &quot;7&quot; ) );
break;
//gatekeeper
case 2:
listOptions.Items.Add( new ListItem( &quot;SPOC&quot;, &quot;1&quot; ) );
listOptions.Items.Add( new ListItem( &quot;Theme Owner&quot;, &quot;4&quot; ) );
break;
//topic owner
case 3:
listOptions.Items.Add( new ListItem( &quot;SPOC&quot;, &quot;1&quot; ) );
listOptions.Items.Add( new ListItem( &quot;Theme Owner&quot;, &quot;4&quot; ) );
listOptions.Items.Add( new ListItem( &quot;Topic Owner&quot;, &quot;3&quot; ) );
listOptions.Items.Add( new ListItem( &quot;IMT&quot;, &quot;7&quot; ) );
break;
//theme owner
case 4:
listOptions.Items.Add( new ListItem( &quot;SPOC&quot;, &quot;1&quot; ) );
listOptions.Items.Add( new ListItem( &quot;Theme Owner&quot;, &quot;4&quot; ) );
listOptions.Items.Add( new ListItem( &quot;Topic Owner&quot;, &quot;3&quot; ) );
listOptions.Items.Add( new ListItem( &quot;IMT&quot;, &quot;7&quot; ) );
break;
//imt
case 7:
listOptions.Items.Add( new ListItem( &quot;SPOC&quot;, &quot;1&quot; ) );
listOptions.Items.Add( new ListItem( &quot;Theme Owner&quot;, &quot;4&quot; ) );
listOptions.Items.Add( new ListItem( &quot;Topic Owner&quot;, &quot;3&quot; ) );
break;
//what to do here?
//default:
}
//set number of rows to number of items
listOptions.Rows = listOptions.Items.Count;

Response.Write( listOptions.Rows.ToString() );

return(listOptions);
}
 
First that catches an eye, ListItem forwardOption = SetForwardOptions(currentRoleId);, while SetForwardOptions returns a ListBox, not a ListItem. Actualy, this line should throw an exception. If you need to add the returned control to the page, you can do it like this:
Code:
html:
<asp:PlaceHolder ID=&quot;pl&quot; Runat=server />

code behind:
ListBox forwardOption = SetForwardOptions(currentRoleId);
pl.Controls.Add(forwardOption);

private ListBox SetForwardOptions( int roleId )
{
  //all your code above
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top