I have created a hidden field in my JavaScript and this hidden filed is used to capture the value that users select from the message box. When selection is made from the drop-down box, I am making a call to check if the ID that is selected from the drop down is already in the table, if only the ID is found in the table then I am calling the JavaScript to show the message box. If user Selects Yes show some alert and if user selects No show another alert. Somehow, I am having hard time to make it to work, nothing happens when I make selection from the drop-down. Please help as I have spent days on researching this. thanks
here my code:
//javascript code
<script type=text/javascript>
function Confirm() {
var confirm_value = document.createElement(INPUT);
confirm_value.type = hidden;
confirm_value.name = confirm_value;
if (confirm(Do you want to delete data?)) {
confirm_value.value = Yes;
} else {
confirm_value.value = No;
}
document.forms[0].appendChild(confirm_value);
}
</script>
//drop down in aspx file
//code behind
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
string ID = ddlName.SelectedValue;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = SELECT count(*) from MyTable WHERE ID =@ID;
cmd.Parameters.AddWithValue(@ID, ID);
con.Open();
int result = (int)cmd.ExecuteScalar();
if (result >= 1)
{
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), text, Confirm(), true);
string confirmValue = Request.Form[confirm_value];
if (confirmValue == Yes)
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), alert, alert('You clicked YES!'), true);
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), alert, alert('You clicked NO!'), true);
}
}
con.Close();
}
}
}