Go Back   CodingForums.com > :: Server side development > ASP.NET

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-23-2012, 01:14 AM   PM User | #1
Khaled
New Coder

 
Join Date: Jan 2006
Location: Cairo, Egypt
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Khaled is an unknown quantity at this point
Question How to access property of user control with variable ID?

Hello everybody.
I have two instances of same user control.
This user control has a property.
Each instance has different value of this property and different ID.
For example:
Code:
<uc:UserControl ID="UC1" runat="server" prop="value1" />
<uc:UserControl ID="UC2" runat="server" prop="value2" />
I want to get the value of prop of specific user control, depending on a condition.
I try to do this by the following code:
Code:
int I;
UserControl UC;
if (condition) {
  I = 1;
} else {
  I = 2;
}
UC = (UserControl)FindControl("UC" + I);
string Prop = UC.prop;
But this code give an error said that UC doesn't have a property called prop.
Of course this error occurred because UC didn't compiled in design mode.
How I can solve this problem?
Thanks for any reply.
__________________
Khaled Mahmoud
Web, Flash, Games developer.
JavaScript reference VBScript reference ASP reference
Khaled is offline   Reply With Quote
Old 11-23-2012, 02:01 PM   PM User | #2
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
Without seeing the UC code it is going to be near impossible to answer- at first glance I don't see anything that would be 'wrong'... provided that those controls have a property called 'prop'. if it is not compiling then there is something wrong, and trying to correct something that you don't know why it's 'wrong' with outstanding compilation errors is also near impossible... you may simply have something wrong with a snip of code somewhere that makes the objects not build- who knows...

here's an example of two drop down list objects being accessed that reside on same page and their properties being accessed (note it is similar to your attempts)
Code:
                List<ListItem> li_actions = new List<ListItem>();
                li_actions.Add(new ListItem("Select...", "0"));
                li_actions.Add(new ListItem("******1", "1"));
                li_actions.Add(new ListItem("*******2", "2"));
                li_actions.Add(new ListItem("*******3", "3"));
                li_actions.Add(new ListItem("******4", "4"));
                foreach (ListItem li in li_actions)
                {
                    ddl_AddUpdateUser.Items.Add(li);
                }

                setDDLIndex(ddl_AddUpdateUser);
                ddl_AddUpdateUser.SelectedIndexChanged += new EventHandler(ProcessIndex);
                ddl_AddUpdateUser.AutoPostBack = true;

                List<ListItem> li_NewUserTypes = new List<ListItem>();
                li_NewUserTypes.Add(new ListItem("Select...", "0"));
                li_NewUserTypes.Add(new ListItem("New ******1", "1"));
                li_NewUserTypes.Add(new ListItem("New ******2", "2"));
                li_NewUserTypes.Add(new ListItem("New *******3", "3"));
                foreach(ListItem li in li_NewUserTypes)
                {
                    ddl_AddUserType.Items.Add(li);
                }

                setDDLIndex(ddl_AddUserType);
                ddl_AddUserType.SelectedIndexChanged += new EventHandler(ProcessIndex);
                ddl_AddUserType.AutoPostBack = true;
                ddl_AddUserType.Visible = false;
See? I am able to access the dropdownlist object's properties without errorring out; also not that the method in there 'setDDLIndex()' also accesses the dropdownlist objects properties...

Let's first focus on 'why' your code is not compiling and resolve those problems first; then move on to why your attempts to access properties are failing out

Worth noting... if that property is a private property, then it will error out with that message (or something similar), because to the outside world (code included) that property does not exist. <- that may be your problem
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE
alykins is offline   Reply With Quote
Reply

Bookmarks

Tags
property, user control

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:28 PM.


Advertisement
Log in to turn off these ads.