在webmethod中调用函数或服务器控件
我的HTML代码是
function ajaxcall() { $.ajax({ type: "POST", url: "index.aspx/lvimgclick", contentType: "application/json; charset=utf-8", data: JSON.stringify({ }), dataType: "json", }); }; // calling script
.cs代码
[WebMethod] public static string lvimgclick() { return "hi"; } protected void lvimg1_Click(object sender, EventArgs e) { lvlink1.CssClass = "lv-under1";//another class lvimg1.ImageUrl = "~/images/1.jpg"; lvlink2.CssClass = "lv-under"; lvimg2.ImageUrl = "~/https://stackoverflow.com/questions/19899664/calling-function-or-server-controls-in-webmethod/images/spacer.gif"; lvlink3.CssClass = "lv-under"; lvimg3.ImageUrl = "~/https://stackoverflow.com/questions/19899664/calling-function-or-server-controls-in-webmethod/images/spacer.gif"; lvlblhd.CssClass = "detailheader";//label lvlblsubhd.CssClass = "detailsubheader";//label lvtd.BgColor = "#7e65a9";//td lvlblhd.Text = "
Wake up"; lvlblsubhd.Text = " to a fragrant day.."; lvlbl.Text = "A beautifully fragrance residence"; }
我想要的是:
[WebMethod] public static string lvimgclick() { lvimg1_Click(null, null); return "hi"; }
要么
[WebMethod] public static void lvimgclick() { lvlink1.CssClass = "lv-under1";//another class lvimg1.ImageUrl = "~/images/1.jpg"; lvlink2.CssClass = "lv-under"; lvimg2.ImageUrl = "~/https://stackoverflow.com/questions/19899664/calling-function-or-server-controls-in-webmethod/images/spacer.gif"; lvlink3.CssClass = "lv-under"; lvimg3.ImageUrl = "~/https://stackoverflow.com/questions/19899664/calling-function-or-server-controls-in-webmethod/images/spacer.gif"; lvlblhd.CssClass = "detailheader";//label lvlblsubhd.CssClass = "detailsubheader";//label lvtd.BgColor = "#7e65a9";//td lvlblhd.Text = "
Wake up"; lvlblsubhd.Text = " to a fragrant day.."; lvlbl.Text = "A beautifully fragrance residence"; }
我该怎么办? 我还必须使用lvimg1_Click(null,null); 和lvlink1.CssClass =“lv-under1”; 在很多function上都不是webmethod
我是新手,如果您想了解更多信息,请告诉我
它只能在瞬间请求时完成。 您可以在静态方法中创建服务器控件对象并更改它们,但如果您希望将更改应用于客户端,则需要从服务器响应添加进程字符串的客户端代码。
因此,通常您可以跳过服务器端方法并立即在客户端上执行所有操作
UPDATE
如果你只需要改变css类和图像url,你不需要ajax和web方法,你可以在客户端同时做这样的事情,就像这样
// calling script
如果使用这种情况 – 不需要web方法
UPDATE2
function imgclick() { ... $('#<%= lvlblhd.ClientID %>').removeClass().addClass("detailheader").html("
Wake up") $('#<%= lvlblsubhd.ClientID %>').removeClass().addClass("detailsubheader").html(" to a fragrant day.."); $('#<%= lvtd.ClientID %>').css('background-color',"#7e65a9");//td $('#<%= lvlbl.ClientID %>').text("A beautifully fragrance residence"); ... }
据我所知,你无法使用WebMethods操纵服务器控件,如果你不想对页面进行完整的回发,你可以使用更新面板。
以下是MSDN中更新面板的概述: http : //msdn.microsoft.com/en-us/library/bb399001.aspx