Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use an object's properties? 1

Status
Not open for further replies.

passs

Programmer
Dec 29, 2003
170
RU
Hello everyone!

I have a problem with using an object by it's name.
I have a list of layers for example and from menu i'm chosing one of it by name. For example
<div id=&quot;city1&quot;....>
...
</div>

and i'm chosing from dropdownlist item &quot;city1&quot;
somestr = dropdwonlist1.selecteditem.value;

i need to make this somestr as a name of an object and then to change some it's properties:

city1.style[&quot;visibility&quot;] = &quot;visible&quot;; for example.
But how to write it?
somestr.style.. - that will be a mistake.

If someone knows how to do that, i'll be very thankful for help!

Best regards,
Alexander
 
When you say somestr.style than means the object with the name &quot;somestr&quot; must exist somewhere and it was created using the new opeartor or &quot;somestr&quot; is a reference to a named object.
There are few solutions and here is one:
Create a Hashtable in which you store the names of the object as many you want:
Code:
Hashtable ht = new Hashtable();
MyType someObj = new MyType();
ht.Add(&quot;somestr1, someObj);
ht.Add(&quot;somestr2&quot;,someObj);
ht.Add(&quot;somestr3&quot;,someObj);

string somestr = dropdwonlist1.selecteditem.value;
swich (somestr)
{
   case &quot;somestr1&quot;:
         ((MyType)ht[&quot;somestr&quot;]).style = style1;
         break;
   case &quot;somestr2&quot;:
         ((MyType)ht[&quot;somestr1&quot;]).style = style2;
         break;
   //
   //
}
-obislavu-
 
Hey Obislavu!

Thank you a lot!
One more question:
case &quot;somestr2&quot;:
((MyType)ht&quot;somestr1&quot;).style = style2;
break;

here is ht&quot;somestr&quot; - the right syntax? or maybe: ht.somestr? How have i to bind it exactly to div layer?
This MyType, which has it to be?

Best regards!
Alexander
 
The rigth syntax is that the key string &quot;somestr2&quot; should be enclosed in the square brackets like index access.
But the best way is to do like here:

Code:
Hashtable ht = new Hashtable();
MyType someObj = new MyType();
ht.Add(&quot;somestr1, someObj);
ht.Add(&quot;somestr2&quot;,someObj);
ht.Add(&quot;somestr3&quot;,someObj);

string somestr = dropdwonlist1.selecteditem.value;
swich (somestr)
{
   case &quot;somestr1&quot;:
         if (ht.ContainsKey(&quot;somestr1&quot;))
         ((MyType)ht[&quot;somestr1&quot;]).style = style1;
         break;
   case &quot;somestr2&quot;:
          if (ht.ContainsKey(&quot;somestr2&quot;))
         ((MyType)ht[&quot;somestr2&quot;]).style = style2;
         break;
   //
   //
}
The MyType could be an existing type such as Button, Label, TextBox or your own type.
public class City : System.Web.UI.MobileControls.TextBox : 
{
}
That will be put in the code behind of the asp pages.
-obislavu-
 
Ok, thank you very much i think that will helps!
But one thing i do not understand:
look i hvae a div with id=layer1
and i'm chosing from dropdwonlist item layer1
i have dropwownlistmenu1.selecteditem.value = &quot;layer1&quot;; and have a <div id=&quot;layer1&quot;...>

then i have to change div's style. What type does have a div object?:)) May be it is a stupid question, but i realy do not understand, sorry for that:)

Anyway i'm very thankful to you and will try to do it!

Best regards,
Alexander
 
The div , span, body elements are objects of System.Web.UI.HtmlControls.HtmlGenericControl.
What you have to do is to define them in the code behind .cs file:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class MyCodeBehind1 : Page {

public HtmlGenericControl MyDiv;

public void SubmitBtn_Click(Object sender, EventArgs e) {

MyDiv.InnerHtml = &quot;Hello,world &quot;;
MyDiv.ID = &quot;layer1&quot;;

}
}
<%@ Page Inherits=&quot;MyCodeBehind1&quot; Src=&quot;Example1.cs&quot; %>
<html>

<body>

<div id=&quot;MyDiv&quot; runat=&quot;server&quot; />
</body>
</html>

-obislavu-
 
Ah, ok, thanks!
Now that understand everything! Thank you so much!!!
AP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top