Solved

Set variable to object already on aspx page

Posted on 2012-03-13
7
347 Views
Last Modified: 2012-03-13
Hello Experts,

I think this is an easy one.  I need to set a variable to an object already my aspx.  Here's my code it's called during the page_load event.

        private void LoadSearchComboBoxs(string ComboName)
        {
            ComboBox TheComboBox;
            TheComboBox = Page.Controls[ComboName];
}

Open in new window


Thanks
0
Comment
Question by:eshurak
[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
7 Comments
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 200 total points
ID: 37717321
TheComboBox = Page.FindControl(ComboName.ClientID);
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 200 total points
ID: 37717333
Since you declared the reference TheComboBox to be of type ComboBox, you need to cast the thing returned by the indexing of the Controls collection (which returns "things" as type Control). In other words:

TheComboBox = (ComboBox)Page.Controls[ComboName];

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37717341
...ged325's solution would require the same, for the same reason  = )
0
Technology Partners: 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!

 
LVL 3

Author Comment

by:eshurak
ID: 37717667
Kaufmed - Thanks for your input.  I'm using:

            ComboBox TheComboBox;
            TheComboBox = (ComboBox)Page.Controls[ComboName];

But I'm getting the following errors

The best overloaded method match for 'System.Web.UI.ControlCollection.this[int]' has some invalid arguments      
Argument '1': cannot convert from 'string' to 'int'      

Is there a better way of doing declaring my object variable?
0
 
LVL 2

Assisted Solution

by:JAruchamy
JAruchamy earned 100 total points
ID: 37717731
Hi,

Try this,

ComboBox TheComboBox;
TheComboBox =  (ComboBox)Page.FindControl(ComboName);
0
 
LVL 3

Author Comment

by:eshurak
ID: 37717780
I'm trying to use TheComboBox = (ComboBox)Page.FindControl(ComboName);

but it's also not working might be because I'm using a content page.
0
 
LVL 3

Author Comment

by:eshurak
ID: 37717786
Got it:

TheComboBox = (ComboBox)this.Master.FindControl("ContentPlaceHolder1").FindControl(ComboName);
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 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