来自linkbutton点击事件的javascript警报

时间:2013-03-11 16:03:46

标签: asp.net

我有一个ASP LinkButton,每次点击时都会向List<int>添加ID值。我需要帮助的是如何在添加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
}

2 个答案:

答案 0 :(得分:1)

注册客户端脚本

        if (myList.Count < 20)
        {
            //Add item to list
        }
        else
        {
            var script = "alert(\"more than 20\");";
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MoreThan20", script, true);
        }

答案 1 :(得分:0)

方法1

在html中放置一些文字。

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

方法2

在你的html上放一个隐藏的字段 每次添加新项目时增加1 并在java脚本中检查最大值
如果达到值,则提醒您。