Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
89
rated 0 times [  90] [ 1]  / answers: 1 / hits: 47853  / 12 Years ago, mon, january 7, 2013, 12:00:00

Suppose I have a textbox Mytbx and I have a javascript for its onchange event. I hook it up in code behind in Page_Load event like:



Mytbx.Attributes.Add(onchange, test();)


Then I changed the text in code-behind for this textbox like (in a button click event for example):



Mytbx.Text = MyValue


I expect onchange event fired. but actually, it's not. When I click on the button to change value for Mytbx, nothing happening.



How to resolve this problem?


More From » asp.net

 Answers
9

onchange will only be triggered on client side when you directly type something and leave the textbox. So nothing will happen when you set the textbox value in code behind.



If you want to handle the text change event on client side:



<script>  
function test(txt){
alert(txt.value);
};
</script>

<asp:TextBox ID=txt runat=server onchange=test(this);></asp:TextBox>


If you want to handle text change event on server side, you could do this:



HTML:



<asp:TextBox ID=txt runat=server OnTextChanged=txt_OnTextChanged AutoPostBack=true></asp:TextBox>


CS:



protected void txt_OnTextChanged(object sender, EventArgs e)
{
// Do something
}

[#81029] Saturday, January 5, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
monetm

Total Points: 615
Total Questions: 103
Total Answers: 119

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
monetm questions
Fri, Feb 26, 21, 00:00, 3 Years ago
Wed, Sep 9, 20, 00:00, 4 Years ago
Sun, Jul 26, 20, 00:00, 4 Years ago
Thu, Jun 11, 20, 00:00, 4 Years ago
;