Solved

Set variable to object already on aspx page

Posted on 2012-03-13
7
346 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

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
asp.net get gridview datakey from row command 2 41
array not updating 8 40
C# LINQ 5 40
Can Selenium do Load Testing? 2 56
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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 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