Christopher Costa
Christopher Costa

Reputation: 17

Change SelectedIndex of a DropDownList Dynamically added

protected DropDownList AddControldpd(int PrioridadeSelecionada)
{
    DropDownList prioridade = new DropDownList();
    prioridade.ID = PrioridadeID.ToString();
    PrioridadeID += 1;
    LoadPrioridade(prioridade);

    //Marcar Selecionados
    int ID;
    if (int.TryParse(HID.Value, out ID))
    {
        objTPTC.id = ID;
        objTPTC = objTPTCDAO.GetDetails(objTPTC, "B", PrioridadeSelecionada);
        if (objTPTC.id >= 0)
            prioridade.SelectedIndex = objTPTC.Prioridade;

    }
    DCHPrioriedade.Controls.Add(new LiteralControl("<label>Prioridade " + (PrioridadeID - 1).ToString() + "</label>"));
    DCHPrioriedade.Controls.Add(prioridade);
    DCHPrioriedade.Controls.Add(new LiteralControl("<br>"));
    return prioridade;
}

I'm using this function to add a new dropdownlist and im consulting an object to see what the the selectedIndex should be.

I can see in debuging that objTPTC.Prioridade = 1 and in Inspect Element

<option value="1" title="Filho de trabalhador da associação" carea="0">Filho de trabalhador da associação</option>

The value of the option i want to pre-select is also 1

The Problem is that the selected item is always this:

<option value="">Selecione uma opção...</option>

Upvotes: 0

Views: 266

Answers (1)

Subhash Sasidharakurup
Subhash Sasidharakurup

Reputation: 368

Your question is not clear. but i guess, your selectedindexchange method of dynamically added control is not working ?. if it is the problem, then to solve it attach postback=true to your dynamically added control. then only it will trigger an event when selected index change occurs.then you can bind a method to selectedindexchange event

Edit

<option value="" selected="selected">hai</option>
<option value="" >hello</option>

then the hai will be appeared as seleected

Upvotes: 1

Related Questions