How do I get the old text and change the textbox text in the TextChanged textbox?
I am new to C #. I have a requirement for previous text and recently changed text of a textbox on a text changed event of the same. I tried to get the text on the textchanged event, but this is only new text. How can I get the previous text as well?
eg. Let's say I have the text "abc" in a textbox and I change it to "pqr" by pasting the text directly rather than by typing. now on the text change event txtbox.text returns me "pqr". But I need to compare the previous and new text, so I also need "abc". So how can I get it?
private void txtFinalTrans_TextChanged_1(object sender, EventArgs e)
{
gstrOldText = txtFinalTrans.Text;
}
source to share
Try to create a global variable and put the text of the textbox at the time of the event GotFocus
and use it as Old text at the time of the event TextChanged
, as if:
string OldText = string.Empty;
private void textBox1_GotFocus(object sender, EventArgs e)
{
OldText = textBox1.Text;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
string newText = textBox1.Text;
//Compare OldText and newText here
}
Hope it helps ...
source to share
You need to keep the old value. For example, in a field or property of the same class.
private string LastFinalTrans { get; set; }
private void txtFinalTrans_TextChanged_1(object sender, EventArgs e)
{
TextBox txt = (TextBox) sender;
if(LastFinalTrans == txt.Text)
{
// ...
}
LastFinalTrans = txt.Text;
}
source to share
Declare String OldValue in public
private void textbox1_Enter(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textbox1.Text))
{
OldValue = textbox1.Text.Trim();
}
}
Disable functioning ...
private void textbox1_Leave(object sender, EventArgs e)
{
string Newvalue = textbox1.Text;
//Compare Both Values
//OldValue and Newvalue
}
source to share
Rejoice
In the case of ASP.NET, the easiest way to accomplish this skill is to use a hiddenField. At one point, I was hoping I could just add a data property to the textbox and put the old value there with a touch of JavaScript, and then read that attribute in code. But unfortunately this is not how the postback system works.
So, we want to get the old value and store it in the hidden box as soon as the user places the cursor in the TextBox. We accomplish this by setting the onfocus property on the TextBox to the name of the javascript method that will perform the save. Let's call this saveOldValue method.
<asp:TextBox ID="MyTextBox" runat="server" AutoPostBack="true" onfocus="setOldValue()" OnTextChanged="MyTextBox_TextChanged"></asp:TextBox>
<asp:HiddenField ID="oldValueHiddenField" Value="" ClientIDMode="Static" runat="server" />
Before adding the javascript setOldValue: method, to make your code as versatile as possible, don't let the saveOldValue method know anything about the specific TextBox calling it or the specific HiddenField where the old value will be stored. Thus, we can use this method with any TextBox and HiddenFields. So let's pass saveOldValue the current textbox instance so that it can access the properties of the TextBox. Also, let's add a property to the TextBox that will hold the id of the hiddenField that will eventually hold the old value; Let's call this property data-hiddenField and assign it a HiddenField id.
<asp:TextBox ID="MyTextBox" runat="server" AutoPostBack="true" data-hiddenField="oldValueHiddenField" onfocus="setOldValue(this)" OnTextChanged="MyTextBox_TextChanged"></asp:TextBox>
<asp:HiddenField ID="oldValueHiddenField" Value="" ClientIDMode="Static" runat="server" />
Next, let's set up the JavaScript setOldVaue method:
<script>
function setOldValue(e){
//I am using a bit of jQuery here, but feel free to go pure JS
var hiddenField = $(e).attr("data-hiddenField");
var oldValue = $(e).val();
$("#"+hiddenField).val(oldValue);
}
</script>
Next, let's get the values ββin the TextChanged event [fires when you leave the text field]:
protected void MyTextBox_TextChanged(object sender, EventArgs e)
{
var tb = sender as TextBox;
var oldVaue = oldValueHiddenField.Value;
var newValue = tb.Text;
}
Hope this added value.
//World
source to share