Solved

How to refer to a control in the cs file

Posted on 2013-05-29
9
532 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
  • 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
 

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
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.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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 shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 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

15 Experts available now in Live!

Get 1:1 Help Now