Solved

How to refer to a control in the cs file

Posted on 2013-05-29
9
555 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Regex validation 2 24
VIDEO DATASOURCE Control Resize issue 2 19
Footer for each row on Gridview 2 21
Convert datetime to time string 10 21
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

770 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