Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Wednesday, April 2, 2008

Reading TextBox or other Control values with javascript

Reading TextBox or other Control values with javascript.
Assume TextBox ID="txtName"
First, there are the simple examples of a standard aspx page:


<script type="text/javascript" languague="javascript">
function simple1() {
var txtVal = $get('<%= txtName.ClientID %>').value;
}
function simple2() {
var txtVal = $document.getElementById('<%= txtName.ClientID %>').value;
}
</script>
Here is the complex sample used for accessing a MasterPage's updatePanel for example:

// In the ASPX class
private string scriptKey = "alertName";
private string script = "function alertName() { var txt = document.getElementById('[txtNameID]'); alert(txt.value); }";

// In the Page_Load event
script = script.Replace("[txtNameID]", txtName.ClientID);
ClientScript.RegisterClientScriptBlock(this.Page.GetType(), scriptKey, script, true);

references: http://www.velocityreviews.com/forums/t109803-callback-manager-masterpage.html

No comments: