Set Focus - Set Default Button to Controls in Web Parts

To sets the browser focus to a specified control and/or set a Default button in your web part, You can use this code:

protected override void CreateChildControls()
{
TextBox txtTextBox = new TextBox();
txtTextBox.ID = "txtMyTextBox";

//Make sure you first add the control then set fucus

this.Controls.Add(txtTextBox);

this.Page.SetFocus(txtTextBox.ClientID);

Button btnSave = new Button();

btnSave.ID = "mySaveButton";
btnSave.Text = "Save";

//Make sure you first add the control then set the default
button

this.Controls.Add(btnSave);

this.Page.Form.DefaultButton = btnSave.UniqueID;

}

Previous
Next Post »
0 Komentar