Solved

ListBox Does not Populate

Posted on 2013-05-29
8
466 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
  • 5
  • 2
8 Comments
 

Author Comment

by:Dovberman
Comment Utility
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 250 total points
Comment Utility
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 12

Accepted Solution

by:
Jitendra Patil earned 250 total points
Comment Utility
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
 

Author Comment

by:Dovberman
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 12

Expert Comment

by:Jitendra Patil
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for all your help
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ASP:DataGrid and how to bind to a BoundColumn 8 28
Code enhancement 5 12
Round a string to two digits 12 22
Form design in vb.net 7 17
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now