Dear all,
I am having problem with placing the proper condition inside the postback method. My Dropdown list value is not retaining on page post back. I am trying to do the following:
1. There are 2 dropdown lists in my program.
2. I have two DropDownLists databound on the webform. The autopostback of the first dropdown list control is set to true. The contents of the first dropdown list is hardcoded. The contents of the second depend on the selected item in the first. In other words: The selectedvalue of the first DropDownList is passed as a parameter for the query at the basis of the second DropDownList's datasource. And this works: Whenever I click on an item in the first DropDownList, the dependant items are shown in the second DropDownList.
3. If I click submit then the specific rows from the DB will be pulled based on the values selected from dropdown list 1 and dropdown list 2.
Problem: In the postback method I think all the times the "ANY" MLNO is selected instead of selecting a specific value from the dropdown list 2. And that's why always all the values are pulled from the DB.
The code is as follows:
Code:
using System.Data;
private void Page_Load(object sender, System.EventArgs e)
{
if (this.IsPostBack)
{
getMLNO(Specific_Query_DDL.SelectedItem.Value); //This is where I am generating all the values from the DB for the second dropdown list. And as it's getting post back so even if I click submit button then the page gets post back and the selected value for the second dropdown list gets set to the default value, which is ANY
}
}
private void getMLNO(string selectedvalue) //This is the method where all the MLNOs are generated
{
if(selectedvalue=="ResistantL")
{
string sql= "SELECT re_MLNO, MAX(re_SpecimenDate)-MIN(re_SpecimenDate) AS DaysSeronegative FROM ml_hiv_status WHERE (re_HIV1_Status=0) GROUP BY re_MLNO";
OleDbConnection connection = new OleDbConnection(HIV.Database.DataConstants.CONNECTION_STRING);
OleDbDataAdapter adapter = new OleDbDataAdapter();
OleDbCommand command = new OleDbCommand(sql, connection);
DataSet ds = new DataSet();
adapter.SelectCommand = command;
if (adapter.Fill(ds) > 0)
{
DataView view = ds.Tables[0].DefaultView;
mlno_DDL.DataSource = view;
mlno_DDL.DataValueField="re_MLNO";
mlno_DDL.DataTextField = "re_MLNO";
mlno_DDL.DataBind();
resultsLabel.Visible= true;
mlno_DDL.Items.Insert(0, "ANY");
}
connection.Close();
}
}
private void mlno_DDL_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void submitButton_Click(object sender, System.EventArgs e) //SubmitButton_Click method fetches all the data from the Database depending on the 2 values selected from the 2 dropdown list
{
if(Specific_Query_DDL.SelectedItem.Value=="ResistantL")
{
if(mlno_DDL.SelectedItem.Value=="ANY") //All the values from the Db are pulled if ANY is chosen from the second DDL
{
executeResistantListQuery_ANY();
}
else if(mlno_DDL.SelectedItem.Value!="ANY") //Data are pulled if a specific MLNO is chosen from the DB, and this one is not working as always ANY value is chosen from the postback method
{
executeResistantListQuery_MLNO(mlno_DDL.SelectedItem.Value);
}
}
}
Thank you all who will try to solve the problem. To me it seems that the the second dropdown list is getting refreshed everytime I am posting the page back/submit and that's why "ANY" is getting selected and thus it's pulling all the data.