Palash Kayal

Palash Kayal

  • NA
  • 5
  • 1.2k

Why this code Write down in Page_Load ?

Sep 12 2017 6:30 AM
  1. protected void Page_Load(object sender, EventArgs e)   
  2. {
  3.    // Why this code Load in Page_Load

  4. if (!Page.IsPostBack)
  5. {
  6. SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
  7. SqlCommand cmd = new SqlCommand("select * from tbl_country", con);
  8. SqlDataAdapter sda = new SqlDataAdapter(cmd);
  9. DataTable dt = new DataTable();
  10. sda.Fill(dt);
  11. DropDownList1.DataSource = dt;
  12. DropDownList1.DataBind();
  13. }
            // end Why this code Load in Page_Load
  1. }
  2. protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  3. {
  4. DropDownList2.Items.Clear();
  5. DropDownList2.Items.Add("Select State");
  6. SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
  7. SqlCommand cmd = new SqlCommand("select * from tbl_state where country_id=" + DropDownList1.SelectedItem.Value, con);
  8. SqlDataAdapter sda = new SqlDataAdapter(cmd);
  9. DataTable dt = new DataTable();
  10. sda.Fill(dt);
  11. DropDownList2.DataSource= dt;
  12. DropDownList2.DataBind();
  13. }
  14. protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
  15. {
  16. DropDownList3.Items.Clear();
  17. DropDownList3.Items.Add("Select State");
  18. SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
  19. SqlCommand cmd = new SqlCommand("select * from tbl_city where state_id=" + DropDownList2.SelectedItem.Value, con);
  20. SqlDataAdapter sda = new SqlDataAdapter(cmd);
  21. DataTable dt = new DataTable();
  22. sda.Fill(dt);
  23. DropDownList3.DataSource = dt;
  24. DropDownList3.DataBind();
  25. }