Home > Archive > ASP .NET > February 2006 > DropDownList in DataList
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
DropDownList in DataList
|
|
|
| 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
| |
| Eliyahu Goldin 2006-02-28, 7:58 am |
| 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
"Kevin" <@> wrote in message news:epF9LFGPGHA.2924@TK2MSFTNGP11.phx.gbl...
> 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
>
>
| |
| Sonu Kapoor [MVP] 2006-02-28, 7:02 pm |
| 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
---
Posted via www.DotNetSlackers.com
|
|
|
|
|