The solution is pretty simple. Use Server.ScriptTimeout to set the timeout for the script in a page. The value is measured in seconds.
Server.ScriptTimeout = 3600
Use this in Page_Load event. The example makes the timeout to be an hour.
NOTE: using compilation debug="true" will ignore script timeout and it won't work. Make sure the debug value is false on the release version of web.config