Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

ListBox Does not Populate

Posted on 2013-05-29
8
Medium Priority
?
480 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

578 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