0

我在gridview中使用了一些下拉菜单,并且已经能够将下拉菜单中的“星期一”(为了用户友好)转换为插入时的简单“1”(这样我就可以轻松地进行sql日期时间查询) . 现在我需要能够对 gridview 上的编辑模板执行相反的操作。当我现在点击编辑时,我收到错误“控件中不存在所选值”。我知道这样做的原因是因为值是 a1而不是Monday. 这是我解决问题的失败尝试:

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;
        }
}

如果 gridview 中的值是1How can I get the selected value to go to Monday...这似乎很简单,但它暗指我

这是控制

<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> 
4

1 回答 1

2

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.

于 2013-06-11T18:32:05.687 回答