使用不同的文本下拉SelectedItem.Value

时间:2014-06-09 10:14:42

标签: c# asp.net

我有一个带有textm的下拉列表,但我想使用这个短语的一半我怎么能这么容易?我将展示我的代码,现在我可以获得全文。

我只想在文中花几个小时:1小时,24小时。我删除了文本。

示例:Text =“发送提醒前1小时”

所以我想这样:Text =“1hr”

我只想花一点时间,谢谢你们

<asp:DropDownList ID="reminderOptions" runat="server">
    <asp:ListItem Value="-1" Text="Don't send reminder" />
    <asp:ListItem Value="3600" Text="Send reminder 1hr before" />
    <asp:ListItem Value="86400" Text="Send reminder 24hrs before" />
</asp:DropDownList>
lblReminderSet.Text = reminderOptions.SelectedItem.Value;
lblReminderSet.Text = String.Format("a message sent to you {0} your lessons", reminderOptions.SelectedItem.Text);

1 个答案:

答案 0 :(得分:5)

我们假设我们使用正则表达式来拉出时间框架:

(\d+hr[s]?)

Regular expression visualization

Debuggex Demo

此表达式表示找到任意数字,一次或多次,然后是hr,可选地后跟s。现在,要使用它,您可能会这样做:

var match = Regex.Match(reminderOptions.SelectedItem.Text, @"(\d+hr[s]?)");
if (match.Success)
{
    var hrs = match.Groups[1];
    lblReminderSet.Text = string.Format(
        "a message sent to you {0} your lessons", hrs);
}

现在,如果你还想要单词before,你可以稍微修改一下Regex:

(\d+hr[s]? before)

并将其作为比赛的一部分。