Solved

ListBox Does not Populate

Posted on 2013-05-29
8
473 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
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 250 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 12

Accepted Solution

by:
jitendra patil earned 250 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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 12

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…

696 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