Confirmation message in asp.net? - c#

<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 save data?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
and my code behind code is
protected void btnSave_Click(object sender, EventArgs e)
{
string confirmValue = null;
confirmValue = Request.Form["confirm_value"];
if (confirmValue == "No")
{
some code....
}
else
{
return;
}
}
What actually gonig is confirmValue storing the values like yes,No,Yes,No.
But for first time it will show confirmation message, I will click on Ok button and some entries will done then I click on save button it will show again message box at this time I will click on Cancel button then it will go into the if condition but confirmvalue is storing First clicked yes and Next time clicked no so it is going into else condition ....
what i have to do please help me b

The cleanest way is to simply use the "OnClientClick" property of the button control like so:
OnClientClick="return confirm('Do you want to save data?');"
If the user clicks "No" then the button event will never fire... You don't need any of that other stuff.

Related

How to change the text on a button every time the button is clicked?

I want my button to be a Start/Stop button at the same time. It starts with the Start as Text property, but when it's clicked I want the Text on the button to be "Stop". And then go on in infinity...
I have tried this, but unfortunately the button doesn't change text when I click it:
My button is declared as a member variable in this class. In the first method I've used the EventHandler start.Click += StartButton;
The second method is the StartButton method. I want that this method changes the Text property of the Button. Then I did this:
public void StartButton(object sender, EventArgs ea)
{
if (start.Text == "Start")
{
start.Text = "Stop";
}
else
start.Text = "Start";
}
try to use else if
should be working
if (start.Text == "Start")
{ start.Text = "Stop"; }
else if (start.Text == "Stop")
{start.Text = "Start"; }
else if is used when the first condition is false
if will always check both.. so there will be no change / one change only if used correctly
Hope I could help you :)

c# keydown code not working

I have a TextBox used to make research by keyword. I have checkboxes to filter the results and also a graph to display occurences of keyword in the table. The search is done once you click on the button made for it, it also works if I click on Enter Key. The problem is that if I decide to check 2 checkboxes and Click on Enter, the research is not made, or if I change the keyword and click again on Enter the result of the graph is not made. I'd like it to work whenever I click on the button or Enter.
I'm using KeyDown like I saw on the internet but it's not changing anythhing, no error either.
My code :
protected void btnSearch_Click(object sender, EventArgs e)
{
Populate();
}
protected void tbSearch_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
btnSearch_Click(null, null);
}
Can someone help me ?
On your textbox, just
add the attribute autopostback="true"
add the attribute OnTextChanged="btnSearch_Click"
and it will do what you want without JQuery
--> autopostback: postback to server when an event is triggered
--> OnTextChanged: when you get out of textbox (or press enter), the event is triggered and go to your search function
Ok I've found the problem, I didn't mention I was using update Panel.
So here is the code working for me :
$('#tbSearch').keydown(function (e) {
var key = e.which;
if (key == 13) // the enter key code
{
//your code
}
});
To make that code work with updatePanel :
//On UpdatePanel Refresh.
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (prm != null) {
prm.add_endRequest(function (sender, e) {
if (sender._postBackSettings.panelsToUpdate != null) {
$('#tbSearch').keydown(function (e) {
var key = e.which;
if (key == 13) // the enter key code
{
// your code
}
});
}
});
};

Testing to verify that a button is clicked first before another is clicked "Webfomrs"

I used an if statement saying that if lblTotalAmount is populated then you can be able to click the second button. Because if lbltotalamount is populated then the first button was clicked to populate it. However, with my code below it works by showing the error message if you try to click the second button before the first button but then if i do it in the correct order it will not redirect me to the page i stated below. How can i correctly state this so that it will work?
protected void btnSubmitOrder_Click(object sender, EventArgs e)
{
if (lblTotalAmount == null)
{
Response.Redirect("~/Default.aspx");
}
else
{
lblMessage.Text = "Please click the Calculate Order Total button first";
}
}
You should be trying to validate on the Text property of the lablel instead
protected void btnSubmitOrder_Click(object sender, EventArgs e) {
if (string.IsNullOrEmpty(lblTotalAmount.Text))
{
Response.Redirect("~/Default.aspx");
}
else
{
lblMessage.Text = "Please click the Calculate Order Total button first";
}
}

Confirmation message box in webapplication

