Solved

How to refer to a control in the cs file

Posted on 2013-05-29
9
575 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
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!

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TCP Chat/GPS application security thru firewall 4 54
Aspx calendar pop up control 3 37
WPF and Powershell button hover change text 5 60
Shared Service Environment 2 54
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

739 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