Problem assigning selectedItem.Value to a dropdownlist filled with OnItemDataBound

A

Antonio D'Ottavio

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
 
A

Antonio D'Ottavio

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

Antonio D'ottavio
www.etantonio/en






Antonio D'Ottavio said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top