Enable or Disable Button Based On TextBox.Text Text Changes - c#

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

Related

Why textbox prevent div to be closed?

I have a page with a food table, and when I want to add a new food, I have a button of "insert food", which when clicked shows a small div with labels, text-boxes and stuff for insertion, and it changes its text from "insert" to "cancel". Here is the code:
protected void insertFoodBTN_Click(object sender, EventArgs e)
{
if (insertFoodBTN.Text == "Insert Food")
{
nameF.Visible = true;
nameFTB.Visible = true;
priceF.Visible = true;
priceFTB.Visible = true;
... etc ...
addFBTN.Visible = true;
insertDiv.Style["display"] = "block";
insertFoodBTN.Text = "Cancel Insertion";
}
else
{
nameF.Visible = false;
nameFTB.Visible = false;
priceF.Visible = false;
priceFTB.Visible = false;
... etc ...
addFBTN.Visible = false;
insertDiv.Style["display"] = "none";
insertFoodBTN.Text = "Insert Food";
}
}
That works fine
My problem is that when I want to close it back by clicking the same "insert food" button it doesn't even enter the function of that insertButton_Clicked.
Notice - its text is "cancel insertion", but the ID hasn't been changed so it should basically enter.
After enquiring - it turns that the two first text boxes cause that, because when I deleted them the Insert div was gone again, as before.
I don't know why, because I have another text box after them which doesn't prevent hiding the div back.
I don't have a clue why is that. Thanks for helping.
[after clicking "Insert food" for the first time, and the div opens][1]
Instead of using a div try surrounding your form with a Panel.
And instead of one button use two.
protected void insertFoodBTN_Click(object sender, EventArgs e)
{
pnInsert.Visible = true;
cancelFoodBTN.Visible = true;
insertFoodBTN.Visble = false;
}
protected void cancelFoodBTN_Click(object sender, EventArgs e)
{
pnInsert.Visible = false;
cancelFoodBTN.Visible = false;
insertFoodBTN.Visble = true;
}
Since you state in the comments that the validators prevent the form from being submitted, the solution is to enable/disable validation for the button when its role changes:
protected void insertFoodBTN_Click(object sender, EventArgs e)
{
if (insertFoodBTN.Text == "Insert Food")
{
...
insertFoodBTN.Text = "Cancel Insertion";
insertFootBTN.CausesValidation = false; // Cancel button does not require valid data
}
else
{
...
insertFoodBTN.Text = "Insert Food";
insertFootBTN.CausesValidation = true;
}
}
Note: Since the button has two different functions, I would suggest to create two buttons instead, only one of which is visible at a given time. That way, you don't have to "reconfigure" your button every time (which also causes you to duplicate things such as the button text in your ASP.NET markup as well as in your code, violating the DRY principle).

Confirmation message in asp.net?

<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.

using checkbox to enable textbox

i have an application here in winforms that am trying to make. This is how i want it to happen: whenever the user clicks on register visitor button the registration form should be opening. works fine. here is the function that is called in that case:
private void Register_Visitor_Load(object sender, EventArgs e)
On this form i have a textfield placed which i want to disable when the form loads. i wrote a line which disables the textbox on form load:
textbox1.enabled = false;
i placed the above line in the load function which is working fine. now i want to enable my textbox1 based on the checkbox checked. for this i wrote the code:
CheckState state = checkBox1.CheckState;
switch (state)
{
case CheckState.Checked:
{
textBox1.Enabled = true;
break;
}
case CheckState.Indeterminate:
case CheckState.Unchecked:
{
break;
}
now when i place the code above in the page load function nothing happens which is surely going to happen as that function is only called on form load. what am not getting is where to place the checkbox code so that my textbox is enable on runtime. other function are in response to button but what i want here it to instantly enable the textfield on runtime when the user checks the checkbox. kindly explain me how am i going to accomplish this!
You can use CheckStateChanged event; so whatever reason the checkBox1 is checked/unchecked/grayed you'll have the textBox1 properly enabled/disabled
private void checkBox1_CheckStateChanged(object sender, EventArgs e) {
textBox1.Enabled = (checkBox1.CheckState == CheckState.Checked);
}
you are placing code at wrong event.
Instead of placing in pageload place that code on chekchange event of checkbox.
That will help you.
private void chkDisable_CheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked)
{
textBox1.Enable=true;
}
else
{
textBox1.Enable=false;
}
}
Place the above code inside the function which handles the event for check box.
In your case it is checkchanged status.
You can try this:
private void checkBox1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
textBox1.Enabled = false;
}
else
{
textBox1.Enabled = true;
}
}
I did a hybrid of some of the above answers and it worked perfectly. I wanted the state of a button to be disabled upon loading the form, but then enabled if the user checks a box, here's the code:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
button1.Enabled = (checkBox1.CheckState == CheckState.Checked);
}
private void Form1_Load(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
button1.Enabled = true;
}
else
{
button1.Enabled = false;
}
}

text box clear issues

Ok im having a problem with the text boxes of my page holding values of the login info when i navigate to the start up page via the back button of the browser. i need the text boxes to clear when the page is loaded but when i use the code im using the text boxes stay equal to "" instead of the user input when the login button is clicked. heres my code.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack == true)
{
txtEmailLog.Text = "";
txtPasswordLog.Text = "";
}
}
protected void btnLogin_Click(object sender, EventArgs e)
{
PlayerModel plyrModel = new PlayerModel();
PlayerBLO plyrBLO = new PlayerBLO();
List<PlayerModel> models = plyrBLO.GetAllPlayers();
foreach (PlayerModel player in models)
{
if (txtEmailLog.Text == player.PlayerEmail && txtPasswordLog.Text == player.PlayerPassword)
{
Session.Add("Player", player);
Response.Redirect("PlayerMenu.aspx");
}
else
{
lblMessage.Text = "Invalid Login! Retry or Register.";
}
}
}
When ever your page is loaded the following script will run.Include script file jquery-1.4.1.js in your LogIn form it will work even when you click Browser Back Button.
<script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
// clear the text box values onload.
$('#<%=txtEmailLog.ClientID%>').val('');
$('#<%=txtPasswordLog.ClientID%>').val('')
});
</script>
there are other ways, this is one of them
protected void btnLogin_Click(object sender, EventArgs e)
{
// rest of your code
txtEmailLog.Text = "";
txtPasswordLog.Text = "";
}

Radio button changing textblocks

When I click on my radio buttons I would like the textblock1.text to update. This is what I have and thought it would work but it doesnt. This is for visual studio 2010express
private void changetitle(object sender, RoutedEventArgs e)
{
if (radioButton1.IsChecked==true)
textBlock1.Text = "Celsius";
textBlock3.Text = "Farenheight";
if(radioButton2.IsChecked==true)
textBlock1.Text = "Inch";
textBlock3.Text = "cm";
}
this on the otherhand does work but ONLY after I press my button (which I want the title to change before hand so the user knows what they are entering (inches or cm and where they should put them)
private void button1_Click(object sender, RoutedEventArgs e)
{
if (radioButton1.IsChecked == true)
{
textBlock1.Text = "Celsius";
textBlock3.Text = "Farenheight";
CalcDegrees();
}
if (radioButton2.IsChecked == true)
{
textBlock1.Text = "Inch";
textBlock3.Text = "cm";
Calcinch2cm();
}
}
Any input or help would be greatly appreciated please, thank you. V/r
You have to wire an event handler to the onChanged event for the radio buttons. In this event handler, you can change the text.
Also, if this is an ASP.NET application, insure that the radio buttons have runat="server" otherwise the event wiring will not function.

Categories

Resources