1

我有一个页面,其中列出了数据网格中的一堆项目。

每个项目都有一个 cooresponding remove link button,它从列表中删除该项目。在我的事件处理程序中——项目被删除的地方——我检查项目是否是列表中的最后一项。如果是最后一项,我不会删除,而是发送一个警告框,告诉用户该项目不能被删除。我不确定如何让 C# 触发此警报框。

我的代码如下所示:

在我的 aspx 中,我有一个带有各种链接按钮的数据网格。代码片段如图所示:

<ItemTemplate>
    <asp:LinkButton runat="server" ID="Remove"
                OnCommand="lnkRemove_Command" CommandArgument='<%# Eval("Id") %>
                OnClientClick="return false;">
    </asp:LinkButton>
</ItemTemplate>

在我后面的代码中,我的事件处理程序如下所示:

   private List<MyItem> _items;

   protected void lnkRemove_Command(object sender, CommandEventArgs e)
   {
      int ID = Convert.ToInt32(e.CommandArgument);
      MyItem item = MyItem.GetItemByID(id); //This gets the item cooresponding to the ID
      if (_items.Count != 1) 
      {
          //code to delete item   
      }
      else
      {
           //Generate an alert box to tell the user that this item cannot be deleted.
           //I have tried the following two lines of commented code, which didn't work for me
           //Response.Write(@"<script language='javascript'>alert('HERE');</script>");
           //Page.ClientScript.RegisterStartupScript(this.GetType(), "hwa", "alert('Hello World');", true);

      }
   }

此外,可能需要注意的是,在我的 中Page_Load,我已经做了一个Context.RegisterResource(this, StyleSheet, Script). 换句话说,我有工作的 JavaScript 和 CSS,它们与 MyFile.js 中此页面的其他功能的代码相一致

如果可能的话,我想在 MyFile.js 中创建一个 JS 函数,其中我已经有由各种 OnClientClicks 等触发的 js 函数......

是否有可能做这样的事情:

in MyFile.js

var GetAlertMessage = function()
{
    alert("Can't delete this item");
}

并在我上面列出的 C# 函数中调用此函数?

在此先感谢您的帮助。

4

1 回答 1

2

您应该像对待任何其他验证例程一样对待它。应该有一个客户端验证功能和一个服务器端验证功能。

客户端验证

在每个可删除的项目上,添加一个onclick="ValidateDeletion();"class="deleteable-item"

function ValidateDeletion()
{
    var itemCount = $(".deleteable-item").length;
    if(itemCount == 1)
    {
        alert("Sorry, you cant delete this item.")
        return false;
    }
    else
    {
        //let it pass through
    }

}
于 2012-11-16T17:27:32.680 回答