|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
03 dropdownlist selectedindexchanged not firing in usercontrol onPage1.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); } -- cindy mello 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 Thanks for Sam's input,
Hi Cindy, As for dynamically created ascx Usercontrol in ASP.NET page, and the postback events for controls inside the UserControl, we need to pay attention to the following things: 1. Since you dynamically create the control in the button's click post back event, we need to create/load that user control everytime the page load in the sequential requests(postbacks). Also, we need to explicitly assign an ID for that usercontrol. 2. For other controls in the UserControl(ascx), we don't need to rebind the data everytime, just in (!IsPostBack) is enough and the postback events should be get fired correctly as normal controls in page. Anyway, I've made a simple page which dynamically created a UserControl which contains a dropdownlist with some data(binded at the first time it is created), you can have a look to see whether it can help provide some clues. I've attached the files in the message's attachement, if you view it through OE, you can get it directly. If there're any problems getting it. Please feel free to post here. Hope helps. Thanks, Regards, Steven Cheng Microsoft Online Support Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.) 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.)
Other interesting topics
Why no richtextbox in Web Controls?
Mystery ASP.NET Error ImageButton bug AddHandler doesn't seem to work. Blinking Controls htmlinputfile - accept Gridview, using Callbacks when Sorting and Paging dataformatstring for dates in datagrid Dyamically add user control, along with properties? can you convert an in-memory bitmap render in a web control image ? |
|||||||||||||||||||||||