Home All Groups Group Topic Archive Search About
Author
24 Nov 2007 3:09 AM
xzzy
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!

Author
24 Nov 2007 3:46 AM
Liz
"xzzy" <mrbikej***@comcast.net> wrote in message
news:z5-dnU0K4cVLCNranZ2dnUVZ_t6onZ2d@comcast.com...
> In code behind,

maybe posting the code-behind would help?


I can iterate thru the items in a ListBox if it is like this
Show quote
> 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!
>
Author
24 Nov 2007 5:58 AM
xzzy
//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() + "-";
}
}











Show quote
"Liz" <liz@tiredofspam.com> wrote in message
news:u5X$dykLIHA.5988@TK2MSFTNGP02.phx.gbl...
>
> "xzzy" <mrbikej***@comcast.net> wrote in message
> news:z5-dnU0K4cVLCNranZ2dnUVZ_t6onZ2d@comcast.com...
>> 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:
>>
>> <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!
>>
>
>
Author
24 Nov 2007 7:28 AM
Liz
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 ...


Show quote
"xzzy" <mrbikej***@comcast.net> wrote in message
news:KvSdnTBLFPY6INranZ2dnUVZ_r6rnZ2d@comcast.com...
> //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() + "-";
> }
> }
>
>
>
>
>
>
>
>
>
>
>
> "Liz" <liz@tiredofspam.com> wrote in message
> news:u5X$dykLIHA.5988@TK2MSFTNGP02.phx.gbl...
>>
>> "xzzy" <mrbikej***@comcast.net> wrote in message
>> news:z5-dnU0K4cVLCNranZ2dnUVZ_t6onZ2d@comcast.com...
>>> 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:
>>>
>>> <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!
>>>
>>
>>
>
>
Author
24 Nov 2007 7:43 AM
Liz
"Liz" <liz@tiredofspam.com> wrote in message
news:%23ch8ZumLIHA.1164@TK2MSFTNGP02.phx.gbl...
>
> 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 ...

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 ...



Show quote
> "xzzy" <mrbikej***@comcast.net> wrote in message
> news:KvSdnTBLFPY6INranZ2dnUVZ_r6rnZ2d@comcast.com...
>> //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() + "-";
>> }
>> }
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> "Liz" <liz@tiredofspam.com> wrote in message
>> news:u5X$dykLIHA.5988@TK2MSFTNGP02.phx.gbl...
>>>
>>> "xzzy" <mrbikej***@comcast.net> wrote in message
>>> news:z5-dnU0K4cVLCNranZ2dnUVZ_t6onZ2d@comcast.com...
>>>> 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:
>>>>
>>>> <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!
>>>>
>>>
>>>
>>
>>
>
>
Author
24 Nov 2007 4:34 PM
xzzy
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 #########################################################

Show quote
"Liz" <liz@tiredofspam.com> wrote in message
news:%23ixA62mLIHA.4688@TK2MSFTNGP06.phx.gbl...
>
> "Liz" <liz@tiredofspam.com> wrote in message
> news:%23ch8ZumLIHA.1164@TK2MSFTNGP02.phx.gbl...
>>
>> 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 ...
>
> 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 ...
>
>
>
>> "xzzy" <mrbikej***@comcast.net> wrote in message
>> news:KvSdnTBLFPY6INranZ2dnUVZ_r6rnZ2d@comcast.com...
>>> //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() + "-";
>>> }
>>> }
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> "Liz" <liz@tiredofspam.com> wrote in message
>>> news:u5X$dykLIHA.5988@TK2MSFTNGP02.phx.gbl...
>>>>
>>>> "xzzy" <mrbikej***@comcast.net> wrote in message
>>>> news:z5-dnU0K4cVLCNranZ2dnUVZ_t6onZ2d@comcast.com...
>>>>> 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:
>>>>>
>>>>> <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!
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Author
24 Nov 2007 5:02 PM
xzzy
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}


