Solved

ASP.NET and Javascript

Posted on 2004-04-14
12
2,636 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
This is a client requirement... not my choice....
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 29

Assisted Solution

by:David H.H.Lee
David H.H.Lee earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

18 Experts available now in Live!

Get 1:1 Help Now