1

我有一个 ASP ,每次单击它时都会LinkButton向它添加 ID 值。List<int>我需要帮助的是如何在添加 20 个项目后向用户发布警报。用户不能添加超过 20 个,所以如果添加了第 21 个项目,我需要提醒他们。这是我当前的代码:

List<int> myList = new List<int>();

if(!String.IsNullOrEmpty(Session["mylist"] + String.Empty))
    myList = (List<int>)Session["mylist"];

//Max 20 items in MyList
if (myList.Count < 20)
{
    //Add item to list
}
else
{
    //Alert to tell user that there are 20 items selected
}
4

2 回答 2

1

注册客户端脚本

        if (myList.Count < 20)
        {
            //Add item to list
        }
        else
        {
            var script = "alert(\"more than 20\");";
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MoreThan20", script, true);
        }
于 2013-03-11T16:19:19.647 回答
0

方法一

在 html 中的某个位置放置文字。

//Max 20 items in MyList
if (myList.Count < 20)
{
   //Add item to list
   literal1.Text="<script>alert("your message");</script>";
}

方法二

在您的 html 上放置一个隐藏字段。
每次添加新项目时将其增加 1
并在 java 脚本中检查最大值
如果值达到警报您的消息。

于 2013-03-11T16:12:32.877 回答