如果gridview为空,则回发验证

时间:2012-09-18 05:34:38

标签: javascript asp.net

我正在尝试提供一个ok取消弹出窗口'如果'gridview没有行,我认为这将像客户端脚本一样工作,但是当我点击ok时,控件会跳过该方法的其余部分。无论如何我可以执行此验证或警报并将控件移动到下一个语句吗?

protected void btnRunD_Click(object sender, EventArgs e)
 {
       try
        {
            int iESStatus = 0;
            int iRunStatus = 0;
            ApplicationUser au = (ApplicationUser)Session["ApplicationUser"];
            if (UploadFileGrid.Rows.Count <= 0)
            {
               ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),     "err_msg", "confirm('No new data files have been uploaded. Are you sure you want to run EYDS Processing Module?');",
           //true);
            }

            if (au.RoleID != 2) //RoleID is not Admin!
            {
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "err_msg", "alert('You do not have enough privileges to use this functionality!');",
           true);
           }
           else
           {.........}
}

我遇到困难的部分在这里:
我试图提供一个ok取消弹出窗口'if'gridview没有行

if (UploadFileGrid.Rows.Count <= 0)
    {
        ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "err_msg", "confirm('No new data files have been uploaded. Are you sure you want to run?');",       
    }

2 个答案:

答案 0 :(得分:3)

假设您的按钮是这样的:

<asp:Button ID="btnDoSomething" runat="server" Text="Do Something" OnClientClick="return GetConfirmation();"  OnClick="btnDoSomething_Click" />

然后添加一个javascript函数,如下所示:

function GetConfirmation() {
    var rowscount = document.getElementByID(<%=UploadFileGrid.ClientID%>).rows.length;
    return rowscount > 0 || confirm('No new data files have been uploaded. Are you sure you want to run?');
}

然后当用户按下确定按钮时显示确认按钮时,它将被回复。

答案 1 :(得分:1)

你可以使用javascript或jquery:

$("#<%=btnApprove.ClientID %>").click(function () {
            if ($("#<%=UploadFileGrid.ClientID %> ").find("tr").length == 0) {
                if (confirm('No new data files have been uploaded. Are you sure you want to run?')) {
                    return true;
                }
                else {
                    return false;
                }
            }

        });

当用户按OK时,您的页面将被回发,您可以编写之后要执行的代码。 我希望这段代码可以帮到你。