0

I want to fire this method when I click on button through javascript/jquery

this is my webmethod,

 [WebMethod] 
public static string haldlescrolling(string name, string address)
{
    String str = string.Empty;

    httpWebRequest2 =             (HttpWebRequest)WebRequest.Create("http://ws.vidlib.com/video/list");
    httpWebRequest2.ContentType = "application/json";
    httpWebRequest2.Method = "POST";

    int start1 = start + 10;

    using (var streamWriter = new StreamWriter(httpWebRequest2.GetRequestStream()))
    {
        int max1 = max + 10;
        max = max + 10;
        string more = "{\"StartRowIndex\":\"" + start + "\",\"MaximumRows\":\"" + max + "\"}";
       // string json2 = js + more;
        streamWriter.Write(more);
        streamWriter.Flush();
        streamWriter.Close();
        var httpResponse = (HttpWebResponse)httpWebRequest2.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            string result = streamReader.ReadToEnd();
            l1 = (List<Test>)Newtonsoft.Json.JsonConvert.DeserializeObject(result, typeof(List<Test>));
            for (int i = 0; i < l1.Count; i++)
            {
                str = str + "<img src='https://s3.amazonaws.com/movingmediapurchase/thumbnail/48748.jpg' />";
                string video_id = l1[i].ClipId.ToString();
                str = str + "<div id='parent'  style=\"position:relative; float:left; text-align: top;\" onmouseover='callMouseOver(\"" + video_id + "\")' onmouseout='callMouseOut(\"" + video_id + "\")' ><a href='" + l1[i].PreviewUrl.ToString() + "' class='html5lightbox' data-width='450' data-height='350'><img src='" + l1[i].ThumbnailUrl.ToString() + "' class ='thumbnail'/></a><div id='" + video_id + "' style='display: none;position: absolute; z-index:10000; top: 110px; left:30px ; height: 34px;'><img src='tweetbutton.png'/><img src ='small-facebook-like-butto.gif' /><img src='pinit-button.png' /></div></div>" + "&nbsp;";

                /*
                Label1.Text = l1.Count.ToString();
                Image1.ImageUrl = l1[0].ThumbnailUrl.ToString();
                Label1.Text = l1[0].ThumbnailUrl.ToString();
                 * 
                 */
            }
        }
    }
    return str;
}

How to create javascript method to call the above code behind method?

2
  • go do homework on Ajax,json.. it will really help you in future.. Commented Apr 23, 2013 at 10:46
  • No idea why this question is downvoted. It is clear (even if the asker isn't native English speaker) and includes code (so we know the problem isn't the C# code). It might be a duplicate though. But for a downvote, it's best to add a comment. Commented Apr 23, 2013 at 10:49

1 Answer 1

1

You could use some javascript code like this, if you're using jQuery (which I would recommend in your case):

$(function() {
    $('#idOfButton').click(function() {
        $.ajax({
            type: "POST",
            url: "Default.aspx/haldlescrolling",
            data: "{name: 'name', address: 'address'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                // do something on success
                $('#idOfDiv').html(msg.d);
            },
            error: function() {
                // do something on error
            }
        });
    });
});

The first line indicates a function will be executed when the DOM is loaded:

$(function() {});

The second line attaches an event handler to the click event of the button with id idOfButton:

$('#idOfButton').click(function() {});

The $.ajax() call is a method from jQuery (see the docs) to make ajax calls easier.

The code above is not pure javascript, but relies on jQuery. jQuery is a library that can make your javascript code easier and more readable. I definitely recommend you use it, but there are other libraries out there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.