If I get this right it's plain and simple answer. The first time the page gets loaded the button doesn't have the onclick attribute because U add it in the server on click. After that you have it because of EnabledViewState... So. Put your lone of code into Page_Load but only the fisrt time it loads (!isPostBack).