• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2648
  • Last Modified:

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?
0
pmacmann
Asked:
pmacmann
  • 5
  • 4
  • 2
  • +1
3 Solutions
 
YZlatCommented:
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
 
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.
0
 
ljfernandezCommented:
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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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.
0
 
ljfernandezCommented:
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
 
pmacmannAuthor Commented:
This is a client requirement... not my choice....
0
 
David H.H.LeeCommented:
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
 
ljfernandezCommented:
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
 
pmacmannAuthor Commented:
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
 
ljfernandezCommented:
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
 
David H.H.LeeCommented:
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
 
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
            GetCommentData()
      End Sub
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now