By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,545 Members | 850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,545 IT Pros & Developers. It's quick & easy.

How to Assign javascript value into VB.Net

P: 3
Hi,

I have a problem to assign a value into text box, below will be my script.

Before view my script, let me hv quick explain on what is my problem:
I would like to assign Java script value into VB text box, but i fail to do so. I can pass value with using: <form id="form1" runat="server">. however I' not able to do so, as i already use runat="server" at my MasterPage. Is that any others way i can assign value into the TextBox.

Error Message: "Object doesn't support this property or method: form1.txtDateFrom"
since like cannot call out textbox.

Any help would be greatly appreciated. Thanks for help.
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" MasterPageFile="~/Menu.master" AutoEventWireup="false" CodeFile="CTest.aspx.vb" Inherits="CTest" title="Untitled Page" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  3.  
  4.   <SCRIPT language="vbscript">
  5.   SUB ImgCalendarFrom_onclick()  
  6.        OpenCalendarPage ' Call the JavaScript function to bring up Calendar Dialog. 
  7.        form1.txtDateFrom.Text = strDate          
  8.   END SUB 
  9.   </SCRIPT>
  10.  
  11.   <SCRIPT language="javascript">
  12.   function OpenCalendarPage()
  13.   {
  14.     strFeatures = "dialogWidth=340px;dialogHeight=200px;status=no;"
  15.                     + "center=yes;border=thin;help=no;scrollbars=no;";
  16.     strDate = showModalDialog("Calendar.htm", "Calendar", strFeatures);                     
  17.  
  18.   }   
  19.   </SCRIPT>
  20.  
  21.     <body>   
  22.         <form id="form1">
  23.             Calender Testing<br /><br />            
  24.             <asp:TextBox ID="txtDateFrom" runat="server"></asp:TextBox>&nbsp;
  25.             <asp:Image ID="ImgCalendarFrom" runat="server" ImageUrl="~/Image/CalendarIcon.gif" OnClick="ImgCalendarFrom_onclick()"/><br />
  26.         </form>
  27.     </body>
  28. </asp:Content>
Jun 30 '08 #1
Share this Question
Share on Google+
5 Replies

Frinavale
Expert Mod 5K+
P: 9,735
Your question is a little confusing and your code is even more confusing.

This section of your code is run on the Server. This means that you cannot access any client-side functions when this code is executed.
Expand|Select|Wrap|Line Numbers
  1.   <SCRIPT language="vbscript">
  2.   SUB ImgCalendarFrom_onclick()  
  3.        OpenCalendarPage ' Call the JavaScript function to bring up Calendar Dialog. 
  4.        form1.txtDateFrom.Text = strDate          
  5.   END SUB 
  6.   </SCRIPT>
  7.  

This section of your code is executed in the client's browser (it is client-side functionality) and it cannot interact with your server side code. From what I know of JavaScript, there is no such thing as a showModalDialog() command. There is an Alert() method in JavaScript that shows a message box...
Expand|Select|Wrap|Line Numbers
  1.   <SCRIPT language="javascript">
  2.   function OpenCalendarPage()
  3.   {
  4.     strFeatures = "dialogWidth=340px;dialogHeight=200px;status=no;"
  5.                     + "center=yes;border=thin;help=no;scrollbars=no;";
  6.     strDate = showModalDialog("Calendar.htm", "Calendar", strFeatures);                     
  7.  
  8.   }   
  9.   </SCRIPT>
  10.  
Hi,
I would like to assign Java script value into VB text box, but i fail to do so. I can pass value with using: <form id="form1" runat="server">. however I' not able to do so, as i already use runat="server" at my MasterPage. Is that any others way i can assign value into the TextBox.

Error Message: "Object doesn't support this property or method: form1.txtDateFrom"

The first thing you need to understand is that JavaScript is executed by the browser....client side. Your VB code is executed on the server. They cannot call each other.

VB controls are rendered as HTML in the web browser.
What you need to do is set your up your VB controls to call JavaScript functions when they are rendered in the browser.

See this article for an example on how to use JavaScript with your controls.

-Frinny
Jun 30 '08 #2

P: 3
Your question is a little confusing and your code is even more confusing.

This section of your code is run on the Server. This means that you cannot access any client-side functions when this code is executed.
Expand|Select|Wrap|Line Numbers
  1.   <SCRIPT language="vbscript">
  2.   SUB ImgCalendarFrom_onclick()  
  3.        OpenCalendarPage ' Call the JavaScript function to bring up Calendar Dialog. 
  4.        form1.txtDateFrom.Text = strDate          
  5.   END SUB 
  6.   </SCRIPT>
  7.  

This section of your code is executed in the client's browser (it is client-side functionality) and it cannot interact with your server side code. From what I know of JavaScript, there is no such thing as a showModalDialog() command. There is an Alert() method in JavaScript that shows a message box...
Expand|Select|Wrap|Line Numbers
  1.   <SCRIPT language="javascript">
  2.   function OpenCalendarPage()
  3.   {
  4.     strFeatures = "dialogWidth=340px;dialogHeight=200px;status=no;"
  5.                     + "center=yes;border=thin;help=no;scrollbars=no;";
  6.     strDate = showModalDialog("Calendar.htm", "Calendar", strFeatures);                     
  7.  
  8.   }   
  9.   </SCRIPT>
  10.  
