10

consuming c # events in web forms NO ASP CONTROL

 3 years ago
source link: https://www.codesd.com/item/consuming-c-events-in-web-forms-no-asp-control.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

consuming c # events in web forms NO ASP CONTROL

advertisements

How would I raise an event if my html syntax is the following:

<select runat="server" id="selection" onclick="inputCheck"  />

I tried this in the code behind:

protected void inputCheck(object sender, EventArgs e)
        {
            //doesnt matter because it raised an on click.
        }

Exception:Microsoft JScript runtime error: 'inputCheck' is undefined


Here is an example with jQuery posting back to the server using ajax method, very clean and easy to use. Let me know if you have questions.

--HTML page

<select id="selection" name="selection" />

--Place this following code in the head tag in the html surrounded by the script tags; you must also include the latest jquery code (free download from http://www.jquery.com)

$(document).ready(function()
{
    $("#selection").click(function()
    {
        var selectionValue = $(this).val();
        $.ajax({
            type: "POST",
            url: "CodeBehindPage.aspx/WebMethodName",
            data: "{'input':'" + selectionValue + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                //return value goes here if any
            }
        });
    }
});

//Code behind page

[System.Web.Services.WebMethod]
public static bool WebMethodName(string input)
{
    try
    {
        //do something here with the input

        return (true);
    }
    catch(Exception ex)
    {
        throw ex;
    }

}

--This will post the code to the server without any postbacks, which I like. In order to test, set a break point inside of the WebMethodName function and view the input passed in. HTH

Tags webforms

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK