调用函数或服务器控件的WebMethod [英] calling function or server controls in webmethod
问题描述
我的HTML code是
My html code is
<script type="text/jscript">
function ajaxcall() {
$.ajax({
type: "POST",
url: "index.aspx/lvimgclick",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ }),
dataType: "json",
});
};
</script>
<img src='images/img1.jpg' onclick='return ajaxcall();' /> // calling script
<asp:LinkButton ID="lvlink1" OnClick="lvimg1_Click" CssClass="lv-under" runat="server" >
<asp:Image ID="lvimg1" runat="server" ImageUrl="~/images/spacer.gif" />
<asp:LinkButton ID="lvlink2" OnClick="lvimg2_Click" CssClass="lv-under" runat="server" >
<asp:Image ID="lvimg2" runat="server" ImageUrl="~/images/spacer.gif" />
<asp:LinkButton ID="lvlink3" OnClick="lvimg3_Click" CssClass="lv-under" runat="server" >
<asp:Image ID="lvimg3" runat="server" ImageUrl="~/images/spacer.gif" />
的.cs code
.cs code
[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 = "~/images/spacer.gif";
lvlink3.CssClass = "lv-under";
lvimg3.ImageUrl = "~/images/spacer.gif";
lvlblhd.CssClass = "detailheader";//label
lvlblsubhd.CssClass = "detailsubheader";//label
lvtd.BgColor = "#7e65a9";//td
lvlblhd.Text = "<img src='images/spacer.gif' height='8px' width='5px' /><br/>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 = "~/images/spacer.gif";
lvlink3.CssClass = "lv-under";
lvimg3.ImageUrl = "~/images/spacer.gif";
lvlblhd.CssClass = "detailheader";//label
lvlblsubhd.CssClass = "detailsubheader";//label
lvtd.BgColor = "#7e65a9";//td
lvlblhd.Text = "<img src='images/spacer.gif' height='8px' width='5px' /><br/>Wake up";
lvlblsubhd.Text = " to a fragrant day..";
lvlbl.Text = "A beautifully fragrance residence";
}
我该有什么关系?我也有使用 lvimg1_Click(NULL,NULL); 和 lvlink1.CssClass =LV-1退休; 是在很多功能这是不会是一个WebMethod
我是新的,所以请让如果你想要更多的信息,我知道
I am new so please make me know if you want many more information
推荐答案
它只能在一瞬间完成的要求。您可以在静态方法服务器控制对象和改变它们,但如果你想的更改是适用于客户端,需要添加客户code从服务器响应过程的字符串。结果
所以通常你可以跳过服务器端方法,并做所有客户端上一次
It can be done only on moment request. You can create server control object in static method and change them, but if you want that changes was apply to client, you need add client code for process string from server response.
So usually you can skip server side method and do all on client at once
更新结果
如果你只需要改变CSS类和图像的URL,你不需要Ajax和Web方法,所有的它,你可以在客户端做一次这样的
UPDATE
If you need only change css class and image url you don't need ajax and web method, all it you can do in client at once like this
<script type="text/jscript">
function imgclick() {
$('#<%= lvlink1.ClientID %>').removeClass().addClass("lv-under1"); //another class
$('#<%= lvimg1.ClientID %>').attr('src','<%= ResolveUrl("~/images/1.jpg") %>');
$('#<%= lvlink2.ClientID %>,#<%= lvlink3.ClientID %>').addClass("lv-under");
$('#<%= lvimg2.ClientID %>, #<%= lvimg3.ClientID %>').attr('src', '<%= ResolveUrl("~/images/spacer.gif") %>');
};
</script>
<img src='images/img1.jpg' onclick='return imgclick();' /> // calling script
<asp:LinkButton ID="lvlink1" OnClick="lvimg1_Click" CssClass="lv-under" runat="server" >
<asp:Image ID="lvimg1" runat="server" ImageUrl="~/images/spacer.gif" />
<asp:LinkButton ID="lvlink2" OnClick="lvimg2_Click" CssClass="lv-under" runat="server" >
<asp:Image ID="lvimg2" runat="server" ImageUrl="~/images/spacer.gif" />
<asp:LinkButton ID="lvlink3" OnClick="lvimg3_Click" CssClass="lv-under" runat="server" >
<asp:Image ID="lvimg3" runat="server" ImageUrl="~/images/spacer.gif" />
如果使用这种情况下 - 不需要Web方法
if use this case - don't need web methods
UPDATE2
function imgclick() {
...
$('#<%= lvlblhd.ClientID %>').removeClass().addClass("detailheader").html("<img src='images/spacer.gif' height='8px' width='5px' /><br/>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");
...
}
这篇关于调用函数或服务器控件的WebMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!