DropDownList in DataList

K

Kevin

Hi
I need to get the "ItemId" from the selected value of the DropDownList in a
DataList, but i don't known where tu put the <%#
DataBinder.Eval(Container.DataItem, "ItemId"), because if I try to put in
the ID of the DropDownList generate an error. I've used this code, i catch
the SelectedIndexChange.
HTML:

<asp:DataList id="dlDropDownList" runat="server">
<ItemTemplate>
<span><%# DataBinder.Eval(Container.DataItem, "ItemName")%></span>
<asp:DropDownList Runat="server" OnSelectedIndexChanged="SetRanking"
AutoPostBack="True">
<asp:ListItem>
1
</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:DataList>

Codebehind:
protected void SetRanking(object sender, System.EventArgs e)
{
DropDownList CurrentDDL;
int SelectedValue;

CurrentDDL = (DropDownList)sender;
SelectedValue = Convert.ToInt32(CurrentDDL.SelectedItem.Text);
//I need some way to get the ItemId to update
}

Thanks
 
E

Eliyahu Goldin

You don't need to databind to the selected value, you just get it in the
event handler as myDdl.SelectedValue, where myDdl is obtained as

DropDownList myDdl = sender as DropDownList;

(c# syntax), where sender is the parameter passed to the event handler.

Eliyahu
 
S

Sonu Kapoor [MVP]

If you are storing the ID in the datagrid DataKeyField field, then you can do the following to retrieve it:

// Get the item
DataGridItem iItem = sender.NamingContainer as DataGridItem;
// Get the ID from the item
int nID = dg.DataKeys(iItem.ItemIndex)
// do the update ...

Hope this helps.

Sonu Kapoor [MVP
w: www.DotNetSlackers.com
b: www.KapoorSolutions.com/blog
 

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,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top