Solved

Attach JavaScript to a server control

Posted on 2014-01-26
4
447 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 83

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

831 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