Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2007

Tip: Looking for answers? Try searching our database.

ListBox problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
xzzy - 24 Nov 2007 03:09 GMT
In code behind, I can iterate thru the items in a ListBox if it is like this
in the aspx page:

<asp:listbox id="ctlLB" runat="server" selectionmode="single">
 <asp:listitem value="0">First item</asp:listitem>
 <asp:listitem value="1">Second item</asp:listitem>
 <asp:listitem value="2">Third item</asp:listitem>
</asp:listitem>

But if it is the following in the aspx page, and I databind in code behind,
then even though the items are properly listed in the listbox and the
correct item is selected, ctlLB.Items.Count is always == 0 and thus I am
unable to iterate thru the items collection ( to store the selected item's
value back to the DB ).

<asp:listbox id="ctlLB" runat="server" selectionmode="single">
</asp:listitem>

or

<asp:listbox id="ctlLB" runat="server" selectionmode="Multiple">
</asp:listitem>

Argh! what am I doing wrong?

Thank you for your help!
Liz - 24 Nov 2007 03:46 GMT
> In code behind,

maybe posting the code-behind would help?

I can iterate thru the items in a ListBox if it is like this
> in the aspx page:
>
[quoted text clipped - 21 lines]
>
> Thank you for your help!
xzzy - 24 Nov 2007 05:58 GMT
//Step #1 bind the data:
 public bool ListAllowedSources(int xUserID,
System.Web.UI.WebControls.ListBox xListbox)
 {
  bool result = false;

  SqlCommand _MyCommand = null;
  SqlDataReader _MyReader = null;

  try
  {
   if ( _MyConnection.State == ConnectionState.Closed ) {
_MyConnection.Open(); }
   _MyCommand  = new SqlCommand("Table001_Sproc001", _MyConnection);
   _MyCommand.CommandType = CommandType.StoredProcedure;
   _MyCommand.Parameters.Add(new SqlParameter("@Field01", xUserID));
   _MyReader = _MyCommand.ExecuteReader(CommandBehavior.CloseConnection);

   xListbox.DataSource = _MyReader;
   xListbox.DataTextField = "SourceName";
   xListbox.DataValueField = "SourceID";
   xListbox.DataBind();
   if ( xListbox.Items.Count > 0 ) {  xListbox.SelectedIndex = 0; }
   result = true;
  }

  catch (Exception ex)
  {
   clsErrorsIO myError = new clsErrorsIO();
   myError.ErrorAdd(modName,myName, "UID = " + xUserID,ex.Message,false);
  }

  finally
  {
   if (_MyCommand != null) { _MyCommand.Dispose(); }
   if (!_MyReader.IsClosed) { _MyReader.Close(); }
   if (_MyConnection.State != ConnectionState.Closed ) {
_MyConnection.Close(); }
  }
  return result;
 }
//end Step#1

//Step#2 get what was selected from the list:
//
//the foreach is the offending line of code because ctlLB.Items.Count always
== 0, when it should equal the number of items in the list, thus the "if (
li.Selected == true )" never evaluated.
//
foreach(ListItem li in ctlLB.Items)
{
if ( li.Selected == true )
{
 ctlTypesIndex = ctlTypesIndex + mm.ToString() + "-";
}
}

>> In code behind,
>
[quoted text clipped - 26 lines]
>>
>> Thank you for your help!
Liz - 24 Nov 2007 07:28 GMT
why:  foreach (ListItem li in ctlLB.Items) when the ListBox is xListbox?

shouldn't that be:

foreach (ListItem li in xListbox.Items) ?

where does ctlLB get into the picture?

other than that, I don't see any problems ...

