Solved

ASP.NET and Javascript

Posted on 2004-04-14
12
2,637 Views
Last Modified: 2013-11-25
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?
0
Comment
Question by:pmacmann
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 35

Assisted Solution

by:YZlat
YZlat earned 100 total points
ID: 10826574
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
0
 

Author Comment

by:pmacmann
ID: 10827037
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.
0
 
LVL 3

Expert Comment

by:ljfernandez
ID: 10827137
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.

Lisa
0
 

Author Comment

by:pmacmann
ID: 10827222
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.
0
 
LVL 3

Expert Comment

by:ljfernandez
ID: 10827412
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?
0
 

Author Comment

by:pmacmann
ID: 10829223
This is a client requirement... not my choice....
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 29

Assisted Solution

by:David H.H.Lee
David H.H.Lee earned 100 total points
ID: 10830802
pmacmann,
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
  scriptJS.Append("Javascript:if(this.value!=this.defaultValue){__doPostBack('TextBox1','')")
  TextBox1.attributes.add("onBlur",scriptJS.ToString)
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....

Regards
x_com
0
 
LVL 3

Accepted Solution

by:
ljfernandez earned 300 total points
ID: 10832847
Here's something fancy, create a script to check for a valid date whenever the user enters anything into the text box:

<html>
<head>
<script>
function checkDate(enteredtxt)
   {
        bValid = false;

      //validate date script here

       if (bValid)
           document.forms[0].submit();
   }

</script>
</head>
<form action="page.aspx">
<input type="text" id="txtDate" onKeyPress="checkDate(this.value)"/>
</form>
</html>
0
 

Author Comment

by:pmacmann
ID: 10833153
This is an asp.net 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.
0
 
LVL 3

Expert Comment

by:ljfernandez
ID: 10833399
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!
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 10839703
pmacmann,
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.
0
 

Author Comment

by:pmacmann
ID: 10841539
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
            GetCommentData()
      End Sub
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now