Problem assigning selectedItem.Value to a dropdownlist filled with OnItemDataBound

Discussion in 'ASP .Net' started by Antonio D'Ottavio, Sep 19, 2005.

  1. Good morning,
    I've a problem with a dropdownlist located inside any row of a datalist,
    I fill both datalist and dropdownlist at runtime, the problem is with the
    dropdownlist infact using the event OnItemDataBound I can fill it but it is
    impossible for me to load the right selectedItem.Value , infact looking at
    the html page produced by the server I've this strange code :

    <select name="MyDataCampi:_ctl1:ComboTipoPartita"
    id="MyDataCampi__ctl1_ComboTipoPartita">
    <option selected="selected" value="1">Calcetto</option>
    <option value="2">Calciotto</option>
    <option value="3">Calcio</option>

    and also

    <option selected="selected" value="3">Calcetto</option>
    <option value="2">Calciotto</option>
    <option value="3">Calcio</option>

    while I expect something like selected = "true" and the value of Calcetto
    always "1".

    If you want please take a look at the code that produce this strange
    behaviour and try to help me,

    with this I create the dataset for the combobox and put it in the Session:

    void CreateDataViewComboBoxes() {
    OleDbconn = new OleDbConnection(sConnessioneDb);
    OleDbconn.Open();
    DataSet dsComboBoxes = new DataSet();
    dsComboBoxes.Tables.Add("TipoPartita");
    OleDbDataAdapter daTipoPartita = new OleDbDataAdapter("SELECT
    IDTipoPartita, DescrizioneTipoPartita from TipoPartita " ,OleDbconn);
    daTipoPartita.Fill(dsComboBoxes,"TipoPartita");
    Session["dsComboBoxes"] = dsComboBoxes;
    dsComboBoxes.Dispose();
    OleDbconn.Close();
    }

    and with this I fill the dropdownlist:

    void BindComboes(Object sender, DataListItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.Item ||
    e.Item.ItemType == ListItemType.AlternatingItem ||
    e.Item.ItemType == ListItemType.EditItem
    )
    {
    DataSet dsComboBoxes = (DataSet)Session["dsComboBoxes"] ;
    String sIDTipoPartita =
    ((HtmlInputHidden)e.Item.FindControl("HiddenIDTipoPartita")).Value;
    DropDownList ComboTipoPartita =
    (DropDownList)e.Item.FindControl("ComboTipoPartita");
    ComboTipoPartita.DataValueField = "IDTipoPartita";
    ComboTipoPartita.DataTextField = "DescrizioneTipoPartita";
    ComboTipoPartita.DataSource =
    dsComboBoxes.Tables["TipoPartita"];
    ComboTipoPartita.DataBind();
    ComboTipoPartita.SelectedItem.Value = sIDTipoPartita;
    dsComboBoxes.Dispose();
    }
    }


    and this is the declaration of the dataset :

    <ASP:DataList id="MyDataCampi" runat="server" ...
    OnItemDataBound="BindComboes" >
    <ItemTemplate>
    <tr style="background-color:CCFF99">
    <td>
    <input id="HiddenIDTipoPartita" type="hidden" value='<%#
    DataBinder.Eval(Container.DataItem, "IDTipoPartita") %>' runat="server" />
    <asp:DropDownList id="ComboTipoPartita" Enabled="true"
    runat="server"></asp:DropDownList>
    </td>
    </tr>
    </ItemTemplate>

    If you have any suggest I'll be happy of this,
    many thanks ...

    Antonio D'Ottavio
    www.etantonio.it/en
    Antonio D'Ottavio, Sep 19, 2005
    #1
    1. Advertising

  2. Yes you're right and solved my problem,
    many thanks

    Antonio D'ottavio
    www.etantonio/en






    "Antonio D'Ottavio" <> ha scritto nel messaggio
    news:...
    > Good morning,
    > I've a problem with a dropdownlist located inside any row of a datalist,
    > I fill both datalist and dropdownlist at runtime, the problem is with the
    > dropdownlist infact using the event OnItemDataBound I can fill it but it

    is
    > impossible for me to load the right selectedItem.Value , infact looking at
    > the html page produced by the server I've this strange code :
    >
    > <select name="MyDataCampi:_ctl1:ComboTipoPartita"
    > id="MyDataCampi__ctl1_ComboTipoPartita">
    > <option selected="selected" value="1">Calcetto</option>
    > <option value="2">Calciotto</option>
    > <option value="3">Calcio</option>
    >
    > and also
    >
    > <option selected="selected" value="3">Calcetto</option>
    > <option value="2">Calciotto</option>
    > <option value="3">Calcio</option>
    >
    > while I expect something like selected = "true" and the value of Calcetto
    > always "1".
    >
    > If you want please take a look at the code that produce this strange
    > behaviour and try to help me,
    >
    > with this I create the dataset for the combobox and put it in the Session:
    >
    > void CreateDataViewComboBoxes() {
    > OleDbconn = new OleDbConnection(sConnessioneDb);
    > OleDbconn.Open();
    > DataSet dsComboBoxes = new DataSet();
    > dsComboBoxes.Tables.Add("TipoPartita");
    > OleDbDataAdapter daTipoPartita = new OleDbDataAdapter("SELECT
    > IDTipoPartita, DescrizioneTipoPartita from TipoPartita " ,OleDbconn);
    > daTipoPartita.Fill(dsComboBoxes,"TipoPartita");
    > Session["dsComboBoxes"] = dsComboBoxes;
    > dsComboBoxes.Dispose();
    > OleDbconn.Close();
    > }
    >
    > and with this I fill the dropdownlist:
    >
    > void BindComboes(Object sender, DataListItemEventArgs e)
    > {
    > if (e.Item.ItemType == ListItemType.Item ||
    > e.Item.ItemType == ListItemType.AlternatingItem ||
    > e.Item.ItemType == ListItemType.EditItem
    > )
    > {
    > DataSet dsComboBoxes = (DataSet)Session["dsComboBoxes"]

    ;
    > String sIDTipoPartita =
    > ((HtmlInputHidden)e.Item.FindControl("HiddenIDTipoPartita")).Value;
    > DropDownList ComboTipoPartita =
    > (DropDownList)e.Item.FindControl("ComboTipoPartita");
    > ComboTipoPartita.DataValueField = "IDTipoPartita";
    > ComboTipoPartita.DataTextField = "DescrizioneTipoPartita";
    > ComboTipoPartita.DataSource =
    > dsComboBoxes.Tables["TipoPartita"];
    > ComboTipoPartita.DataBind();
    > ComboTipoPartita.SelectedItem.Value = sIDTipoPartita;
    > dsComboBoxes.Dispose();
    > }
    > }
    >
    >
    > and this is the declaration of the dataset :
    >
    > <ASP:DataList id="MyDataCampi" runat="server" ...
    > OnItemDataBound="BindComboes" >
    > <ItemTemplate>
    > <tr style="background-color:CCFF99">
    > <td>
    > <input id="HiddenIDTipoPartita" type="hidden" value='<%#
    > DataBinder.Eval(Container.DataItem, "IDTipoPartita") %>' runat="server" />
    > <asp:DropDownList id="ComboTipoPartita" Enabled="true"
    > runat="server"></asp:DropDownList>
    > </td>
    > </tr>
    > </ItemTemplate>
    >
    > If you have any suggest I'll be happy of this,
    > many thanks ...
    >
    > Antonio D'Ottavio
    > www.etantonio.it/en
    >
    >
    >
    >
    Antonio D'Ottavio, Sep 19, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer
    Replies:
    5
    Views:
    8,067
    maysam
    Mar 11, 2008
  2. Julius Fenata
    Replies:
    6
    Views:
    16,690
    David Jessee
    Jan 17, 2005
  3. HH
    Replies:
    2
    Views:
    16,001
    tom pester
    Aug 14, 2005
  4. Andy B.
    Replies:
    2
    Views:
    1,726
    Andy B.
    Apr 1, 2009
  5. Antonio D'Ottavio
    Replies:
    1
    Views:
    205
    Antonio D'Ottavio
    Sep 19, 2005
Loading...

Share This Page