Calling server side sub from client side button click

Hi, I have some javascript in my page, and a function within that, which is run when the client side form is submitted. What I need is for that function to run, which updates a server side variable, then following that a server side sub should run, using that (newly updated) variable. I have currently set it up with the  onsubmit= tag of the client side form running the first (JavaScript) function, and then the submit button at the bottom of the client side form calling the server side sub with its onClick property. The (probably quite daft!) thinking behind that was that the onSubmit part of the form would run before the onclick event of the button. However at present it seems neither the server side sub or javascript function are running at all. Thanks very much in advance for any help, my code follows:

 ASP.Net code

 <%@ Page Language="VB" AutoEventWireup="False" CodeFile="demo.aspx.vb" Trace="true" Inherits ="demo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
// Cross-Browser Rich Text Editor
// Written by Kevin Roth (
// Visit the support forums at
// This code is public domain. Redistribution and use of this code, with or without modification, is permitted.
<script runat="Server">
    Sub writeOut()
        Response.Write("Runs this one")
    End Sub
    Sub UpdateDB()
        sContent = RTESafe(dContent)
        SqlDataSource1.InsertParameters.Add("Desc", sContent)
    End Sub
    'Sub updateText()  
    '  If Request.Form("rte1") = "" Then
    '      sContent = "here's the " & Chr(13) & """preloaded <b>content</b>"""
    '      sContent = RTESafe(sContent)
    '  Else
    '      sContent = RTESafe(Request.Form("rte1"))

    ' End If
    'End Sub
<html xmlns="" xml:lang="en">
<head><link rel = "Stylesheet" href = "dss.css" type = "text/css" />
    <title>Input Job Details</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <meta name="keywords" content="cross-browser rich text editor, rte, textarea, htmlarea, content management, cms, blog, internet explorer, firefox, safari, opera, netscape, konqueror" />
    <meta name="description" content="The cross-browser rich-text editor (RTE) is based on the designMode() functionality introduced in Internet Explorer 5, and implemented in Mozilla 1.3+ using the Mozilla Rich Text Editing API." />
    <!-- html2xhtml.js written by Jacob Lee <> //-->
    <script language="JavaScript" type="text/javascript" src="html2xhtml.js"></script>
    <script language="JavaScript" type="text/javascript" src="richtext_compressed.js"></script>
<div align="center">
  <img src="GIFs/bannerv3.jpg" width="800" height="71" border="0" usemap="#PageLink" />
<map name="PageLink" id="PageLink">
  <area shape="rect" coords="682,18,791,49" href="Contact us.html" alt="Contact GT" />
  <area shape="rect" coords="544,18,678,48" href="storeLocator.aspx" alt="Find your nearest branch" />
  <area shape="rect" coords="405,18,539,48" href="jobsGriddj.aspx" alt="Jobs at GT" />
  <area shape="rect" coords="293,18,399,48" href="Promotions.html" alt="See our Special Offers" />
  <area shape="rect" coords="199,18,288,48" href="about us.html" alt="About GT" />
  <area shape="rect" coords="121,18,195,48" href="home.html" alt="Home Page" />

<form runat=server id="CDForm" method = "Post">
        <asp:Label ID="Label1" runat="server" Text="Job Title" Width="65px"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server" Width="144px" CssClass="Style1"></asp:TextBox>
        <asp:Label ID="Label2" runat="server" Text="Hours" Width="61px"></asp:Label>&nbsp;
        &nbsp;<asp:TextBox ID="TextBox2" runat="server" Width="142px"></asp:TextBox></p>
        &nbsp;<asp:Label ID="Label3" runat="server" Text="Branch" Width="60px"></asp:Label>
        <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
            DataTextField="ShortName" DataValueField="Address">
            <asp:ListItem Selected="True"></asp:ListItem>
        </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:newconnectionString1 %>"
            SelectCommand="SELECT ShortName, Address FROM [Branch]"
            InsertCommand="INSERT INTO [Jobs] ([title],[location],[date posted],[type],[hours],[shop],[description],[fulltime],[permanent]) VALUES (@Title,@Branch,getDate(),@Perm,@hours,@Branch,@Desc,1,1)">
        <asp:ControlParameter Name="Title" Type="String" ControlID="TextBox1" PropertyName="Text" />
        <asp:ControlParameter Name="Branch" Type = "String" ControlID="DropDownList1" PropertyName="SelectedValue" />
        <asp:Parameter Name="Type" Type="String"/>
        <asp:ControlParameter Name="Hours" Type="String" ControlID="TextBox2" PropertyName="Text" />
        <asp:Parameter Name="FullTime" Type="String" />
        <asp:ControlParameter Name="Perm" Type = "String" ControlID="RBL1" PropertyName="SelectedValue" />
        &nbsp; &nbsp; &nbsp;&nbsp;
        <asp:RadioButtonList ID="RBL1" runat="server">
            <asp:ListItem Value = "Permanent">Permanent</asp:ListItem>
            <asp:ListItem Value = "Temporary">Temporary</asp:ListItem>
    <div style="position:absolute;left:500px;top:750px">
        <asp:Button ID="Button1" runat="server" Text="Button"/>&nbsp;


<!-- START Demo Code -->
<form name="RTEDemo" action="demo2.aspx" method="post" onsubmit="return submitForm();">
<script language="JavaScript" type="text/javascript">
function submitForm() {
    //make sure hidden and iframe values are in sync for all rtes before submitting form
    alert("rte1 = " + (document.RTEDemo.rte1.value));
    //return false;
    '<%#dContent%>' = (document.RTEDemo.rte1.value);
    return true;

//Usage: initRTE(imagesPath, includesPath, cssFile, genXHTML, encHTML)
initRTE("./images/", "./", "", true, true);


<noscript><p><b>Javascript must be enabled to use this form.</b></p>

<script language="JavaScript" type="text/javascript">

//build new richTextEditor
var rte1 = new richTextEditor('rte1');
rte1.html = '<%=sContent%>';
rte1.toggleSrc = false;;
//writeRichText('rte1', '<%=sContent%>', 720, 415, true, false)

<input runat = "server" id="Submit1" type="submit" value="submit" onclick='<%updateDB()%>'/>

<!-- END Demo Code -->


 VB Code Behind:

 Partial Class demo
    Inherits System.Web.UI.Page

    Public sContent As String = ""
    Public dContent As String = ""

    Public Function RTESafe(ByVal strText As String) As String
        'returns safe code for preloading in the RTE
        Dim tmpString As String = ""

        tmpString = Trim(strText)

        'convert all types of single quotes
        tmpString = Replace(tmpString, Chr(145), Chr(39))
        tmpString = Replace(tmpString, Chr(146), Chr(39))
        tmpString = Replace(tmpString, "'", "&#39;")

        'convert all types of double quotes
        tmpString = Replace(tmpString, Chr(147), Chr(34))
        tmpString = Replace(tmpString, Chr(148), Chr(34))
        'tmpString = replace(tmpString, """", "\""")

        'replace carriage returns & line feeds
        tmpString = Replace(tmpString, Chr(10), " ")
        tmpString = Replace(tmpString, Chr(13), " ")

        RTESafe = tmpString
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Response.Write("Page Loading")
    End Sub

End Class


10/19/2007 8:44:13 AM 24353 articles. 2 followers. Follow

4 Replies

Similar Articles

[PageSpeed] 29
Get it on Google Play
Get it on Apple App Store



just use an button on your webform.

you can also do a postback with client side javascript but with you won't need that.

see my blog at and check the future of open mobile widget solutions at
10/19/2007 2:24:33 PM


Thanks for your reply, I would like to do that, however I dont know how to get the value off the control that is created by javascript into the ASP.Net part. The javascript is in a separate form. In the code above, the control that I want to get the value of is called rte1, I think that the form containing it has to be submitted before I can get the text off the rte, is this correct? Or is there a much easier way to achieve this?

10/19/2007 2:30:45 PM

Can anyone please help with this?

10/20/2007 11:28:16 AM


im if you want to pass a data from the client to the server ,

you can use a hiddenfiled server control ,

so you can set its value on the client and on the server ,

you can get that value ...



Anas Ghanem.

Note:Please Don't hesitate to click "Report Abuse" link if you noticed something wrong on the forums (like duplicate ,Off-topic,offensive,or any post that violates the website "TERMS OF USE"). -- Thanks!

10/20/2007 11:44:12 AM

Similar Artilces:

Call server-side page function from client-side button click
I have an existing ASP application that I am migrating to In the ASP app I was using the Microsoft.XMLHTTP on a the client-side of a ASP page to retrieve data from another asp page using the Microsoft.XMLDOM object. The client page passes 3 tags columns, table and Criteria. The receiving page builds the SQL statement executes the query and returns the row with all the column data. I am trying to convert it over to ASP.NET and I am getting errors on the xmlDOM.Load(Request.InputStream) on DLookup.aspx, the code is listed below. Someone suggested to use System.Net HTTP classes su...

Firing both Client side event and server side event for server side button
I am having some difficulties getting this to work.. I have button_click ( and ClientClick (Javascript).  Javascript is firing fine.  Server script is not. may be you have return return false in the in the javascript which is why its not firing server side event.You should add javascript evelt like thisbutton1.onclientclick = "functionname" Function should nopt return false. Vikram www.vikramlakhotia.comPlease mark the answer if it helped you...

Conditional Client Side and Server Side call same button
I have a submit button on a form that I want to first do the validation and if valid call a client side javascript function and then run through its click event handler currently I have this set up by adding this line in the Page_Loadmybutton.Attributes.Add("onclick", "javascript:ShowRunningDialog(this);");But if the page is not valid I don't want to call this client side function. As it is now the 'ShowRunningDialog' is obviously going to be called no matter what     if (Page.IsValid) mybutton.Attributes.Add("onclick", &qu...

How to access Client Side control in server side button click event?
Hi all, I need to access client side control from server side code. For an example <form id="form1" runat="server"> <table> <tr> <td>UserName:</td> <td><input type="text" id="textBoxUserName" /></td> <td><asp:Button ID="buttonShow" runat="server" Text="show" OnClick="buttonShow_Click" /></td> </tr> </table>   </form> I need to access the textBoxUsername in the server side button click event. How to do that? Than...

Is it possible to call client side function from server side code without initial client request?
Atlas is great. I'm using timer to periodically check database changes on server to populate client side controls with new data. Is it possible to do it without a lot of client requests to server which takes a huge useless network traffic? Server should call client side function on all connected clients when, for example, 'database updated' event occurs on server. Thanks. Hi~ Just for your interest, I think you could have a look at Comet, many web IM like Gmail's embeded Gtalk and are built on this architecture你好! Just FYI o_O...

executing server side code and client side (javascript) code simultaneously while clicking a button
how to execute server side code and client side (javascript) code simultaneously while clicking a button in You can't. When clicking a button: the client-side executes, the page does a PostBack, then the server-side executes.NC... what if i want to update a database and then open a new windows is it possible with

Calling server-side code from client-side
Hi,  I´m implementing some shortcut functionality in my web forms and wanna know where i can find something that explains, for example, how to call the handler event "onclick" of a button. I´m using this library to add shortcuts to my controls:   And I want to "link" some key combination for the event handlers of the controls. Does anybody knows?  Thanks. You could attach an AJAX event to the Javascript shortcut event handler so, for example, the shortcut function will trigger a button cl...

Call Server Side methods from Client Side
Hi, How to call Server Side methods from ClientSide with results back. Earlier we were using Anthem approach but now want to use some thing with AJAX. Thanks in Advance.  RegardsSreenivas. you can use the page methods of the ClientScript  control ,please read  : Regards,Anas Ghanem.Note:Please Don't hesitate to click "Report Abuse" link if you noticed something wrong on the forums (like duplicate ,Off-topic,offensive,or any post that violates the website "TERMS OF USE...

How to call server side function on client side
Hi,   How to call server side function on client side? here i want to call change() function on textbox onchange event.   <script language="javascript">    function serverfunction()    {    }    </script><input id="Text1" onchange="javascript:serverfunction();"  type="text" /> code behind:  public void change()    {        Response.Write("hello");    } <script type=&quo...

Call server side method from client side...
I'm using Jquery to call a modal popup dialog and depend on what user select i nead to call a method to populate my generic list can that be don or is no posible way to do that...     Sorry for my bad english...Florim Maxhuni  If you need to call the server from the clientside, you need to look into Ajax Eric | Twitter epascarello | LinkedIn...

Calling a client side function from the server side
Is it possible to call a client side function from the server side. I dont want to invoke that function through button click. I just want to call that to do some operations.  Regards, Aish Use the below code ASP.NET 2.0 Page.ClientScript.RegisterStartUpScript(this.GetType(),"callfunction","<script language=javascript>Test();</script>"); ASP.NET 1.X Page.RegisterStartUpScript("callfunction","<script language=javascript>Test();</script>"); Where Test() is the javascript function. HCHaissam Abdul MalakMCAD.NET| Blog | No, it is not possible.  The cl...

Calling server side scripts from client side
Hello, Can Anyone help out from the following : It is regarding calling server side scripts from client side events. I am able to instantiate a jaguar component in the server side script written in the body of the HTML page as follows : <% var mycomp = java.CreateComponent ("pkg_dispatch/n_jaguar", "iiop://jaguar:9000", "Jagadmin", "" ); if (mycomp == null) { document.writeln("ERROR: failed to instantiate !"); return; } %> I want to use the instance of the jaguar component else wher...

Storing the checked Htmlimputcheckbox values on client side and passing to server side on submit button clicked
Hi All  I've got these bunch of HtmlInputcheckboxes that i'm creating dynamically using div.innerhtml on server side and putting it in a <TD> block, calculate averages etc. Things are working fine until this point. Now i need to get all the checked checkboxes and their values(which are dates in my case) and pass these values back to server side for processing on the submit button click. But i'm using a pagefooter submit button to fire my submit button so i cannot directly pass these values to my submit button onclick method. I tried to use the findcontrol method ...

Weird stylesheet when calling a pop-up window from a server side button rather than a client side one.
I need to create a pop-up window from a server side button rather than a client side one. This is because I need to do some validation, save some records and then create the pop-up window only if the user is allowed access. I call the new form using the Response.Write command below. Response.Write("<Script Language=JavaScript>'NewPage.aspx','newpage','');</script>") A problem occurs when the pop-up form is created, as the parent form's fonts are all changedas they increase in size.  (The Pop-up form's fonts are ok) I have tested this with just a butto...