Reputation: 79
I have a asp:datalist that allows users to go into edit mode to edit data. I have a dropdownlist within the edititemtemplate and am trying to set the selected value based on the field from the datasource but dont know how to do this. Here is a sample of my asp.net code:
<asp:DataList ID="dlOtherSubjects" runat="server" DataKeyField="ID" EnableViewState="True"
OnEditCommand="Edit_Command"
OnUpdateCommand="Update_Command"
OnCancelCommand="Cancel_Command"
OnDeleteCommand="Delete_Command"
Width="700">
<ItemTemplate>
<table width="700" cellspacing="2" cellpadding="2">
<tr>
<td width="350" class="Label">Type</td>
<td width="350"><%#Eval("Type")%></td>
</tr>
</table>
<table width="700">
<tr>
<td align="left">
<asp:ImageButton ImageUrl="images/Editbutton.png" CommandName="Edit"
Runat="server" ID="lbedit" />
<asp:ImageButton ImageUrl="images/Deletebutton.png" CommandName="Delete"
Runat="server" ID="lbdelete" />
</td>
</tr>
</table>
</ItemTemplate>
<EditItemTemplate>
<table width="700" bgcolor="#BFD8D9" cellspacing="2" cellpadding="2">
<tr>
<td class="Label">Type</td>
<asp:DropDownList ID="ddlEType" runat="server">
<asp:ListItem Value="Household/Family Member" Text=" Family/Household Member"/>
<asp:ListItem Value="Significant Other" Text="Significant Other (Non Household)"/>
</asp:DropDownList>
</td>
</tr>
</table>
<br />
<table width="700">
<tr>
<td align="left">
<asp:ImageButton ImageUrl="images/Updatebutton.png" CommandName="Update"
Runat="server" ID="lbupdate" />
<asp:ImageButton ImageUrl="images/Cancelbutton.png" CommandName="Cancel"
Runat="server" ID="lbcancel" />
</td>
</tr>
</table>
</EditItemTemplate>
</asp:DataList>
I found an example where this was done in a gridview using the OnRowDataBound to call a sub which then sets the value of the dropdownlist (see here) but is there a similar OnRowDataBound object for a datalist?
How can I achieve this in a datalist? I will also need to be able to do something similar for an asp:radiobuttonlist.
Any help will be greatly appreciated. Thanks
Upvotes: 0
Views: 2241
Reputation: 1505
Have you tried DataList OnItemDataBound?
<asp:DataList ID="dlOtherSubjects" runat="server" DataKeyField="ID" EnableViewState="True"
OnEditCommand="Edit_Command"
OnUpdateCommand="Update_Command"
OnCancelCommand="Cancel_Command"
OnDeleteCommand="Delete_Command"
Width="700"
OnItemDataBound="Item_Bound"
>
protected void Item_Bound(Object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
var yourLabel = (Label)e.Item.FindControl("Label");
//add your logic
}
}
Upvotes: 1