Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
Solution:
Special thanks to Mark Nischalke, Yusuf and Martin Jarvis.
I hope this will help other peoples who was searching for same question.

I create page that includes a button and a textbox;
When user click button i send the value of textbox to the ashx handler if value equals to 1234 im giving a True alert else i give false alert

here is the code;

Aspx page code;

<input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />
<asp:textbox id="TextBox1" runat="server" xmlns:asp="#unknown" />

<script type="text/javascript">
    function createXMLHttpRequest() {
        try {
            return new XMLHttpRequest();
        } catch (e) {
        }
        try {
            return new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
        }
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
        }
        alert("XMLHttpRequest not supported");
        return null;
    }
    function Button1_onclick() {
        var xmlHttpReq = createXMLHttpRequest();
        xmlHttpReq.open("GET", "Handler1.ashx?Value=" + document.getElementById('TextBox1').value, false);
        xmlHttpReq.send(null);
        var yourJSString = xmlHttpReq.responseText;
        alert(yourJSString);
    }
</script>


Hander1.ashx code;
public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/plain";
    string Value = context.Request.QueryString["Value"].ToString();
    if (Value == "1234")
    {
        context.Response.Output.Write("True");
    }
    else
    {
        context.Response.Output.Write("False");
    }
}
public bool IsReusable
{
    get
    {
        return false;
    }
}
Posted
Updated 22-May-10 7:15am
v5

You can't call an event handler in the code behind from JavaScript. You can handle the javascript click event and call an Ajax method, or a scriptable method such a WebService
 
Share this answer
 
Like Mark said, you can't call code behind methods directly from javascript. Another scenario to consider is, if your button event handler code is genric, or it can be moved outside your page code, then you can create asp.net ashx handler code. Now you can call your ashx handler code using ajax and interact with it using Javascript code. I have found this trick handy in solving some client / server interaction, that I don't need to refresh the page.
 
Share this answer
 
Whenever I've needed to do this I've used something similar to this:

http://www.dotnetspider.com/resources/1521-How-call-Postback-from-Javascript.aspx[^]

If you need it to be asynchronous you can put your button in an UpdatePanel as a trigger.

Hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900