المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Dropdown list value is not retaining on page post back



C# Programming
08-31-2009, 08:51 PM
Dear all,
I am having problem with placing the proper condition inside the postback method. 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;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
using System.Globalization;


namespace HIV
{
///
/// Summary description for run_specific_query.
///
public class run_specific_query : System.Web.UI.Page
{
protected HIV.Controls.NavMenu navMenu;
protected HIV.Controls.NavSubMenu navSubMenu;
protected System.Web.UI.HtmlControls.HtmlGenericControl message;
protected System.Web.UI.WebControls.Label resultsLabel;
protected System.Web.UI.WebControls.DropDownList Specific_Query_DDL;
protected System.Web.UI.WebControls.DropDownList mlno_DDL;
//protected System.Web.UI.WebControls.TextBox date_TB;
protected System.Web.UI.WebControls.CompareValidator dateValidator;
protected System.Web.UI.WebControls.DataGrid resultsDatagrid;
protected System.Web.UI.WebControls.Button submitButton;
protected System.Web.UI.WebControls.Label data_src;
protected System.Web.UI.WebControls.LinkButton exportLinkbutton;

private void Page_Load(object sender, System.EventArgs e)
{
navMenu.SelectedMainItem = HIV.Controls.NavMenu.MainItems.QUERY;
navSubMenu.SelectedMainItem = HIV.Controls.NavMenu.MainItems.QUERY;
navSubMenu.SelectedSubItem = HIV.Controls.NavSubMenu.SubItems.RUN_SPECIFIC_QUERY;

if (this.IsPostBack)
{
if(Specific_Query_DDL.SelectedItem.Value=="ResistantL")
{
getMLNO(Specific_Query_DDL.SelectedItem.Value);
}
if(Specific_Query_DDL.SelectedItem.Value=="NegativeL")
{
getMLNO(Specific_Query_DDL.SelectedItem.Value);
}
if(Specific_Query_DDL.SelectedItem.Value=="PositiveL")
{
getMLNO(Specific_Query_DDL.SelectedItem.Value);
}
if(Specific_Query_DDL.SelectedItem.Value=="DeadL")
{
getMLNO(Specific_Query_DDL.SelectedItem.Value);
}

//getMLNO();
}

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
BuildQueryForm();
InitializeComponent();
base.OnInit(e);
}

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.exportLinkbutton.Click += new System.EventHandler(this.exportLinkbutton_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

protected void BuildQueryForm()
{
try
{
this.submitButton.Click += new System.EventHandler(this.submitButton_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
catch(Exception e)
{
message.InnerHtml = "Exception: " + e.ToString();
}
}

private void getMLNO(string selectedvalue)
{
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)
{
foreach (DataRow r in ds.Tables[0].Rows)
{
if(Convert.ToInt32(r["DaysSeronegative"]) < 2555)
{
r.Delete();
}
}
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();
}
if(selectedvalue=="NegativeL")
{
string sql= "SELECT re_MLNO,sum(re_HIV1_Status) AS hivNeg, MAX(re_SpecimenDate)- MIN(re_SpecimenDate) AS DaysSeronegative FROM ml_hiv_status 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)
{
foreach (DataRow r in ds.Tables[0].Rows)
{
if(Convert.ToInt32(r["hivNeg"]) != 0)
{
r.Delete();
}
}
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();
}
if(selectedvalue=="PositiveL")
{
string sql= "SELECT DISTINCT re_MLNO FROM ml_hiv_status WHERE (re_HIV1_Status=1 AND re_HIV2_Status=1)";

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();
}
if(selectedvalue=="DeadL")
{
string sql= "SELECT DISTINCT re_MLNO FROM ml_hiv_status WHERE (re_Status=0)";

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 listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void submitButton_Click(object sender, System.EventArgs e)
{
if(Specific_Query_DDL.SelectedItem.Value=="default")
{
resultsLabel.Visible= true;
resultsLabel.Text ="Please select a specific query from the dropdown list";
exportLinkbutton.Visible = false;
}
if(Specific_Query_DDL.SelectedItem.Value=="ResistantL")
{
if(mlno_DDL.SelectedItem.Value=="ANY")
{
executeResistantListQuery_ANY();
}
else if(mlno_DDL.SelectedItem.Value!="ANY")
{
executeResistantListQuery_MLNO(mlno_DDL.SelectedItem.Value);
}
}
if(Specific_Query_DDL.SelectedItem.Value=="NegativeL")
{
if(mlno_DDL.SelectedItem.Value=="ANY")
{
executeNegativeListQuery_ANY();
}
else if(mlno_DDL.SelectedItem.Value!="ANY")
{
executeNegativeListQuery_MLNO(mlno_DDL.SelectedItem.Value);
}
}
if(Specific_Query_DDL.SelectedItem.Value=="PositiveL") //Positive list starts here
{

if(mlno_DDL.SelectedItem.Value=="ANY")
{

executePositiveListQuery_ANY();
}
else if(mlno_DDL.SelectedItem.Value!="ANY")
{
executePositiveListQuery_MLNO(mlno_DDL.SelectedItem.Value);
}
} //Positive list ends here
if(Specific_Query_DDL.SelectedItem.Value=="DeadL")
{
resultsLabel.Text ="you have selected "+ mlno_DDL.SelectedItem.Value;
if(mlno_DDL.SelectedItem.Value=="ANY")
{
executeDeadListQuery_ANY();
}
else if(mlno_DDL.SelectedItem.Value!="ANY")
{
executeDeadListQuery_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 *******ed everytime I am posting the page back/submit and that's why "ANY" is getting selected and thus it's pulling all the data.