?
Solved

ListBox Does not Populate

Posted on 2013-05-29
8
Medium Priority
?
476 Views
Last Modified: 2013-05-30
The following code populated a listbox in VS 2008 with Windows 7.

The same code did not throw an error, but did not populate the listbox in VS 2012 with Windows 7.

aspx.cs:

 private void FillEvalItemNameList()
        {
           
            ListBox lstEvalItemName;
            lstEvalItemName = FindControl("lstEvalItemName") as ListBox;
            lstEvalItemName = new ListBox();

            lstEvalItemName.Items.Clear();
                     
            string selectSQL = "SELECT EvalItemID, EvalItemName FROM EvalItems WHERE isActive = 1 ORDER BY EvalItemName";

            string strConnection = connString.ToString();

            SqlConnection conStockSelect = new SqlConnection(strConnection);

            // Try to open the connection.
            conStockSelect.Open();

            SqlCommand cmd = new SqlCommand(selectSQL, conStockSelect);
            lstEvalItemName.DataSource = cmd.ExecuteReader();
            lstEvalItemName.DataTextField = "EvalItemName";
            lstEvalItemName.DataValueField = "EvalItemID";
            lstEvalItemName.DataBind();
            conStockSelect.Close();
            cmd.Dispose();
        }


I stepped through each line in Debug mode and followed the immediate window.

any ideas?
0
Comment
Question by:Dovberman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 

Author Comment

by:Dovberman
ID: 39206293
Here is the aspx code that defines the listbox.

<asp:ListBox id="lstEvalItemName" runat="server" AutoPostBack="True" Height="1.2in"
                                Width="2.28in" BackColor="Azure"  
                                SelectionMode="Single"
                                OnSelectedIndexChanged = "lstEvalItemName_Click">
                 </asp:ListBox>
0
 
LVL 19

Assisted Solution

by:Manoj Patil
Manoj Patil earned 1000 total points
ID: 39206767
Hey, no need of following code, if you have a ListBox on your aspx page.
            ListBox lstEvalItemName;
            lstEvalItemName = FindControl("lstEvalItemName") as ListBox;
            lstEvalItemName = new ListBox();
0
 
LVL 13

Accepted Solution

by:
Jitendra Patil earned 1000 total points
ID: 39206864
where have you place your <asp:Listbox>,

if it is in your master page then you need to
find the controls from your master page other wise as techchallenger1 said your dont need
to find controls if you have already defined in your .aspx page.

if your are using master page then please find the below example for the same
MasterPage.Master
<html xmlns="http://www.w3.org/1999/xhtml">
 
<head runat="server">
 
<title>Master Page with Controls</title>
 
<asp:ContentPlaceHolder id="head" runat="server">
 
</asp:ContentPlaceHolder>
 
</head>
 
<body>
 
<form id="form1"runat="server">
 
<div>
 
MasterPage Label:
 
<asp:Label ID="lblMaster"runat="server"Text="Sample master Page label Control"/>
 
MasterPage Textbox:
 
<td><asp:textbox ID="txtMaster"runat="server"Text="Sample Master Page Textbox Control"/>
 
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
 
</asp:ContentPlaceHolder>
 
</div>
 
</form>
 
</body>
 
</html>

Open in new window

Content Page Code may be default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
 
{
 
if(!IsPostBack)
 
{
 
Label masterlbl = (Label)Master.FindControl("lblMaster");
 
TextBox mastertxt = (TextBox) Master.FindControl("txtMaster");
 
lblContent.Text = masterlbl.Text;
 
txtContent.Text = mastertxt.Text;
 
}
 
}

Open in new window


hope this helps
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:Dovberman
ID: 39206979
The listbox is not in the master page. It is in the aspx page. Therefore I do not know why I needed to use the FindControl method.

Perhaps you can discover why the software thinks the listbox is on the master page.

Thanks
Default1.aspx
Default1.aspx.cs
Site.Master
Site.Master.cs
0
 
LVL 13

Expert Comment

by:Jitendra Patil
ID: 39207382
as per your code its working here. there is no issue with master page and i am able to access the listbox in codebehind without any problem.

so a small and quick solution could be,

just remove the below lines from your default.aspx.cs page

  ListBox lstEvalItemName;
            lstEvalItemName = FindControl("lstEvalItemName") as ListBox;
            lstEvalItemName = new ListBox();

save your project, clean your solution & rebuild it again

try this once.

hope this helps.
0
 

Author Comment

by:Dovberman
ID: 39207440
This did not help.  The object was not recognized without the FindControl method.

Should I report this as a bug?
0
 

Author Comment

by:Dovberman
ID: 39208306
Finally the mystery is solved.

Open a New Project
Choose  ASP.NET Web Forms Application, .NET Framework 4.5

Do not select the Empty Web Application.

Do not Rename the Default.aspx file. If you need additional pages, then

Right Click the project name in Solution Explorer.
From the Right Click Menu Choose 'Add'
From the 'Add' menu Choose 'Web Form using Master Page'

Modify the existing Site.Master, Web.config , and Default.aspx, and Default.aspx.cs as you choose.

Test every time you add a line of code !

In this case I added the ListBox control to the Default.aspx file.

<asp:ListBox id="lstEvalItemName" runat="server" AutoPostBack="True" Height="1.2in"
                                Width="2.28in" BackColor="Azure"  
                                OnSelectedIndexChanged = "lstEvalItemName_Click" DataSourceID="dscEvalItems" DataTextField="EvalItemName" DataValueField="EvalItemID">
                 </asp:ListBox>

This control and others on the aspx page were referenced in code behind without using FindControl().

Thanks for all your help
0
 

Author Closing Comment

by:Dovberman
ID: 39208311
Thanks for all your help
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question