How to Set Master Page dynamically?

Yes. You can assign a master page dynamically during the PreInit stage. Because the master page and content page are merged during the initialization stage of page processing, a master page must be assigned before then.
void Page_PreInit(Object sender, EventArgs e) { this.MasterPageFile = "~/NewMaster.master"; }
PreInit event enables you to check conditions of the page request, such as whether the page is being loaded in response to a postback. You can also check the values of profile properties. Attempting to set the MasterPageFile property after the PreInit event will throw an InvalidOperationException exception. If the MasterPageFile property is not valid, an exception of type HttpException is thrown later in the page life cycle, but no exception is thrown when the property is set in the PreInit event.