ASP.NET and Javascript

I have three ASP.NET controls on a page - a drop down list, a text box and a textarea.

The behaviour I want is that whenever the dropdown list OR the text box changes, the data in the textarea will change (there is a query to the database for this information based on the criteria in the dropdown and textbox).

This is working fine with the textbox but since the textbox is not forcing a postback, this one is not working.

I'm thinking I need some javascript coding to make this happen - I'm a bit rusty here -- and ideas on what I need?
Who is Participating?
ljfernandezConnect With a Mentor Commented:
Here's something fancy, create a script to check for a valid date whenever the user enters anything into the text box:

function checkDate(enteredtxt)
        bValid = false;

      //validate date script here

       if (bValid)

<form action="page.aspx">
<input type="text" id="txtDate" onKeyPress="checkDate(this.value)"/>
YZlatConnect With a Mentor Commented:
declare your javascript functions as usual- in <head> section of your page. Then call your javascript from

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      'Put user code to initialize the page here
       If Not IsPostBack Then
             TextBox1.Attributes.Add("onchange", "javascript:return myFunction1(this);")
                        TextBox2.Attributes.Add("onblur", "this.value=formatFunction(this.value);")
                        btnSubmit.Attributes.Add("onclick", "myFunction2();")

      end if
end sub
pmacmannAuthor Commented:
If I just want the page to post back, can I say
 TextBox1.Attributes.Add("onchange", "javascript:submit(this);")

I don't have a submit button on the page.
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

It may not work the way you want. The onchange event fires after the textbox loses focus and if it has changed since it last had focus.

One thing you can do with javascript is capture the keystrokes and if the textbox is the source of the key event, then read it's value, compare it to a stored value to see if there is a change, and then perform the action (form submit or whatever) if it has.

Let me know if you are interested in seeing some code to demo this idea.

pmacmannAuthor Commented:
The text box is a date field and I have a javascript calendar that can populate this field and the user is also able to enter a date.

Sample code would be great -thanks.
This may be a dumb question but why couldn't you just put a button there that submits the form info from the text field? It seems more appropriate anyway because you don't want to submit something like a date if the user is not done typing it right?
pmacmannAuthor Commented:
This is a client requirement... not my choice....
David H.H.LeeConnect With a Mentor Commented:
You can set AutoPostBack=True for every web control seem you're like to PostBack to server to get the latest result for any changes. Another solution is set it manually, here's the solution :

Sub Page_Load(ByVal Sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
  Dim scriptJS as New System.Text.StringBuilder
End Sub

-I'll advise you to use OnBlur event instead of onchange event. What you need to do is compare the current value with previous stored value, if the latest value is not match with the previous value, you'll postback the server to get the latest result. If this solution not solve your problem, just add a button to your web site and the rest is lifernandez's comments....

pmacmannAuthor Commented:
This is an page - can't use OnKeyPress - and since I have runat=server, I'm not sure how to get client side code to interact with the server control.
I don't see why not. When the page is rendered, it is html in the browser like any other page and I would think that you have access to those event objects (onKeyPress) the same as any other page.

When the form is submitted, I thought you can get the value on the code behind of the response page request.txtDate or request('txtDate') or something and do whatever processing you want on the server side. Maybe I don't remember correctly, but I took a stab anyway!
David H.H.LeeCommented:
I'm also confuse why you can't use OnKeyPress. I dont see runat=server as you mentioned inside your sample code. By the way, do you try all the solutions mentioned above?

>>I'm not sure how to get client side code to interact with the server control.
-Can you give more explanation about what you want from this statement?

-If all the comments still can't solve your problems, please attach some related code.
pmacmannAuthor Commented:
I got this working as  a subset of what I'm doing - I finally just put a submit button on this - (will deal with client later) - need to move on - thanks for your help.

I got this to work by putting the handels txtWorkDate.TextChanged in the code behind.

Private Sub txtWorkDate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtWorkDate.TextChanged
      End Sub
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.