Solved

Getting the text from TextBox after JavaScript insertion

Posted on 2010-08-12
11
308 Views
Last Modified: 2012-05-10
Hi!

I have an Applet that send strings to a webpage through LiveConnect.

In Java:
JSObject window = JSObject.getWindow(this);
window.call("writeStatusOnPage", new Object[]{text});

Open in new window


In ASPX:
    <script type="text/javascript" language="javascript">
            function writeStatusOnPage(texto) {
                var address = texto;
                var prevValue = document.getElementById("ctl00_contentplaceholderbody_TextBox2").value;
                document.getElementById("ctl00_contentplaceholderbody_TextBox2").value = prevValue + "\n" + address;
            }
    </script>

Open in new window


The applet sends the string very well and it appears in page.
The problem is that I'm trying to read the TextBox2.Text from C# codebehind and it's returning "", even it's already filled with the string sent from Java (and inserted with Javascript with .value property).

What I'm doing wrong?

Thanks in advance!
0
Comment
Question by:calypsoworld
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 11

Expert Comment

by:Robb Hill
ID: 33423153
You are trying to put a client side value on a server side call...u may need to play the value from jscript in a hidden field..then capture the value on your postback
0
 

Author Comment

by:calypsoworld
ID: 33423280
>robbhill

Thank you for reply!

The textbox control is server-side.
<asp:TextBox ID="txtsimid" runat="server" ReadOnly="true" CssClass="textboxes"></asp:TextBox>

I can't do a postback on this page... Maybe with AJAX... I don't know. But the page shouldn't be reloaded.


Thank you!
0
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33423344
If you want avoid the whole page to be reloaded then you can use Update panel so that only the control will be loaded. Because you said it is live content and it may update regularly so you can just add that textbox to update panel and then load the textbox will the value from javascript.
0
 
LVL 11

Expert Comment

by:Robb Hill
ID: 33423359
Well if you want all this to happen on the initial load then the textbox has to be there before you pass the value to it...so without doing a postback..you would have to write a script for creating the textbox with javascript and pass the value then.

Always take note of the Page Life Cycle process.  There may be other ways to do this with jquery and ajax..but this works too:)


cheers
document.getElementById("somediv").innerHTML="<input type=\"text\"...blah blah...></input>";

Open in new window

0
 
LVL 9

Expert Comment

by:puru1981
ID: 33423498
just remove the readonly attribute from the textbox it will work like a charm
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33423685
0
 

Author Comment

by:calypsoworld
ID: 33423929
When the user clicks on RUN button on applet, the applet sends a string to a textbox on webpage.
This value will not change, it's sent from Java only once.

> robbhill
Sorry, I didn't understand the solution that creates a input box.

> lucky85
Your suggestion is to put the textbox inside an UpdatePanel, right? And after that, what should I do?
Sorry, can you be more specific?

Thanks a lot, everybody!
0
 
LVL 11

Accepted Solution

by:
Robb Hill earned 250 total points
ID: 33424081
ok...simple hello goodbye example...just pass your java value in where I am putting text.
<script type="text/javascript">

function insertText(val,e){

document.getElementById(e).innerHTML+=val;

}

</script>

<textarea id="textIns"></textarea><br />

<a href="javascript:insertText('Hello ','textIns');" onClick="void(0)">Insert 'Hello'</a><br /><a href="javascript:insertText('GoodBye ','textIns');" onClick="void(0)">Insert 'GoodBye'</a>

Open in new window

0
 
LVL 18

Assisted Solution

by:Anil Golamari
Anil Golamari earned 250 total points
ID: 33424101
Step1: As you don't want to reload the whole page everytime there is change in the textbox, I have asked to place the textbox in a update panel something  like

<asp:UpdatePanel ID="upnlDodajFirma" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                  <asp:TextBox ID="txtbox1" runat="server" AutoPostBack="False"></asp:TextBox>                
         
              </ContentTemplate>
        </asp:UpdatePanel>

Step2: You are pulling value using javascript from someother source right, now append that data to the textbox something like this

function clickevent()  
{          
var txtbox1 = document.getElementById("txt1");    
}    

// code behind

txtbox1.Attributes.Add("onclick", "javascript:clickevent();");

Good Luck.

     
0
 
LVL 11

Expert Comment

by:Robb Hill
ID: 33424190
If the value is never changed on a postback..there is no need to wire it with Ajax or an update panel...it just needs to be dynamically created ont the fly each time the other source sends the data.
0
 

Author Closing Comment

by:calypsoworld
ID: 33425067
There is no problem with this operation. I was calling the column name on DB with a wrong name...
Thank you for all the replies anyway!

Now that problem is solved, I have another issue with UpdatePanels and Timer, but I'll put it on another question.

Once again, thank you very much and I'm sorry for the mistake.

Regards.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 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

19 Experts available now in Live!

Get 1:1 Help Now