Dropdown list value is not retaining on page post back [modified]
-
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>
/// Summary description for run_specific_query.
/// </summary>
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; navSubMe
-
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>
/// Summary description for run_specific_query.
/// </summary>
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; navSubMe
Yes.. the culprit is your page load method.. Just for every post-back, even when submit button is clicked, ASP.NET first calles Page_Load and after its execution it calles the general event handler. Just omit the lines
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);
}
}and place them in
Listbox1_selectedIndexChanged
event. I thin as that is called specific when postback occurs on selected indexchanged event, the submit will not load the 2nd dropdown onsubmit. The problem will be fixed then... :thumbsup::thumbsup::cool:Abhishek Sur
My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB
**Don't forget to click "Good Answer" if you like to.
-
Yes.. the culprit is your page load method.. Just for every post-back, even when submit button is clicked, ASP.NET first calles Page_Load and after its execution it calles the general event handler. Just omit the lines
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);
}
}and place them in
Listbox1_selectedIndexChanged
event. I thin as that is called specific when postback occurs on selected indexchanged event, the submit will not load the 2nd dropdown onsubmit. The problem will be fixed then... :thumbsup::thumbsup::cool:Abhishek Sur
My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB
**Don't forget to click "Good Answer" if you like to.
Thank you Mr. Sur. I knew that the culprit is the page load method.. but didn't know how to populate the second dropdown list without the pageload. Should I put the code you mentioned exactly inside the Specific_Query_DDL_selectedIndexChanged function ? And should I call that function inside the page_Load function? Please suggest. Thanks in advance.
-
Thank you Mr. Sur. I knew that the culprit is the page load method.. but didn't know how to populate the second dropdown list without the pageload. Should I put the code you mentioned exactly inside the Specific_Query_DDL_selectedIndexChanged function ? And should I call that function inside the page_Load function? Please suggest. Thanks in advance.
No.. You dont need to call
selectedIndexChanged
function frompage_load
, it will automatically been called. Rather than that, you just writeif(IsPostBack) return;
in the page_load, to eliminate any code to be executed inpage_load
during postbacks. When theselectedIndexChanged
is posting back the page to the server, it always calls the Page_Load event first, then its original event handler. So you just need to suppress page_load during event calls. Hope you got it clearly.Abhishek Sur
My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB
**Don't forget to click "Good Answer" if you like to.
-
No.. You dont need to call
selectedIndexChanged
function frompage_load
, it will automatically been called. Rather than that, you just writeif(IsPostBack) return;
in the page_load, to eliminate any code to be executed inpage_load
during postbacks. When theselectedIndexChanged
is posting back the page to the server, it always calls the Page_Load event first, then its original event handler. So you just need to suppress page_load during event calls. Hope you got it clearly.Abhishek Sur
My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB
**Don't forget to click "Good Answer" if you like to.
Abhishek Sur wrote:
if(IsPostBack) return; in the page_load, to eliminate any code to be executed in page_load during postbacks.
This is nuts. It may work for the code he has today, but it's a terrible paradigm. Why should one not write code in page load that needs to run every time ? Instead, put if (!IsPostback) blocks around the specific code you need to stop running
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
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>
/// Summary description for run_specific_query.
/// </summary>
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; navSubMe
Yes, this is still far too much code to be posting. You don't need to be reloading the data every time, unless you turn off viewstate. If you do need to, do it in page_prerender, that's the right place to do it anyhow. Then it will happen AFTER your event fires.
skhan17 wrote:
if(Specific_Query_DDL.SelectedItem.Value=="default")
Hard coded strings are a bad idea. can't you store an enum or something in the value ?
skhan17 wrote:
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);
SQL in the presentation layer is always a sign of no design. I'd fire anyone who presented code like this to me.
skhan17 wrote:
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); }
Hard coded strings are still bad. And, have you never heard of the || operator ?
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Abhishek Sur wrote:
if(IsPostBack) return; in the page_load, to eliminate any code to be executed in page_load during postbacks.
This is nuts. It may work for the code he has today, but it's a terrible paradigm. Why should one not write code in page load that needs to run every time ? Instead, put if (!IsPostback) blocks around the specific code you need to stop running
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
Actually If you saw the code above, the person is populating the Dropdown from Page_load.
Christian Graus wrote:
Why should one not write code in page load that needs to run every time
Actually your question will get you the answer. Yes of course.. If one needs to run a code every time, he could have write this before the line, or if specific to the postback he could use it just within the if block. I think it depends upon the requirement. :-\ :-\ The code that he wrote doesnt require to populate dropdown during postbacks. so he should eliminate the function call during each postback as the page which is posted back already had the items in the select. :rose:
Abhishek Sur
My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB
**Don't forget to click "Good Answer" if you like to.