• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

Set variable to object already on aspx page

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
eshurak
Asked:
eshurak
3 Solutions
 
Kyle AbrahamsSenior .Net DeveloperCommented:
TheComboBox = Page.FindControl(ComboName.ClientID);
0
 
käµfm³d 👽Commented:
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
 
käµfm³d 👽Commented:
...ged325's solution would require the same, for the same reason  = )
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
eshurakAuthor Commented:
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
 
JAruchamyCommented:
Hi,

Try this,

ComboBox TheComboBox;
TheComboBox =  (ComboBox)Page.FindControl(ComboName);
0
 
eshurakAuthor Commented:
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
 
eshurakAuthor Commented:
Got it:

TheComboBox = (ComboBox)this.Master.FindControl("ContentPlaceHolder1").FindControl(ComboName);
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now