showModalDialog will call out calender.htm, where i manage to prompt out. But when i want to assign value back to Text Box, it's fail.
You are mention that Server and client cannot access both together? is there anyway i can assign value back to Text Box?
Jul 1 '08 #3

Frinavale
Expert Mod 5K+
P: 9,735
showModalDialog will call out calender.htm, where i manage to prompt out. But when i want to assign value back to Text Box, it's fail.
You are mention that Server and client cannot access both together? is there anyway i can assign value back to Text Box?
I'll give you an example of "hello world" using JavaScript and .NET

In your ASPX page you'll have a TextBox used to display the text "hello world" and a button that triggers the JavaScript that inserts "hello world" into the TextBox....

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" MasterPageFile="~/Menu.master" AutoEventWireup="false" CodeFile="CTest.aspx.vb" Inherits="CTest" title="Untitled Page" %>
  2.  
  3. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  4.  
  5.          <asp:TextBox id="MyTextbox" runat="server"></asp:Textbox>
  6.          <asp:Button id="HelloWorldButton" runat="server" text="Say Hi" />
  7.  
  8. </asp:Content>
  9.  
Now during the PeRender event you want to assign the JavaScript call to the button that will display "hello world" in the TextBox:

Expand|Select|Wrap|Line Numbers
  1. Private Sub CTest_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
  2.  
  3.       CreateHelloWorldJavaScript()
  4.  
  5.      'Please note that here we are providing the DisplayHelloWorld function with the MyTextBox.ClientID. 
  6.      'The reason we use the ClientID is because when the TextBox is rendered in the browser it is given a special "ClientID" ...it's name changes from "MyTextBox" to something like "ctl00_ContentPlaceHolder1_MyTextBox" when using MasterPage.  Therefore, in order for the JavaScript to know what the TextBox's id is in the page, we pass it the TextBox's ClientID
  7.  
  8.      HelloWorldButton.Attributes.Add("onclick","DisplayHelloWorld('" + MyTextbox.ClientID + "'); return false;")
  9.  
  10. End Sub
  11.  
  12.  
Notice that there is a call to a Sub called "CreateHelloWorldJavaScript". This sub is responsible for dynamically creating the JavaScript and register it in the page:

Expand|Select|Wrap|Line Numbers
  1. Private Sub CreateHelloWorldJavaScript()
  2.  
  3.   If Not Page.ClientScript.IsStartupScritRegistered("HelloWorldScript") Then
  4.      Dim script As New StringBuilder()
  5.      script.Append("function DisplayHelloWorld(textboxID)" + vblf)
  6.      script.Append("{" +vblf)
  7.      script.Append("     document.getElementById(textboxID).value = 'Hello World!';" +vblf)
  8.      script.Append("}" +vblf)
  9.  
  10.      Page.ClientScript.RegisterStartupScript(page.GetType,"HelloWorldScript",script.ToString())
  11.  
  12.   End If
  13. End Sub 
  14.  
So to recap:
  • You have a .NET button that will call a JavaScript function named "DisplayHelloWorld" to display the text "Hello World!" in a .NET TextBox.
  • The DisplayHelloWorld function takes one parameter: the ID of the .NET TextBox. The TextBox ID that is passed into the DisplayHelloWorld function is the TextBox's ClientID. The reason we use the ClientID is because (when using MasterPages or Web User Controls) the .NET TextBox is given a special ID when it is rendered in the browser that does not match the .NET variable name.
  • You have assigned the JavaScript call to the Button so that when its (JavaScript event) "onclick" event is fired, it calls the JavaScript Function "DisplayHelloWorld".
  • The "DisplayHelloWorld" JavaScript function has been registered in the page and inserts the text "Hello World" into the TextBox by retrieving the TextBox from the page using the document.getElementById() function.


-Frinny
Jul 1 '08 #4

P: 3
Hi Frinny,

example above I get an error message: Do you wish to debug
A Runtime Error has occurred.
Line: 133
Error: Object expected

after debug:
function DisplayHelloWorld(textboxID) { document.getElementById(textboxID).value = 'Hello World!'; }

textboxID = MyTextbox
**value cannot assign to document.getElementById(textboxID).value
Jul 2 '08 #5

Frinavale
Expert Mod 5K+
P: 9,735
Hi Frinny,

example above I get an error message: Do you wish to debug
A Runtime Error has occurred.
Line: 133
Error: Object expected

after debug:
function DisplayHelloWorld(textboxID) { document.getElementById(textboxID).value = 'Hello World!'; }

textboxID = MyTextbox
**value cannot assign to document.getElementById(textboxID).value
Please post your code (ASP and VB.NET) so that I can see.
Normally this error is caused when the JavaScript cannot find the element that you are looking for...you see this when you try to get the value of a null object...or when you're trying to call a method that doesn't exist.

-Frinny
Jul 2 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.