During 8th century, the Lawik rulers of the region adopted Islam. They formerly practiced either Hinduism or Buddhism, since they were associated with the Buddhist Kabul Shahis, and later with the Hindu Shahis (based in Gandhara, in present-day north-west Pakistan). Gardez later became a center of Kharijism and suffered several attacks by anti-Kharijite military chiefs. According to "Zayn al-Akhbar", written by historian Abu Sa'id Gardezi, Abu Mansur Aflah Lawik was reduced to a tributary status in Gardez by Emir Ya'qub ibn al-Layth al-Saffar in 877. However, the city remained under Lawik rule for about a century more. Around 975, Samanid-appointed governor Bilgetegin sieged Gardez but was killed by Lawiks during the attack. In 977, the Ghaznavids took Gardez, finally uprooting the Lawik dynasty. Some Lawiks, including Abu Sahal Marsal, entered the Ghaznavid nobility. In 1162, the city fell to the Ghurid dynasty.