Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1456
  • Last Modified:

ASP: onclick button handler

I've been trying to run the following code but keep getting a runtime error. At the click of a button, I want it to display a table that is the result of a query. Please help!

<FORM>
<INPUT TYPE="BUTTON" onClick="(<%Response.Write RecToTable(rsFreq)%>)" VALUE="List Frequent calls">
</FORM>

Thanks


0
odisu
Asked:
odisu
1 Solution
 
Sashi RachabattulaSCRUM Master, .NET Project LeadCommented:
listen onClick is the event and u can call a function from that

what u can do is make a table as hidden and poppulate with all data and onclick of button make table as visible  
0
 
bradleymr2Commented:
Hello odisu,

I'm going to put a little background information about ASP development in this answer.  I'm pretty sure you already know a lot of this (but maybe someone else looking at this question with the same problem may not understand how developing for the Internet works).

When a user to your application loads the ASP page, the entire page (with consists of server-side scripts, client-side scripts and HTML) is processed by the server and sent to the user's web browser (such as Internet Explorer) as an HTML page.  At that point, the interaction between the web server and the user (client) is complete.  

The onClick event handler for the HTML button is used to execute client-side scripts.  Client-side scripts execute solely on the user's PC and do not interact with the server.  Therefore, if you need to talk to a database, you cannot do that with a client-side script, since it has no connection back to the database server.

Functions such as RecToTable are server-side scripts, meaning that they are executed by the web-server (which can connect to the database server) and their visible results are translated into HTML to be sent to the client PC (the user's browser).

Unfortunately, client-side scripts cannot call server-side scripts.  This is the reason why you are getting your error when trying to call RecToTable from the onClick event.

The differences between client-side and server-side scripts are often difficult to grasp at first for new web developers, especially traditional Window developers.  The good news is that in ASP.NET, web development becomes more like windows development, giving you much more power to handle events.

To solve your specific problem, you can do a couple of things:

1) Call RecToTable as part of the normal ASP script and then from the onClick event handler call a Javascript function that displays the table. (see example below)

2) When the user clicks the onClick button, call a Javascript function that reloads the page and shows the table (useful if the values of the table are populated based on the interactions of the user on that page)

3) Use your button to submit a form and, using the Action parameter of the <FORM> tag, reload the page and show your table.

The example below shows option #1, which from the sound of your message will probably solve your problem.  Please let me know if you need any further information.

Ryan

<%
      sub RecToTable
            Response.Write("<TABLE width='100%' border=1 id='tblRecords' style='VISIBILITY: hidden'>")
            Response.Write("      <TR>")
            Response.Write("            <TD colspan=2><B>Records table</B></TD>")
            Response.Write("      </TR>")
            Response.Write("      <TR>")
            Response.Write("            <TD width='25%'><B>Record 1:</B></TD>")
            Response.Write("            <TD width='75%'>Record 5 text goes here</TD>")
            Response.Write("      </TR>")
            Response.Write("      <TR>")
            Response.Write("            <TD width='25%'><B>Record 2:</B></TD>")
            Response.Write("            <TD width='75%'>Record 5 text goes here</TD>")
            Response.Write("      </TR>")
            Response.Write("      <TR>")
            Response.Write("            <TD width='25%'><B>Record 3:</B></TD>")
            Response.Write("            <TD width='75%'>Record 5 text goes here</TD>")
            Response.Write("      </TR>")
            Response.Write("      <TR>")
            Response.Write("            <TD width='25%'><B>Record 4:</B></TD>")
            Response.Write("            <TD width='75%'>Record 5 text goes here</TD>")
            Response.Write("      </TR>")
            Response.Write("      <TR>")
            Response.Write("            <TD width='25%'><B>Record 5:</B></TD>")
            Response.Write("            <TD width='75%'>Record 5 text goes here</TD>")
            Response.Write("      </TR>")
            Response.Write("</TABLE>")
      end sub
%>
<script language="JavaScript">
      function ShowTable()
      {
            document.all("tblRecords").style.visibility = "visible";
            return true;
      }
</script>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
</HEAD>
<BODY>
            <P>
                  <INPUT type=button onClick="return ShowTable();" value="Show Table">
            </P>
            <%
                  RecToTable
            %>
</BODY>
</HTML>
0
 
WakieCommented:
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now