Home » ASP.NET » How to maintain TAB Order after POSTBACK Event in order to Set Focus to Next Control

How to maintain TAB Order after POSTBACK Event in order to Set Focus to Next Control

Use below code in order to set focus to next control after post back.


protected void Page_Load(object sender, EventArgs e) 

if (Page.IsPostBack) 

WebControl wcICausedPostBack = (WebControl)GetControlThatCausedPostBack(sender as Page); 
int indx = wcICausedPostBack.TabIndex; 
var ctrl = from control in wcICausedPostBack.Parent.Controls.OfType<WebControl>() 
where control.TabIndex > indx 
select control; 
ctrl.DefaultIfEmpty(wcICausedPostBack).First().Focus(); 


protected Control GetControlThatCausedPostBack(Page page) 

Control control = null; 
string ctrlname = page.Request.Params.Get("__EVENTTARGET"); 
if (ctrlname != null && ctrlname != string.Empty) 

control = page.FindControl(ctrlname); 

else 

foreach (string ctl in page.Request.Form) 

Control c = page.FindControl(ctl); 
if (c is System.Web.UI.WebControls.Button || c is System.Web.UI.WebControls.ImageButton) 

control = c; 
break; 



return control; 


4 Responses so far.

Leave a Reply

Your email address will not be published. Required fields are marked *

www.000webhost.com