View Full Version : Problem setting which item is selected in a DropDownList when the page is loaded

Anne Huberman
02-01-2012, 10:55 PM

I'm using ReportViewer with asp.net 4.0 to display reports on the web. I have a DropDownList on a page that I'm using to redirect to other pages, so I need to have AutoPostBack for the DropDownList set to true so that the SelectedIndexChanged function is called.

I also want to set the initial selected item in the DropDownList when the page is first loaded (or returned to from other pages). I've tried putting this into Page_Load and only doing it when Page.IsPostBack == false, but it's still writing over the user's choice when the user selects something.


02-02-2012, 08:33 AM
what does your

Page.IsPostBack == false

statement look like? I am guessing by the code you are doing VB and not C#
if VB should look something like this

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
dropdownlist1.SelectedIndex = 0
end If
end Sub

if C# should look like

protected void Page_Load(object sender, EventArgs e){
dropdownlist1.SelectedIndex = 0;

Anne Huberman
02-07-2012, 06:36 PM
I'm coding in C#. The code is

protected void Page_Load(object sender, EventArgs e)
if (Page.IsPostBack == false)
ddlGroupBy.Items[0].Selected = true;
ddlGroupBy.Items[1].Selected = false;
ddlGroupBy.Items[2].Selected = false;

I have since found another way around this problem: instead of having 3 aspx pages to go to 3 reportviewers that all take the same data inputs, I set the reportviewer's LocalReport.ReportPath to whichever reportviewer I want.

The odd thing about this is: when I went back to using 3 aspx pages to match my orignial question, I no longer had the problem where Load was getting called with IsPostBack = false when it was overwriting the user's choice! Thanks for your help!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum