Asyraf MN

Asyraf MN

  • NA
  • 28
  • 7k

How to implement/validate IsPostBack on Page_Load?

Mar 13 2018 2:11 AM
  1. public partial class _Default : System.Web.UI.Page  
  2. {  
  3.     private Random random = new Random();  
  4.     private string testFilePath = @"C:\Users\Nor  Asyraf  Mohd No\Documents\TestFile.txt";  
  5.     private List<string> testFileLines;  
  6.     protected void Page_Load(object sender, EventArgs e)  
  7.     {  
  8.    
  9.   
  10.             if (!File.Exists(testFilePath))  
  11.             {  
  12.                 Response.Write($"The file does not exist: {testFilePath}");  
  13.   
  14.             }  
  15.             else  
  16.             {  
  17.                 try  
  18.                 {  
  19.                     testFileLines = File.ReadAllLines(testFilePath).ToList();  
  20.                 }  
  21.                 catch (Exception ex)  
  22.                 {  
  23.                     Response.Write($"Could not read file {testFilePath}. Exception details: {ex}");  
  24.                 }  
  25.             }  
  26.        
  27.   
  28.     }  
  29.   
  30.     protected void Button1_Click(object sender, EventArgs e)  
  31.     {  
  32.         if (testFileLines != null && testFileLines.Count > 0)  
  33.         {  
  34.             var randomLine = testFileLines[random.Next(testFileLines.Count)];  
  35.             Label1.Text = randomLine;  
  36.             testFileLines.Remove(randomLine);  
  37.         }  
  38.     }  
  39. }  

Answers (2)