> //Step #1 bind the data:
>  public bool ListAllowedSources(int xUserID,
[quoted text clipped - 83 lines]
>>>
>>> Thank you for your help!
Liz - 24 Nov 2007 07:43 GMT
> why:  foreach (ListItem li in ctlLB.Items) when the ListBox is xListbox?
>
[quoted text clipped - 5 lines]
>
> other than that, I don't see any problems ...

sorry, I see you passed CtlLB to the ListAllowedSources method ... but I
don't know where in the code your foreach iteration of ctlLB is .. maybe
you're pointing to the wrong reference, or at the wrong time ??   hard to
follow snippets like this ...  but I'll bet you can iterate xListbox in the
ListAllowedSources method ... if so, a good reference to the Items
collection should do it ...

>> //Step #1 bind the data:
>>  public bool ListAllowedSources(int xUserID,
[quoted text clipped - 83 lines]
>>>>
>>>> Thank you for your help!
xzzy - 24 Nov 2007 16:34 GMT
Thank you for you time looking into this.  Here is the code-behind for the
web page:

//in code-behind of Webform.aspx
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 private void Page_UnLoad(object sender, System.EventArgs e)
 {

  //The foreach is called in "SetAll"
  SetAll("Page_UnLoad");
 }

//in code-behind of Webform.aspx
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 private void Page_Load(object sender, System.EventArgs e)
 {

  try
  {
   if ( !IsPostBack )
   {
    //ListAllowedSources is in a class and is called from "RefreshAll"
    RefreshAll();
   }
  }
  catch (Exception ex)
  {
  }
  finally
  {
  }
 }

//in code-behind of Webform.aspx
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 private void SetAll(string xCalledFrom)
 {
  try
  {
   if ( ValidScreen() )
   {
    foreach(ListItem li in ctlLB.Items)
    {
     if ( li.Selected == true )
     {
     }
    }

//in code-behind of Webform.aspx
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 private void RefreshAll()
 {
  try
  {
   clsSources mySource = new clsSource();
   mySource.ListAllowedSources(yUserID, ctlTypes);

// Previous posts #########################################################

>> why:  foreach (ListItem li in ctlLB.Items) when the ListBox is xListbox?
>>
[quoted text clipped - 102 lines]
>>>>>
>>>>> Thank you for your help!
xzzy - 24 Nov 2007 17:02 GMT
the error:
? ctlLB.Items
{System.Web.UI.WebControls.ListItemCollection}
   System.Object: {System.Web.UI.WebControls.ListItemCollection}
   Capacity: 16
   Count: 0
   IsReadOnly: false
   IsSynchronized: false

//the next 2 lines are the underlying problem
   Item: <cannot view indexed property>
   listItems: {Count=0}
//

   marked: true
   saveAll: false
   SyncRoot: {System.Web.UI.WebControls.ListItemCollection}

> Thank you for you time looking into this.  Here is the code-behind for the
> web page:
[quoted text clipped - 162 lines]
>>>>>>
>>>>>> Thank you for your help!
xzzy - 24 Nov 2007 17:03 GMT
SetAll is also called whenever the viewer selects one of these command
buttons:

protected void DeleteBtn_Click(Object Sender, EventArgs e)
{
 int NbrErrors = 0;
 try
 {
  if ( !SetAll("DeleteBtn_Click") )
  {
   ++NbrErrors;
  }
 }
 catch (Exception ex)
 {
  ++NbrErrors;
 }
 finally
 {
  if ( NbrErrors != 0 )
  {
   Response.Redirect(clsStaticVARS.WebSiteNameBrowser +
"ErrorPage.aspx",true);
  }
 }
}

 protected void SubmitBtn_Click(Object Sender, EventArgs e)
 {
  int NbrErrors = 0;
  try
  {
   if ( !SetAll("SubmitBtn_Click") )
   {
    ++NbrErrors;
   }
  }
  catch (Exception ex)
  {
   ++NbrErrors;
  }
  finally
  {
   if ( NbrErrors == 0 )

> the error:
> ? ctlLB.Items
[quoted text clipped - 181 lines]
>>>>>>>
>>>>>>> Thank you for your help!
Liz - 24 Nov 2007 19:44 GMT
xzzy:

my gut tells me this is all WAY too much code to populate a listbox and
update a DB column with its value;  this is basically easy stuff .. wht
don't you have a look at some code samples from other folks for ideas ..

OTOH, it would probably be worthwhile to understand why you can't iterate
the items collection ...

> SetAll is also called whenever the viewer selects one of these command
> buttons:
[quoted text clipped - 226 lines]
>>>>>>>>
>>>>>>>> Thank you for your help!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.