Reputation: 2215
I am using some dropdowns in a gridview and have been able to convert where the dropdown would say "Monday"(for a bit of user friendliness) to just a simple "1" on insert(so I can easily do sql date time querys). Now I need to be able to do the reverse for the edit template on the gridview. When I hit edit now I get the error "The selected value doesnt exist in the control". I understand that the reason for this is because the value is a 1
instead of Monday
. Here is my failed attempt to solve the problem:
protected void EditFoodItem(object sender, GridViewEditEventArgs e)
{
string Day = ((DropDownList)gvMainView.FooterRow.FindControl("ddlDay")).SelectedValue;
switch (Day)
{
case "1":
Day = "Monday";
break;
case "2":
Day = "Tuesday";
break;
case "3":
Day = "Wednesday";
break;
case "4":
Day = "Thursday";
break;
case "5":
Day = "Friday";
break;
case "6":
Day = "Saturday";
break;
case "7":
Day = "Sunday";
break;
}
}
If the value in the gridview is 1
How can I get the selected value to go to Monday
...this seems rather simple but it is alluding me
Here is the control
<EditItemTemplate>
<asp:DropDownList ID="ddlDay" SelectedValue='<%# Bind("Day") %>' Text='<%# Bind("Day") %>' runat="server">
<asp:ListItem>Monday</asp:ListItem>
<asp:ListItem>Tuesday</asp:ListItem>
<asp:ListItem>Wednesday</asp:ListItem>
<asp:ListItem>Thursday</asp:ListItem>
<asp:ListItem>Friday</asp:ListItem>
<asp:ListItem>Saturday</asp:ListItem>
<asp:ListItem>Sunday</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
Upvotes: 0
Views: 5469
Reputation: 31228
You don't need to do this in code; the ListItem
has both a Text
and a Value
property:
<asp:DropDownList ID="ddlDay" runat="server" SelectedValue='<%# Bind("Day") %>'>
<asp:ListItem Value="1" Text="Monday" />
<asp:ListItem Value="2" Text="Tuesday" />
<asp:ListItem Value="3" Text="Wednesday" />
<asp:ListItem Value="4" Text="Thursday" />
<asp:ListItem Value="5" Text="Friday" />
<asp:ListItem Value="6" Text="Saturday" />
<asp:ListItem Value="7" Text="Sunday" />
</asp:DropDownList>
The SelectedValue
will return the day number, and the list will display the day name.
Upvotes: 2