Solved

How to refer to a control in the cs file

Posted on 2013-05-29
9
576 Views
Last Modified: 2013-05-31
I placed a control named "lstEvalItemName" in the aspx file.

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

Then I tried to refer to it in the cs file.

intEvalItemID = int.Parse(lstEvalItemName.SelectedValue);

The name 'lstEvalItemName' does not exist in the current context.

This happens with all the controls on the page.

This page is attached to a master page.

What do I need to change?

Thanks
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
  • 4
  • 3
  • 2
9 Comments
 
LVL 25

Assisted Solution

by:Luis Pérez
Luis Pérez earned 200 total points
ID: 39204315
Try

intEvalItemID = int.Parse(this.lstEvalItemName.SelectedValue);

Hope that helps.
0
 

Author Comment

by:Dovberman
ID: 39204399
Did not work.

This does not work either.

intEvalItemID = int.Parse(this.FindControl("lstEvalItemName").SelectedValue);

Ideally, I would like to reference the control as a public listbox and give it a name.

public ListBox myListBox;

Then I would like to set myListBox equal to the actual control on the page.

?? myListBox == ("lstEvalItemName")
0
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 39204423
Where is located your lstEvalItemName ListBox? In the master page or in the derived page?
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

Author Comment

by:Dovberman
ID: 39204477
It is in a derived page.

This works, but is it the preferred way?

       public ListBox myListBox;

   
        protected void Page_Load(object sender, EventArgs e)
        {
            myListBox = FindControl("lstEvalItemName") as ListBox;
0
 
LVL 25

Assisted Solution

by:Luis Pérez
Luis Pérez earned 200 total points
ID: 39204509
Well, in theory it would't be necessary to declare the variable by yourself. Normally, you would be able to access directly by its real name ("lstEvalItemName").

You said the control is declared in the derived page. Are you trying to access it from the master's Page_Load?
0
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 39204659
By default all controls added to a page are protected members. It is accessible from within that page and only from within that page.
If you want to access the control from outside the page you have to make it public or use FindControl method.
public ListBox SelectBox
{
    get { return lstEvalItemName; }
}

Open in new window

You can then access SelectBox in the derived page
0
 

Author Comment

by:Dovberman
ID: 39205237
The control is acting as if it is protected. However the control I am trying to reference should be available directly.

I placed a control named "lstEvalItemName" in the aspx file.

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

Then I tried to refer to it in the cs file.

I am not trying to access it from the master's Page_Load?

intEvalItemID = int.Parse(lstEvalItemName.SelectedValue);

It should be accessible from within that page.
0
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 100 total points
ID: 39207933
If your project uses designer files, it could be that the designer file is not up to date. (could be due to changes made during debugging).
Try to re-save your aspx and rebuild the solution.
0
 

Author Closing Comment

by:Dovberman
ID: 39211369
Thank you.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

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 this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

729 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