Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 14th, 2005, 01:04 PM
Authorized User
 
Join Date: Aug 2003
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default C# String to Variable?

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:
Code:
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?

__________________
http://www.softlinksys.com
Professional Software Developer since 1994.
 
Old October 2nd, 2005, 12:45 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
 
Old October 2nd, 2005, 12:52 PM
Authorized User
 
Join Date: Aug 2003
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?

 
Old October 2nd, 2005, 04:32 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
 
Old October 2nd, 2005, 04:47 PM
Authorized User
 
Join Date: Aug 2003
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Using INSERT INTO with a string Variable Suspect Access VBA 2 January 6th, 2015 11:25 AM
Need Help With System String^ variable etow Visual C++ 1 October 13th, 2008 08:45 PM
Convert a string into a variable jtrifts VBScript 4 February 21st, 2008 09:08 AM
Calling c# String variable help simonlm C# 2005 0 May 25th, 2007 04:01 AM
mysql_fetch_array with a variable string aeres PHP How-To 7 August 18th, 2003 01:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.