DropDownList not accepting the SelectedIndex im trying to assign

AnnaB

New Member
I'm having a hard time figuring this out and I hope you guys would helpe me.I have an Index.aspx page with a DropDownList that is a separated UserControl class ('cause it'll be used in other pages). Here's the code for that:UcSelecionarLocal.ascx\[code\]<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UcSelecionarLocal.ascx.cs" Inherits="QuickMassage.uc.UcSelecionarLocal" %><asp:DropDownList ID="ddlLocais" runat="server" CssClass="span4 dropdown-toggle" AutoPostBack="true"></asp:DropDownList>\[/code\]UcSelecionarLocal.ascx.cs\[code\]public partial class UcSelecionarLocal : UserControl { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { PreencherLocais(); } } private void PreencherLocais() { ddlLocais.Items.Clear(); ddlLocais.Items.Add(new ListItem("Selecione", "0")); ControleLocal controle = new ControleLocal(); DataTable tab = controle.ListarLocais(); foreach (DataRow row in tab.Rows) { ddlLocais.Items.Add(new ListItem(row["Descricao"].ToString(), row["ID"].ToString())); } }\[/code\]}This control is placed in Index.aspx and loads its values correctly. The form that it's contained, has the action set to agendamentos.aspx. When I change the ddlist, the page is submitted to the form's action page, as it should be.On the other page the problems begin. I get the parametes posted to this page and one of them is the ddlist value. In immediate window i check the value and it's there, let's say it's 1.To make long story short, I have this code:agendamentos.aspx.cs\[code\]protected void Page_Load(object sender, EventArgs e) { DropDownList locais = ObterComponenteListaLocais(); try { locais.SelectedIndex = int.Parse(HttpContext.Current.Request["ucSelLocal$ddlLocais"]); }\[/code\]Debugging I see that locais.SelectedIndex is -1. After the assignment it remains -1. The page loads and then I change the ddlist value again to 2. When debugging the same code above, I see that the locais.SelectedIndex is now 1. Again, setting it to 2, as it would normally be, produces no effects. If I change the ddlist again to 3, the selectedidex comes as 2 and does not take the 3.In other words: the value of the index in a newly loaded page is the value of the page that was loaded before.Could you guys help me??TIA! regards
 
Back
Top