Am using the below message box in asp.net web application. Now i want to convert this message box as a confirmation message box and do something when it is true else means reject the application.
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('Are you sure, you want to apply?');</script>", false);
I think you are going about this the wrong way. You should display this confirmation before posting back, and then only post back if they choose to "Apply".
Using ASP.NET web controls, the Button control has an OnClientClick property which can be used to call javascript prior to Http POST:
You could do something like this:
<asp:button id="btn"
runat="server"
Text="Apply"
OnClientClick="return confirm('Are you sure you wish to apply?');"
OnClick="btn_Click" />
register script bellow instead of alert
<script type="text/javascript">
var r=confirm("Are you sure you are sure?")
if (r==true)
{
//You pressed OK!
}
else
{
//You pressed Cancel!
}
</script>
The javascript equivalent to a confirmation box is the confirm() method. This method returns a true or false value depending on the user's "OK" or "Cancel" button response.
Usage:
var confirmed = confirm('Are you sure?','Are you sure you want to delete this item?');
if(confirmed){
//do something
} else {
//do something else
}
Try this
Add this on your cs file to display a confirm instead of alert
string confirm =
"if(confirm('Are you surely want to do this ??')) __doPostBack('', 'confirmed');";
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", confirm, true);
Add this on same page to check when user is coming from that confirmation box
protected void Page_Load(object sender, EventArgs e)
{
string parameter = Request["__EVENTARGUMENT"];
if (string.Equals("confirmed",
parameter,
StringComparison.InvariantCultureIgnoreCase))
{
// Call your server side method here
}
}
For this I used __doPostBack you can learn more about it from here.
Hope it'll help you
private void showMessage(string msg){
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('"+ msg +"');</script>", false);
protected void BtnReg_Click(object sender, EventArgs e)
{
OracleHelper.OracleDBOpen();
object flag = OracleHelper.OracleExecuteScalar("your select Query ");
if (flag == null)
{
showMessage("Failed !!! ");
}
else
{
string reg = String.Format("your Insert Query ");
showMessage("successfuly");
OracleHelper.OracleExecuteNonQuery(reg);
}
OracleHelper.OracleDBClose();
}
}
To do this completely within C#, you can try this:
protected override void OnInit(EventArgs e)
{
AddConfirmationButton();
base.OnInit(e);
}
private void AddConfirmationButton()
{
Button confirmButton = new Button();
confirmButton.Text = "Action Foo";
string confirmationMessage = "Are you sure you wish to do action Foo?";
confirmButton.OnClientClick = "return confirm('" + confirmationMessage + "');";
confirmButton.Command += confirmButton_Command;
Controls.Add(confirmButton);
}
void confirmationMessage_Command(object sender, CommandEventArgs e)
{
DoActionFoo(); //work your magic here.
}
This presents and "OK/Cancel" dialog box to the user from the webpage. If the user clicks 'ok', the function from the command Event fires. If the user clicks 'cancel', nothing happens.
ScriptManager.RegisterStartupScript(page,this.GetType(), "temp","javascript:calopen();
",true);
function calopen()
{
if (confirm("Are you sure?"+'\n'+"Are you want to delete"))
{
enter code here
}
else
{
return false;
}
}
try this code check on OnClientClick event when user click on button
<script type="text/javascript">
function check()
{
var chk =confirm("Are you sure you are sure?")
if (chk==true)
{
// try you want
}
else
{
// try you do not want
}
return true;
}
</script>
<asp:button id="Button1"
runat="server"
Text="Button1"
OnClientClick="return check();"/>

Enable or Disable Button Based On TextBox.Text Text Changes

I am creating a web application for my company. My application has a button and a textbox.
What I want to do is entering some value into the textbox and then when i click the button the application will process the value based on the input in the textbox.
Now here is the tricky part.
After the button is clicked once and the text in textbox remains, the button shall disappear.
However if there is modification in the textbox.text, the button shall reappear.
But if the textbox.tex somehow return to original value, the button shall disappear again.
Please its quite urgent, I have tried my best to do it already, yet so far unsuccessful. I also did a lot of research in Google, but so far none of my findings suit my case.
Your help is appreciated.
You got it here http://jsfiddle.net/ywe9G/
var ori = '';
$(document).ready(function(){
$('button').hide();
$('input').keyup(function(){
if($(this).val() != ori){
$('button').show();
}else{
$('button').hide();
}
});
$('button').click(function(){
ori = $('input').val();
$(this).hide();
});
});
private string oldTextboxValue = "";
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text != oldTextboxValue)
{
button1.Visible = true;
}
else
{
button1.Visible = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
oldTextboxValue = textBox1.Text;
button1.Visible = false;
}
to disable button you can do this-
var oldvalue="";
$('#buttonid').click(function(){
//your code
oldvalue = $("#textBox").text();
this.hide();
}
$("#textBoxId").keypress(function(){
if(this.value!=oldvalue){
this.show();
}
}
Demo here
HTML
<textarea id='mytext' rows='4' cols='20'></textarea>
<br>
<input type="button" id='my_button' value="click">
<input type="hidden" id="my_hidden">
jQuery
var clicked=false;
jQuery(document).ready(function() {
$("#my_button").click(function(){
$("#my_hidden").val($("#mytext").val());
$(this).hide();
clicked=true;
});
$("#mytext").keyup(function(){
if(clicked==true && $(this).val()==$("#my_hidden").val()) {
$("#my_button").hide();
}else if(clicked==true){
$("#my_button").show();
}
});
});
You can similarly enable/disable button instead of show/hide

Categories

Resources