Javascript alert problem
I am trying to add an alert box so that the user can choose either Yes or No or Ok and Cancel, but it is not working correctly.I have to do a database check which is done in c sharp not just link that function to a button clicked event. I开发者_Python百科t is my first time I am trying to do this. I am using visual studio 2010. I am not sure if my code is correct. Can anyone please guide me if I am mistaken.
private void AlertWithConfirmation()
{
Response.Write("<script language='javascript'>");
Response.Write("function onsub() ");
Response.Write("{ ");
Response.Write("return confirm(\"Are you sure?\")");
Response.Write("} ");
Response.Write("</script>");
}
This is my full C# Code:
protected void Import_Click(object sender, EventArgs e)
{
if (!Validation.ValidateDateFormat(dateField.Text))
{
errorMessageLabel.Text = "Invalid Date Format, for example: 1/1/2011 should be 01/01/2011";
}
else
{
//Validation to check if data is already imported
if (BusinessLayerHandler.isImported(dateField.Text) == false)
{
try
{
if (BusinessLayerHandler.isInProgress(dateField.Text)== true)
{
AlertWithConfirmation();
}
}
catch
{
//catch error
}
}
else if (BusinessLayerHandler.isImported(dateField.Text) == true)
{
Alert("That date was already imported");
}
}
your code is absolutely correct. only syntax error. just remove "\" before starting the double quotes in the line->
Response.Write("return confirm(\"Are you sure?\")");
replace with this line
Response.Write("return confirm("Are you sure?\")");
I don't see where you're calling the function. Why not have the function always on the page and dynamically add the function call to the button in the code behind?
Something like:
button.Attributes.Add("onclick", "onsub();");
精彩评论