Solved

ListBox Does not Populate

Posted on 2013-05-29
8
475 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 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 13

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

691 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