Accessing HTML controls programatically

ballisticnylon

Regular
Joined
Jul 9, 2003
Messages
82
Location
Tucson, AZ
Hi everyone,

I'm making a simple user control that encapsulates some javascript that I found online (the javascript is a pop-up calendar that writes the selected date to an html input tag). The script will not work if runat="server" is added to the tag, nor will it work if I try to use a textbox instead. I'd like to be able to expose the selected date as a property of the control I'm making, but I'm having trouble finding a way to access the value of the input tag programatically.

Any ideas?
 
Just inject a script block where you call something like:
Code:
document.getElementById("myControl").myProperty = 1234;
Do this as a startup script so it is rendered after the control.
 
That's a good start. I could do something like that in javascript, and then execute that script inside an event handler. But I'm not trying to set the input's property, I'm trying to expose the input's value as a read-only property of the containing .ascx control. If I could execute the javascript in C# or VB.NET and have it return a value (most likely a string), that would be ideal.
 
Oh, I see what you mean right now. It sounds like the input control is a pure HTML and not an asp:TextBox server control.

When your control loads you simply need to check the IsPostBack variable. If it's true then the control is being rendered due to the user submitting the form back to the server and the form data will be available to you. All HTML controls that are within the <form> tag can be accessed through Page.Request.Form using the name attribute for the control (not the ID attribute).

Option 1) Just call Request.Form["MyText"]

Option 2) You can make the properties of the HTML input available programmatically with:
Code:
<input id="MyText" type="text" runat="server" />
Option 3) You could just make it into a server control:
C#:
<asp:TextBox ID="MyText" runat="server" />
With 2 and 3 you can access the text with:
C#:
MyText.Text

You can then use any of these options to expose a read-only property:
C#:
public string MyProperty
{
   get { return MyText.Text; }
}
 
Back
Top