Problem with dropdownlist in a webpart

Ramón Almarza asked on December 4, 2014 12:53

Good morning,

I’ve trying to make a webPart that contains a form. This form contains many asp:DropDownList that are loaded in the code behind with elemements defined as properties of the webPart. This is the code to load the direfent dropdownLists:

protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { cargarLista(GetValue("tipoIncidente"), "ddlTipoIncidente", true); cargarLista(GetValue("centroTrabajo"), "ddlCentroTrabajo", true); cargarLista(GetValue("horaJornada"), "ddlHoraJornada", false); cargarLista(GetValue("turnoTrabajo"), "ddlTurnoTrabajo", true); cargarLista(GetValue("valoracionInicial"), "ddlvaloracion", true); cargarLista(GetValue("parejaIndividual"), "ddlIndividual", true); cargarLista(GetValue("situacionEPI"), "ddlDetalleSituacion", true); } }

private void cargarLista(object valores, string nombre, bool puedeVacio) { try { DropDownList listado = new DropDownList(); listado = (DropDownList)this.FindControl(nombre); if (puedeVacio) listado.Items.Add(new ListItem("Seleccione un valor...", "-1"));

        string[] duplas = valores.ToString().Split('\r');
        foreach (string elemento in duplas)
        {
            string valor = elemento.Split(';')[0];
            string descr = elemento.Split(';')[1];

            listado.Items.Add(new ListItem(descr, valor));
        }
    }
    catch
    {
    }

}

The lists are loaded correctly with this function, but when I try to take the selected value of any of this list on server side (for example when a buttom is clicked) always take the first value of the list, never the selected Item. This is the code related with the buttom:

<asp:Button ID="btnEnviar" runat="server" CausesValidation="true" ValidationGroup="grupoEnviar" Text="Enviar" Tooltip="Enviar" CssClass="boton_4" onclick="btnEnviar_Click" />

protected void btnEnviar_Click(object sender, EventArgs e) { lblResp.Text = "Enviado centro trabajo: " + ddlCentroTrabajo.SelectedValue.ToString() ; }

The value of ddlCentroTrabajo.SelectedValue is allways the first element of the list.

Why I can´t recover the selected item??

Thanks in advance

Recent Answers


Brenden Kehren answered on December 4, 2014 13:11

Webparts have different page lifecycles than you are used to with standard asp.net controls. You need to load and set the properties of the controls within the OnContentLoaded() event unless you have a specific reason to perform it later in the lifecycle. You also need to set them on every postback. If you're concerned with reloading data over and over, no worries, Kentico will cache that for you as needed.

Try moving your code to this event:

public override void OnContentLoaded()
{
    base.OnContentLoaded();
    // do your work here
}

Also ensure your control is inheriting the CMSAbstractWebpart class.

0 votesVote for this answer Mark as a Correct answer

Ramón Almarza answered on December 4, 2014 14:24

Hi Brenden,

Firstly, thanks for your response.

I've done that you told me. I've moved the code for loadind the diferent dropdownlists into the OnContentLoaded() event, and I've tested that the control is inheriting from CMSAbstractWebPart.

...

public partial class CMSWebParts_Babel_EMT_insertarIncidentes : CMSAbstractWebPart

...

public override void OnContentLoaded() { base.OnContentLoaded(); SetupControl(); cargarLista(GetValue("tipoIncidente"), "ddlTipoIncidente", true); cargarLista(GetValue("centroTrabajo"), "ddlCentroTrabajo", true); cargarLista(GetValue("horaJornada"), "ddlHoraJornada", false); cargarLista(GetValue("turnoTrabajo"), "ddlTurnoTrabajo", true); cargarLista(GetValue("valoracionInicial"), "ddlvaloracion", true); cargarLista(GetValue("parejaIndividual"), "ddlIndividual", true); cargarLista(GetValue("situacionEPI"), "ddlDetalleSituacion", true); }

But this hasn't solved the problem. So, when I try to recover on the server side the selected value of any dropdownlist, this value is allways the first value of the list. Besides, the recharged page show the first element of each dropdownlist as selected element.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on December 4, 2014 15:17

I see what you're trying to accomplish now and I apoligize for my miscommunication, you need to set the data retrieval methods typically in the OnContenLoaded event and if you set any drop down values, do those after the data has loaded in the PageLoad event. I'm not sure what our cargarLista function does either so that leaves some mystery.

0 votesVote for this answer Mark as a Correct answer

Ramón Almarza answered on December 4, 2014 17:49

"cargarLista" function only loads dinamically the dropdownlists with values that are obtained like properties of the webpart.

I've tested to load the dropdownlists statically in the .ascx file, with a code similar to this one:

<asp:DropDownList ID="ddlCentroTrabajo" runat="server" CssClass="textField" EnableViewState="True" ViewStateMode="Enabled"> <asp:ListItem Text="Uno" Value="uno"></asp:ListItem> <asp:ListItem Text="dos" Value="dos"></asp:ListItem> <asp:ListItem Text="tres" Value="tres"></asp:ListItem> </asp:DropDownList>

and this works fine: I can take the selected value in the server side and when the page is recharged, the selected value is not the first one, it's the value that I'd selected.

Besides, I have tested to copy the same form into a System.Web.UI.Page object, and it works perfectly too. So I think that the problem is related with the CMSAbstractWebPart class, but I don't know exactly why.

Could you help me, please?

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on December 4, 2014 20:02

As I mentioned before webparts have a whole different page lifecycle than a standard aspx or ascx control. If you have a user control already created you can use the User Control webpart to place it on a page. This would probably be the fastest fix for you vs. trying to understand how Kentico's page lifecycle works.

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.