简体   繁体   English

ASP.Net中GridView的JavaScript

[英]JavaScript for GridView in ASP.Net

I am creating a validation for gridview row Selection. 我正在为gridview行选择创建验证。

There is a "Delete" button and a GridView, in that GridView, there is a "Select" LinkButton column. 有一个“删除”按钮和一个GridView,在该GridView中,有一个“选择” LinkBut​​ton列。

I am trying to create a validation for that, When clicking on the "Delete" Button, If the particular row in GridViewis not selected for deleting, then it should show a alert message ie "First select any row to delete." 我正在尝试为此创建一个验证,当单击“删除”按钮时,如果未选择GridView中的特定行进行删除,则它应显示警告消息,即“首先选择要删除的任何行”。

So I wrote a code for the validation: 所以我写了验证代码:

<script type="text/javascript">

function validateGridSelection() {

    var GridSelectedID = document.getElementById('<%=GridView1.ClientID%>');

    if(GridSelectedID.rows.selectedIndex = -1) {
        alert("First select any row to delete");
        return false;
    }
}

</script>

But this JavaScript is not working for this validation. 但是此JavaScript不适用于此验证。

So, Please help me out to sort out this problem. 因此,请帮助我解决此问题。

Thanks. 谢谢。

Is your GridSelectedID getting the grid control? 您的GridSelectedID是否获得网格控件? If not try making your grid ClientIDMode="Static". 如果没有,请尝试使您的网格ClientIDMode =“ Static”。

try this 
function confirmBox() 
    {
        var index = <%=grdMTHints.SelectedIndex%>;
        if (index > -1) 
        {
            return confirm('Are you sure you want to delete this record?');
        }
        else 
        {
            alert('Plese select row to be deleted.');
        }
    }

<asp:Button 
        ID="btnDelete" CssClass="buttonStyle" runat="server" Text="Delete"  CommandName="Delete"         
        OnClientClick="javascript:return confirmBox();" 
        ToolTip="Delete selected row." onclick="btnDelete_Click"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM