?
Solved

Set variable to object already on aspx page

Posted on 2012-03-13
7
Medium Priority
?
349 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 800 total points
ID: 37717321
TheComboBox = Page.FindControl(ComboName.ClientID);
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 800 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 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

752 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