Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional 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 October 2nd, 2005, 11:52 AM
Authorized User
 
Join Date: Aug 2003
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default Can I get a Web Control's Variable Name?

Hi:

I want to be able to enumerate through the controls on a Web Form like:
Code:
foreach(Control c in Controls)
{
    sType = c.GetType();
    sName = c.VarName <<-- not real how can I do this?
}
I'm trying to get the control's variable name.
For example, if a TextBox control is:
System.Web.UI.WebControls.TextBox FirstName;
When I'm iterating through them, how can I know which one is FirstName?

I could do it with a regular request.form like:
Code:
foreach(string Fld in Request.Form)
{
    Response.Write(Fld + ": " + Request.Form(Fld));
}
This gives me the name of the field plus the value.

Can I do it using Server Side Web controls?
__________________
http://www.softlinksys.com
Professional Software Developer since 1994.
 
Old October 2nd, 2005, 11:58 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Can't you set the ID property and then use that?

Jacob.
 
Old October 2nd, 2005, 12:05 PM
Authorized User
 
Join Date: Aug 2003
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the quick reply.

You've lost me though, how would I do that?
I'm gussing in the OnInit proc somehow?


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

You have something like this...
Code:
<asp:textbox id="firstname" runat="server" />
... in a form, right? And you want to find out if you are dealing with this exact controld when iterating through all controls, right? You will be able to get the ID through the id property, wouldn't you? Or did I misundestand?

Jacob.
 
Old October 2nd, 2005, 12:42 PM
Authorized User
 
Join Date: Aug 2003
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OH!! Yea, DING That's ringin a bell!

Then I guess when I'm iterating through them, to set the value, selected index, etc. I have to check the type and then cast the control to a variable of the appropriate type?

Or is there an easier way to set the value, selected index, checked, etc.?

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

Not completely sure what you are trying to accomplish but I think it sound right. The problem about using properties of rather unknown objects is that they are unknown! i.e. knowing which properties are available. Therefore you have to cast it to the right type.

Can't you set the properties directly through the definition (tag/attributes)? Or do you have to do it dynamically?

Hope it helps, Jacob.
 
Old October 2nd, 2005, 01:01 PM
Authorized User
 
Join Date: Aug 2003
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm trying to set the properties dynamically.

Basically, I'm trying to create a generic editor for database interfaces.

Usually to set the fields from an existing record I do:
Code:
FirstName.Text = dRow["FirstName"].ToString();
LastName.Text = dRow["LastName"].ToString();
//etc . . .
To update a DataRow from them I do:
Code:
dRow["FirstName"] = FirstName.Text;
dRow["LastName"] = LastName.Text;
// . . .
The problem is, for every editor those functions have to be rewritten because the fields are different (i.e. Event = , EventDate = , etc...)

So, I'm trying to write it so that I can use the same one, no matter what the FIELDS for the given editor are. I don't have to rewrite the SetText, UpdateRec, etc. procs for each editor.

I hope that makes sense.
Anyway, I think what you've given me here is enough to go on.

Thanks very much for your help, on a SUNDAY AFTERNOON?
Sheesh!! What's wrong with us?!?! hehe
Thanks again:o)






Similar Threads
Thread Thread Starter Forum Replies Last Post
How to retrieve control's "Name" and "Value" in an niranjanshaiwale C# 0 September 27th, 2006 04:20 AM
How to change the color of list control's column gemini Visual C++ 1 August 28th, 2006 12:58 AM
Problem at Custom control's OnDataBinding r_taduri ASP.NET 1.0 and 1.1 Professional 0 July 1st, 2006 06:41 AM
setting a web control's ID programatically lcsgeek General .NET 3 January 24th, 2005 02:50 PM
How to change filefield control's button text? shenliujun Javascript How-To 1 July 20th, 2004 12:05 PM





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