Search code examples
c#gridviewupdatepanel

ScriptManager alert is not working in Gridview c# asp.net


I have a gridview in which I want to show alert if Remarks is blank, So for that I have used the below code but the alert is not displaying.

Also I am using Gridview inside Update panel.

protected void rptHotoIPDataInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    Label id = rptHotoIPDataInfo.Rows[e.RowIndex].FindControl("lbl_ID") as Label;
    TextBox txt_Remarks = rptHotoIPDataInfo.Rows[e.RowIndex].FindControl("txtRemarks") as TextBox;

    string strID = id.Text;
    string strRemarks = txt_Remarks.Text;
    string rejected_by = CurrentUserName;
    string rejected_group_id = ddlApprovalStatus.SelectedValue;

    var splitValue = rejected_group_id.Split('-');
    var middleValue = splitValue[1];

    bool updateData = false;

    if (id != null)
    {
        if (strRemarks != "")
        {
            updateData = UpdateRejectInfo(strID, strRemarks, rejected_by, middleValue);

            if (updateData)
            {
                rptHotoIPDataInfo.EditIndex = -1;
                BindHotoIPNonIPSubmit();
            }
        }
        else
        {
            //ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Remarks cannot be blank')", false);
            string message = "Remarks cannot be blank";
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<script type = 'text/javascript'>");
            sb.Append("window.onload=function(){");
            sb.Append("alert('");
            sb.Append(message);
            sb.Append("')};");
            sb.Append("</script>");
            ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
        }
    }
}

Solution

  • After searching a lot and after lots of trail, below is the proper code which works under Gridview perfectly.

    ScriptManager.RegisterStartupScript(this, typeof(Page), "Alert", "<script>alert('Remarks cannot be blank');</script>", false);