如何在javascript中为按钮onclick事件调用方法后面的代码

时间:2013-04-23 10:19:21

标签: javascript asp.net

我想通过javascript / jquery

点击按钮时触发此方法

这是我的网络方法,

 [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;
}

如何创建javascript方法来调用上面的代码隐藏方法?

2 个答案:

答案 0 :(得分:1)

您可以将jquery ajax用于您正在寻找的相同目的。网上有很多可用的示例。请检查此链接http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

答案 1 :(得分:1)

你可以使用这样的一些javascript代码,如果你正在使用jQuery(我会在你的情况下推荐):

$(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
            }
        });
    });
});

第一行表示在加载DOM时将执行的功能:

$(function() {});

第二行将事件处理程序附加到标识为idOfButton的按钮的click事件:

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

$.ajax()调用是来自jQuery的方法(请参阅docs),以便更轻松地进行ajax调用。

上面的代码不是纯粹的javascript,而是依赖于jQuery。 jQuery是一个可以使您的javascript代码更容易和更易读的库。我绝对建议你使用它,但还有其他库。