By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,739 Members | 1,523 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,739 IT Pros & Developers. It's quick & easy.

Problem assigning selectedItem.Value to a dropdownlist filled with OnItemDataBound

P: n/a
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("HiddenIDTipo Partita")).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


Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
I think i see the problemo

the line:
ComboTipoPartita.SelectedItem.Value = sIDTipoPartita;

in the "CreateDataViewComboBoxes()" method should read:
ComboTipoPartita.Item.FindByValue(sIDTipoPartita). Selected = true;

Because the way your setting it, its actually resetting the value of the
selected item (the first in the list)

Hope that helps

"Antonio D'Ottavio" <po********@etantonio.it> wrote in message
news:u7*************@TK2MSFTNGP12.phx.gbl...
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("HiddenIDTipo Partita")).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

Nov 19 '05 #2

P: n/a
Yes you're right and solved my problem,
many thanks

Antonio D'ottavio
www.etantonio/en


"Antonio D'Ottavio" <po********@etantonio.it> ha scritto nel messaggio
news:u7*************@TK2MSFTNGP12.phx.gbl...
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("HiddenIDTipo Partita")).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

Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.