Solved

Attach JavaScript to a server control

Posted on 2014-01-26
4
442 Views
Last Modified: 2014-01-26
This .aspx page has a text box and a button. When you enter your name in TextBox1 and click Button1, the JavaScript function showName() is supposed to run on the client to show an alert box with the name entered. But VWD won't show the page in the browser.

How can I wire the JavaScript code to Button1?

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

<script type="text/javascript">
     function showName() {
       alert ("Hello " + '<%= TextBox1.Text%>');
     }
</script>

</head>
<body>
    <form id="form1" runat="server">    
   
        Please enter your name:<br />
                                          
      <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>&nbsp;

      <asp:Button ID="Button1" runat="server" Text="OK" OnClick="showName()"  />

    </form>
</body>
</html>
0
Comment
Question by:MBarongan
  • 2
4 Comments
 
LVL 4

Expert Comment

by:ravikantninave
ID: 39810621
<asp:Button ID="Button1" runat="server" Text="OK" OnClientClick="return showName();" />

<script language="javascript" type="text/javascript">
    function showName() {

alert('hello ' + document.getElementById("<%=TextBox1.ClientID%>").value);
}
0
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 200 total points
ID: 39810651
The correct value does not exist on the server so <%=TextBox1.ClientID%> doesn't do anything useful.  You have to get the value in javascript using the 'id' of the element.
<script type="text/javascript">
    function showName() {
      alert('hello ' + document.getElementById("TextBox1").value);
      }
</script> 

Open in new window

0
 
LVL 4

Accepted Solution

by:
ravikantninave earned 300 total points
ID: 39810655
I tried the code and it works perfectly.  Check
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">
    function showName() {
alert('hello ' + document.getElementById("<%=TextBox1.ClientID%>").value);
} 
</script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    Please enter your name:<br />
                                          
      <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>&nbsp;

      <asp:Button ID="Button1" runat="server" Text="OK" OnClientClick="return showName();" />
    </div>
    </form>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:MBarongan
ID: 39810739
Thank you for both versions in the script,  ("Textbox1").value and ("<%= TextBox1.ClientID%>").value. The thing I was curious about the most was how to wire it up with Button1,  OnClientClick="return showName();"
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

758 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

20 Experts available now in Live!

Get 1:1 Help Now