Solved

Attach JavaScript to a server control

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…

680 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