Click here to Skip to main content
16,015,296 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey everyone

I have a button btnDelete --
<asp:Button ID="btnDelete" runat="server" Text="Delete" Style="float: left; margin-right: 10px;
                                    color: black; font-weight: bold; margin-left: 40px" Height="26px" Width="67px"
                                    OnClientClick="javascript:return confirm('Are you sure you want to delete this item?')"
                                    OnClick="btnDelete_Click" />


and codebehind for button click handler is--

protected void btnDelete_Click(object sender, EventArgs e)
        {
            RadGrid Grid = (this.FindControl("RadGrid1") as RadGrid);
            if (RadGrid1.SelectedItems.Count != 0)
            {
                string Pid = Convert.ToString(Grid.SelectedValues["Pid"]);
                if (!string.IsNullOrEmpty(Pid))
                {
                    try
                    {
                        int resultDel = objSQLHelper.DeleteRowsInDb(Convert.ToInt32(Pid));
                        //RadGrid1.Rebind();
                        BindGrid();
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }


is there a way by which i can validate this button client side,such that if no row is selectced,it shows an alert "Please select a row" and no confirmation box is shown until a row is selected?...

Thanks
Amit
Posted
Comments
Sunasara Imdadhusen 29-Oct-10 6:01am    
This would be possible when you will able to get RadGrid1.SelectedItems.Count at client side?
AmitChoudhary10 29-Oct-10 6:07am    
And how to do that?...
Radhakrishnan G. 29-Oct-10 6:56am    
http://msdn.microsoft.com/en-us/library/ms972961.aspx

Control Validation using Javascript This code for Text box.
I hope its useful for U.
Check Condition onClientClick Before Delete if all path return true then insert code for Confirmation.
<pre lang="cs">// Get All Childe Node value inside Selected Div
        function CheckSelection() {
    var DivCollection = document.getElementById("UrradGrid");
            var inputDiv = DivCollection.getElementsByTagName("input");

            if (DivCollection.hasChildNodes) {
                for (var i = 0; i < inputDiv.length; i++) {
                    if (inputDiv[i].type == 'text') {
                        if (inputDiv[i].id.search('txtBuildingName') != -1) {
                            var id = inputDiv[i].id;
                            var txtControl = document.getElementById(id);
                            var txtValue = txtControl.value;
                            if (txtControl.value == "") {
                                txtControl.focus();
                                alert("Please Enter Correct Name");
                                return false;
                            }
                           
                        }
                    }
                }
            } // hasChildENd

 
Share this answer
 
v2
Arvind's solution:

C#
function CheckSelection() {
    var myGrid = document.getElementById("<%=RadGrid1.ClientID%>");
    var checks = myGrid.getElementsByTagName("input");
    var selected = false;
    if (checks.hasChildNodes) {
        for (var i = 0; i < checks.length; i++) {
            if (checks[i].type == 'checkbox')
                selected = checks[i].checked == 1;
        }
    }
    if(selected)
        return confirm('Are you sure you want to delete this item?')
    else
        return false;
}

...
<br />
<asp:Button ID="btnDelete" runat="server" Text="Delete" Style="float: left; margin-right: 10px;<br />
                                    color: black; font-weight: bold; margin-left: 40px" Height="26px" Width="67px"<br />
                                    OnClientClick="return CheckSelection()"<br />
                                    OnClick="btnDelete_Click" />
 
Share this answer
 
C#
if (RadGrid1.SelectedItems.Count != 0)
{
    string Pid = Convert.ToString(Grid.SelectedValues["Pid"]);
    if (!string.IsNullOrEmpty(Pid))
    {
        try
        {
            int resultDel = objSQLHelper.DeleteRowsInDb(Convert.ToInt32(Pid));
            //RadGrid1.Rebind();
            BindGrid();
        }
        catch (Exception ex)
        {
        }
    }
}
else
    AlertBox("Hey you, select something first.");

...
C#
protected void AlertBox(string message)
{
    ClientScript.RegisterClientScriptBlock(
        Page.GetType(), "alertBox", "alert('" + message + "');", true);
}
 
Share this answer
 
Comments
AmitChoudhary10 29-Oct-10 7:21am    
Thats right,but the thing is that i want an alert before confirmation box....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900