Husnain Khan Husnain Khan - 3 months ago 9
ASP.NET (C#) Question

Form get blank when Page_Load function is called?

I am facing a problem in my form. There are two text field and then there is drop-down menu, which link to a database.

When I click on the drop down menu, my two text fields are getting empty.

protected void OnChange_Acdemics(object Sender, EventArgs e)
{
DropDownList list = (DropDownList)Sender;
string value = (string)list.SelectedValue;

// degrees_dropdown.Visible = true;
try
{

// String query = "SELECT Degree_types.detail,Degree_Detail.GPA FROM Degree_Detail INNER JOIN Degree_types ON Degree_Detail.Degree_tilte = Degree_types.Degree_title where Degree_types.degree_type = '" + value + "';";
String query = "Select detail from Degree_Detail where id=" + int.Parse(value) + ";";
Dt = dbComm.GetDataTable(query);
Degree_Selection.DataTextField = "detail";
// Degree_Selection.DataValueField = "GPA";
Degree_Selection.DataSource = Dt;
Degree_Selection.DataBind();


}
catch (Exception ex)
{

}
}

Answer

You need to take care of this by using Page.IsPostBack:

if(!Page.IsPostBack)
{
   // load page 
}
else
{
   // check input values and set it again
}