Solved

Attach JavaScript to a server control

Posted on 2014-01-26
4
451 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
checkbox group not updating when none of them is selected 11 43
Data Analysis 7 57
Google Chrome: Debugging AJAX call 6 33
Woo-commerce gallery not working properly 12 34
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 article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

739 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