ListBox problem

X

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!
 
X

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() + "-";
}
}
 
L

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

Liz

Liz said:
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 ...
 
X

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 #########################################################
 
X

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}
 
X

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 )
 
L

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top