|
Subject:
|
Can I get a Web Control's Variable Name?
|
|
Posted By:
|
MAtkins
|
Post Date:
|
10/2/2005 11:52:19 AM
|
Hi:
I want to be able to enumerate through the controls on a Web Form like:
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:
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?
|
|
Reply By:
|
jacob
|
Reply Date:
|
10/2/2005 11:58:10 AM
|
Can't you set the ID property and then use that?
Jacob.
|
|
Reply By:
|
MAtkins
|
Reply Date:
|
10/2/2005 12:05:17 PM
|
Thanks for the quick reply.
You've lost me though, how would I do that? I'm gussing in the OnInit proc somehow?
|
|
Reply By:
|
jacob
|
Reply Date:
|
10/2/2005 12:28:02 PM
|
You have something like this...<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.
|
|
Reply By:
|
MAtkins
|
Reply Date:
|
10/2/2005 12:42:39 PM
|
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.?
|
|
Reply By:
|
jacob
|
Reply Date:
|
10/2/2005 12:51:59 PM
|
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.
|
|
Reply By:
|
MAtkins
|
Reply Date:
|
10/2/2005 1:01:03 PM
|
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:
FirstName.Text = dRow["FirstName"].ToString();
LastName.Text = dRow["LastName"].ToString();
//etc . . .
To update a DataRow from them I do:
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)
|