ClientScript.RegisterClientScriptBlock not working with DetailsView_ItemInserting

4.2k Views Asked by At

I have a detailsview binded to a ClientEntitydatasource. In DetailsView_ItemInserting i'm trying to check whether user has inserted all the fields and show popup according to that.

PROBLEM

I have found out that if detailsview is binded to a datasource following would not execute:

 protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {

        if (e.Values["Name"] == null)
        {
            string script = "alert('Please Insert all the mandatory Fields');";
            ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true); //would not get execute.
        }
    }

In order to do that ,I have to explicitly add items to corresponding datasource and also I have not binded it to ClentEntityDatasource in .aspx as follows:

 protected void DetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {


        FileUpload FileUploadControl = DetailsView.FindControl("FileUpload1") as FileUpload;
         if (FileUploadControl.HasFile && e.Values["Name"] != null)
        {
            string Name = e.Values["Name"].ToString();
            try
            {
                string FileName = Path.GetFileName(FileUploadControl.FileName);
                string MapPath = "~/Images/" + FileName;
                FileUploadControl.SaveAs(Server.MapPath("~/Images/") + FileName);
                byte[] imgdata = System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath(MapPath));
                SqlConnection myConnection;
                string myConnectionString = "Initial Catalog=42HNetDb;Data Source=localhost;Integrated Security=SSPI";
                myConnection = new SqlConnection(myConnectionString);
                string myInsertQuery = "INSERT INTO Client (Name,Logo) Values(@Name,@Logo )";
                SqlCommand myCommand = new SqlCommand(myInsertQuery);
                myCommand.Connection = myConnection;
                myCommand.Parameters.Add("@Name", SqlDbType.NVarChar);
                myCommand.Parameters["@Name"].Value = Name;
                myCommand.Parameters.Add("@Logo", SqlDbType.Image);
                myCommand.Parameters["@Logo"].Value = imgdata;
                myConnection.Open();
                myCommand.ExecuteNonQuery();
                myCommand.Connection.Close();
            }
            catch (Exception ex)
            {
                StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }
        else
        {
           string script = "alert('Please Insert all the mandatory Fields');";
            ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true); //gets executed properly
        }

In short i have found out that if Detailsview is binded to some datasource ClientScript.RegisterClientScriptBlock would not get execute.

I have also tried using ScriptManager.RegisterStartupScript but nothing happens it won't give an error but no popup is shown. Am I missing something here??If you need code.aspx just tell me.

Any help would be appreciated.Thanks!!!

2

There are 2 best solutions below

2
Neeraj On

Hey please use this code

 ClientScript.RegisterStartupScript(typeof(Page), "Alert", "script", true);

instead of

ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);

Hope It helps you

0
Ankush Dubey On

Actually I was going in wrong Direction , What I needed is RequiredFieldValidator for Validation controls .Refer Here http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.requiredfieldvalidator.aspx

Thanks!