Tuesday, June 8, 2010

How to access a control by using JavaScript

Reference the ClientID property (or UniqueID) of the control in the JavaScript.
protected void Page_Load(object sender, EventArgs e)
{
    Button btn= new Button();
    btn.ID = "btn5";
    btn.Attributes.Add("runat", "server");
    btn.Attributes.Add("onclick", "pop('" + btn.ClientID + "')");
    btn.Text = "Test";
    this.form1.Controls.Add(btn);
}

function pop(InputBoxID)
{
    var InputControl = document.getElementById(InputBoxID);
    alert(InputControl.value);
}

Or you can use the following method:

 
btn.Attributes.Add("onclick", "pop(this)");
function pop(InputBox)
{
    alert(InputBox.value);
}

No comments:

Post a Comment