?
Solved

ASP.NET and Javascript

Posted on 2004-04-14
12
Medium Priority
?
2,646 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 35

Assisted Solution

by:YZlat
YZlat earned 400 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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
 
LVL 29

Assisted Solution

by:David H.H.Lee
David H.H.Lee earned 400 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 1200 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

649 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