Friday, September 3, 2010

Access a control in a web user control from within JavaScript

Suppose that you have a page that contains a web user control that, in turn, includes a Button control. If you want to access the button properties or methods, you can expose a property in the user control that returns the property or the method result. To use it in JavaScript, use the syntax shown below:

<script language="javascript" type="text/javascript">
function myFunction()
{
      var btnid = '<%= WebUserControl1.ButtonClientID %>';
      document.getElementById(btnid).style.color= 'green';
}
</script>

The code assumes that there is a property named ButtonClientID defined in the web user control.

public string ButtonClientID
{
      get
      {
            return Button1.ClientID;
      }
}

No comments:

Post a Comment