Solved

Attach JavaScript to a server control

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

840 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