0

I do some custom Validation in the OnInserting-Event of a data-binded DetailsView and cancel when needed the operation.

Now i want to give the user a feedback, why the Insert is not operated and tried a lot of stuff like Custom-Message-Boxes, making Labels visible and so on.

But no Code is doing something like usual in that event. Before or after the line with the cancelling (e.cancel = true). The code is debugging without errors but without any effect.

Am i going the wrong way? What is the usual workflow for something like that?

I hope somebody can tell me what I'm doing wrong.

(The related part what is not executing is below "// THIS PART:")

protected void DetailsView2_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        // E-Mail auf Existenz prüfen, wenn existiert abbrechen und Nachricht
        string email = e.Values["email"].ToString();
        int vorkommenDB = 0;
        string abfrage = "select count(*) from [user] where email = @mail";
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringLOKALverband"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand(abfrage, con))
            {
                command.Parameters.AddWithValue("@mail", email);
                con.Open();
                int.TryParse(command.ExecuteScalar().ToString(), out vorkommenDB);
                con.Close();
            }
        }
        if(vorkommenDB > 0)
        {

            // THIS PART:

            myMessageBox.Show("Die E-Mail Adresse existiert schon in der Benutzer-Datenbank.\r\nBitte bearbeiten Sie den Datensatz mit der vorhandenen Adresse und legen ihn nicht neu an.");                
            Label temp = (Label)((DetailsView)sender).FindControl("labelEmailDoppelt");
            temp.Visible = true;
            Response.Redirect("user.aspx?email=belegt");

            e.Cancel = true;

        }
        // Passwort verschlüsseln und Parameter ersetzen
        if (e.Values["passwort"] != null)
        {
            string passwortInsert = e.Values["passwort"].ToString();
            string hashVonPasswort = mySHA1.SHA1HashStringForUTF8String(passwortInsert);
            e.Values["passwort"] = hashVonPasswort;
        }
    }
comidos
  • 123
  • 1
  • 12

1 Answers1

0

In your ItemInserting method, you can do the following:

//credit to https://stackoverflow.com/questions/1841452/new-line-in-javascript-alert-box
//For javascript alert, I am putting in a \\n instead of \r\n
string myScript = "\n<script type=\"text/javascript\" language=\"Javascript\" id=\"EventScriptBlock\">\n";
myScript += "alert('Die E-Mail Adresse existiert schon in der Benutzer-Datenbank.\\nBitte bearbeiten Sie den Datensatz mit der vorhandenen Adresse und legen ihn nicht neu an.');";
myScript += "\n\n </script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", myScript, false);
kblau
  • 2,094
  • 1
  • 8
  • 20