Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to refer to a control in the cs file

Posted on 2013-05-29
9
Medium Priority
?
589 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 600 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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 600 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 300 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 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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

877 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