Subject: C# String to Variable?
Posted By: MAtkins Post Date: 9/14/2005 1:04:43 PM
Hi:

I've got a Dictionary object.
I've also got a bunch of System.Web.UI.WebControls.

Most of the keys in the Dictionary are the same as the variable names for some of the Web controls.

I want to get the Web control object from the DictEntry.Key.
You can do this in Javascript using eval.

I'd like to do something like:

DictionaryEntry myDE;
System.Collections.IEnumerator MyEnum = MyDict.GetEnumerator();

while (myEnum.MoveNext())
{
    myDE = (DictionaryEntry) myEnumerator.Current;
    myCtrl = eval(myDE.Key); //<<-- get the object from the key.
    myCtrl.Text = myDE.Value;
}


Is anything like this feasable?

Reply By: jacob Reply Date: 10/2/2005 12:45:17 PM
So what you are trying to do is to make some kind of UI from string values in a Dictionary; e.g. instantiating a TextBox from a string value "TextBox", right?

I think (not sure) you might wanna look at System.Activator.CreateInstance method. I have used this one to make plugable classes. I am not sure that you are able to use it in your case, but you should check it out.

Hope it helps anyways, Jacob.
Reply By: MAtkins Reply Date: 10/2/2005 12:52:44 PM
No, you've about solved my problem in the other thread.

I was just looking for a way to iterate through the form controls and capture them.

You've told me how to do that.
Now all I need is an efficient way to gain access to the control's properties like (.text, .selectedValue, .checked).
I think I have a way, I've created variables of each specific type and then cast the control object (c) to that object.
[code]
TextBox oTxt;
DropDownList oDrp;

 //. . . iteration
    oTxt = (TextBox) c;
    oTxt.Text = MyText;

Is there an easier way?

Reply By: jacob Reply Date: 10/2/2005 4:32:53 PM
Well, I am not quite sure I know what you are doing (I have also read the other thread).

So you are creating a number of controls dynamically and adding them to a form. Then you want to iterate through the controls and initialize them with values from the database and make it possible to edit these values.

Are all the controls TextBoxes? And where do you store the information about which controls to select etc.?

If you are referencing a TextBox abstractly as a Control then you would have to tell that it is infact a TextBox. Otherwise the code wouldn't know which properties it has.

Hope it helps, Jacob.
Reply By: MAtkins Reply Date: 10/2/2005 4:47:05 PM
No, I'm not creating the controls dynamically.

I develop database 'editors' all the time.
Sometimes they're for a Member, or a Company or an Event or  . . .

Every time I created one I had to manually rewrite the 'set text', insert, update procs to match the fields for that particular editor.

I was looking for a way to generically set the values of textboxes, dropdowns, checkboxes, etc. based on the fields in a DataRow object that matched. The DataRow's field name matches the ID of the Textbox, DropDown or Checkbox.

Now I'm using the new functions I just wrote w/ your help.
To update the values in a form to match a given data row, I just feed my proc the System.Web.UI.HtmlControls.HtmlForm object and the DataRow object.
The proc matches the ID to the DataRows field and sets the control's value to the DataRow's value.

To edit a record, I do just the opposite. I feed the update proc the HtmlForm and the DataRow objects and the proc sets the DataRow fields to match the HtmlForm object values.

Lastly, I have another proc that will go through all the form fields and reset them to "", 0, selectedIndex = -1, etc.
I'm gonna guess that this proc already exists (Form.Reset or something) but didn't know of one so I just wrote it.

It's working great.
Thanks for helping me out.


Go to topic 35312

Return to index page 461
Return to index page 460
Return to index page 459
Return to index page 458
Return to index page 457
Return to index page 456
Return to index page 455
Return to index page 454
Return to index page 453
Return to index page 452