03 dropdownlist selectedindexchanged not firing in usercontrol on

C

cindy

I have a webform, click on link "one" and usercontrol page1.ascx loads.
Page1.ascx has the dropdrop list. The data loads when control first
displayed. The postback fires when item selected. The data does not do a
reload because of if !postback check. The value returned from the list is
always ""



Code from Page1.ascx

<asp:DropDownList EnableViewState="True" id="drpZone" AutoPostBack="True"
runat="server"></asp:DropDownList>

I am using session
protected string Zone
{
get
{
return Session["Zone"] as string;
}

set
{
Session["Zone"] = value;
}
}

Before the control renders if not postback I populate list with data
private void Page1_PreRender(object sender, EventArgs e)
{


Zone = drpZone.SelectedValue;

SearchBLL bll = new SearchBLL();
bll.ZoneData(this);

}

code from SearchBLL

public void ZoneData(Page1 pageControl)
{
SearchDAL dal = new SearchDAL();
pageControl.ZoneDataSource = dal.ZoneList();
}
code from SearchDAL
public DataSet ZoneList()
{
try
{
DataSet ds = new DataSet();
// Open database connection
SqlConnection connection = new SqlConnection("Data Source=tsbucontent;
Initial Catalog=sis_applications; uid=sa; pwd=1452hd;");
SqlCommand command = new SqlCommand();
// Create SQL command
string sql="SELECT ID,NAME FROM UserLU WHERE ACTIVE = '1' and TYPE =
'Zone'";
SqlDataAdapter adapter = new SqlDataAdapter();
command.CommandText = sql;
command.Connection = connection;
adapter.SelectCommand = command;
// Fill Dataset
adapter.Fill(ds);
return ds;
}
catch //Error occured
{
throw;
//return null;
}
}
List is populated from database
I select value and postback happens, code does not go back to database to
repopulate I checked it. The event selectedindexchanged is never fired

code on Page1.ascx
private void InitializeComponent()
{
this.drpZone.SelectedIndexChanged +=new
EventHandler(drpZone_SelectedIndexChanged);
this.btnRetrieve.Click += new System.EventHandler(this.btnRetrieve_Click);
this.dgResults.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgResults_ItemCommand);
this.Load += new System.EventHandler(this.Page_Load);
this.PreRender += new System.EventHandler(this.Page1_PreRender);


}


private void drpZone_SelectedIndexChanged(object sender, EventArgs e)
{

SearchBLL bll = new SearchBLL();
bll.LCTData(this);
}
 
S

sam

Put this code in PageLoad instead of PreRender and bind it every time
(don't check IsPostback)

SearchBLL bll = new SearchBLL();
bll.ZoneData(this);

If you want to detect the OnChangeEvent you have to databind the
dropdown every time in page load. How else could ASP.NET know that the
index has changed if it didn't have the old values to compare it to?

You have to bind it in page load becuase the OnChangeEvent fires after
page load but *before* PreRender. If you want pointers to
documentation on the page lifecycle let me know.

-Sam Robertson
 
S

Steven Cheng[MSFT]

Hi Cindy,

Have you got a chance to check the suggestions and test page in my previous
message? If there're anything else we can help, please feel free to post
here. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top