Show quote
"xzzy" <mrbikej***@comcast.net> wrote in message
news:oKidnfAWZuIuz9XanZ2dnUVZ_sytnZ2d@comcast.com...
> 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
> #########################################################
>
> "Liz" <liz@tiredofspam.com> wrote in message
> news:%23ixA62mLIHA.4688@TK2MSFTNGP06.phx.gbl...
>>
>> "Liz" <liz@tiredofspam.com> wrote in message
>> news:%23ch8ZumLIHA.1164@TK2MSFTNGP02.phx.gbl...
>>>
>>> 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 ...
>>
>> 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 ...
>>
>>
>>
>>> "xzzy" <mrbikej***@comcast.net> wrote in message
>>> news:KvSdnTBLFPY6INranZ2dnUVZ_r6rnZ2d@comcast.com...
>>>> //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() + "-";
>>>> }
>>>> }
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> "Liz" <liz@tiredofspam.com> wrote in message
>>>> news:u5X$dykLIHA.5988@TK2MSFTNGP02.phx.gbl...
>>>>>
>>>>> "xzzy" <mrbikej***@comcast.net> wrote in message
>>>>> news:z5-dnU0K4cVLCNranZ2dnUVZ_t6onZ2d@comcast.com...
>>>>>> 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:
>>>>>>
>>>>>> <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!
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Author
24 Nov 2007 5:03 PM
xzzy
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 )


Show quote
"xzzy" <mrbikej***@comcast.net> wrote in message
news:I4qdnRKuy-zXxNXanZ2dnUVZ_vumnZ2d@comcast.com...
> 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}
>
>
> "xzzy" <mrbikej***@comcast.net> wrote in message
> news:oKidnfAWZuIuz9XanZ2dnUVZ_sytnZ2d@comcast.com...
>> 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
>> #########################################################
>>
>> "Liz" <liz@tiredofspam.com> wrote in message
>> news:%23ixA62mLIHA.4688@TK2MSFTNGP06.phx.gbl...
>>>
>>> "Liz" <liz@tiredofspam.com> wrote in message
>>> news:%23ch8ZumLIHA.1164@TK2MSFTNGP02.phx.gbl...
>>>>
>>>> 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 ...
>>>
>>> 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 ...
>>>
>>>
>>>
>>>> "xzzy" <mrbikej***@comcast.net> wrote in message
>>>> news:KvSdnTBLFPY6INranZ2dnUVZ_r6rnZ2d@comcast.com...
>>>>> //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() + "-";
>>>>> }
>>>>> }
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> "Liz" <liz@tiredofspam.com> wrote in message
>>>>> news:u5X$dykLIHA.5988@TK2MSFTNGP02.phx.gbl...
>>>>>>
>>>>>> "xzzy" <mrbikej***@comcast.net> wrote in message
>>>>>> news:z5-dnU0K4cVLCNranZ2dnUVZ_t6onZ2d@comcast.com...
>>>>>>> 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:
>>>>>>>
>>>>>>> <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!
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Author
24 Nov 2007 7:44 PM
Liz
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 ...


Show quote
"xzzy" <mrbikej***@comcast.net> wrote in message
news:keKdnZbzft3jxNXanZ2dnUVZ_smnnZ2d@comcast.com...
> 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 )
>
>
> "xzzy" <mrbikej***@comcast.net> wrote in message
> news:I4qdnRKuy-zXxNXanZ2dnUVZ_vumnZ2d@comcast.com...
>> 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}
>>
>>
>> "xzzy" <mrbikej***@comcast.net> wrote in message
>> news:oKidnfAWZuIuz9XanZ2dnUVZ_sytnZ2d@comcast.com...
>>> 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
>>> #########################################################
>>>
>>> "Liz" <liz@tiredofspam.com> wrote in message
>>> news:%23ixA62mLIHA.4688@TK2MSFTNGP06.phx.gbl...
>>>>
>>>> "Liz" <liz@tiredofspam.com> wrote in message
>>>> news:%23ch8ZumLIHA.1164@TK2MSFTNGP02.phx.gbl...
>>>>>
>>>>> 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 ...
>>>>
>>>> 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 ...
>>>>
>>>>
>>>>
>>>>> "xzzy" <mrbikej***@comcast.net> wrote in message
>>>>> news:KvSdnTBLFPY6INranZ2dnUVZ_r6rnZ2d@comcast.com...
>>>>>> //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() + "-";
>>>>>> }
>>>>>> }
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> "Liz" <liz@tiredofspam.com> wrote in message
>>>>>> news:u5X$dykLIHA.5988@TK2MSFTNGP02.phx.gbl...
>>>>>>>
>>>>>>> "xzzy" <mrbikej***@comcast.net> wrote in message
>>>>>>> news:z5-dnU0K4cVLCNranZ2dnUVZ_t6onZ2d@comcast.com...
>>>>>>>> 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:
>>>>>>>>
>>>>>>>> <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!
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>

AddThis Social Bookmark Button