I'm trying to work on this website wherein I need to filter the data which are to be inserted into the database. For instance, I need to insert 'Hello World' to the database. However, it already exists. Therefore, I need to pop-up a confirmation box saying "The string Hello World already exists in the database. Are you sure you want to continue?"
My problem is that I don't know in which place in my code should I include my confirmation message. Please see below for referrence:
private bool CheckData(string myString)
{
//Check database if myString already exists.
return;
}
private void btnSave_Click(Object sender, EventArgs e)
{
CheckData(myString)
//If the above is true, then the confirmation box should appear.
//Do something to save myString to the database.
}
I'm programatically adding an OnClientClick Event handler to my button using the code below:
btnSave.OnClientClick = "confirmation('The string " + myString + " already exists in the database. Are you sure you want to continue?')";
My problem is basically what is the best way to handle this kind of scenario? Since I can't place this code on the btnSave_Click event (it will add the OnClientClick handler but it will only fire the next time the button was clicked).
to do this via ajax, have the onClientClick call a js function that calls a .net handler to check if the file exists, and either:
otherwise, you can check if the file exists in the postback, but then you'll have to display a message and another confirm button to allow the user to overwrite the file. It will create two postbacks if the user decides to upload and overwrite an existing file.
//add this function in your program
public static void ShowAlertMessage(string error)
{
Page page = HttpContext.Current.Handler as Page;
if (page != null)
{
error = error.Replace("'", "\'");
ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "alert('" + error + "');", true);
}
}
//check the value returned by your function
private void btnSave_Click(Object sender, EventArgs e)
{
bool flag=CheckData(myString);
//If the above is true, then the confirmation box should appear.
//Do something to save myString to the database.
if (flag==true)
{
ShowAlertMessage("The data Could not be added");
}
else
{
//do the desired operation of adding in the database.
}
}
Hope it helps..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.