Execute JavaScript code on GridView SelectedIndexChanged event using jQuery in ASP.Net

Last Reply on May 04, 2017 05:40 AM By Indresh

Posted on May 03, 2017 05:59 PM

the below code is working if I call in button OnclientClick But not working if I call in  gridview SelectedIndexChanged event

function Confirm()
{
     var confirmValue = document.createElement("INPUT");
     confirmValue.type = "hidden";
     confirmValue.name = "confirm_value";

     if (confirm("Process Staring ?"))
     {
           confirmValue.value = "Yes";
           document.forms[0].appendChild(confirmValue);
     }
     else
     {
           confirmValue.value = "No";
           document.forms[0].appendChild(confirmValue);
      }
}

Controls.Add(new LiteralControl("<script type='text/javascript'>return Confirm();</script>"));
string confirmValue = Request.Form["confirm_value"];        
if (confirmValue == "Yes")
{
     ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Yes')</script>");
          
}
else 
{
     ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('No')</script>");
}
Posted on May 04, 2017 05:40 AM Modified on on May 04, 2017 05:41 AM

Hi alya14,

Please refer the below code and modify as per your requirement.I have added ControlStyle-CssClass="Select" in your commandField.

HTML

<asp:CommandField ControlStyle-CssClass="Select" HeaderText="Select" SelectText="Select"
        ShowSelectButton="True" />

Jquery

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $('.Select').click(function () {
            var confirmValue = document.createElement("INPUT");
            confirmValue.type = "hidden";
            confirmValue.name = "confirm_value";

            if (confirm("Process Staring ?")) {
                confirmValue.value = "Yes";
                document.forms[0].appendChild(confirmValue);
            }
            else {
                confirmValue.value = "No";
                document.forms[0].appendChild(confirmValue);
            }
        });
    });
</script>