在WebMethod中访问runat =“server”控件ID

时间:2013-11-07 09:20:18

标签: c# jquery asp.net

html代码是

<script type="text/jscript">
function ajaxcall() {
     $.ajax({
     type: "POST",
     url: "index.aspx/lvimgclick",
     contentType: "application/json; charset=utf-8",
     data: JSON.stringify({ value: "test" }),
     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" />

<asp:LinkButton ID="lvlink4" OnClick="lvimg4_Click" CssClass="lv-under" runat="server"  >
<asp:Image ID="lvimg4" runat="server" ImageUrl="~/images/spacer.gif" />

.cs代码

[WebMethod]
public static string lvimgclick()
{      
    return "hi";
}

protected void lvimg1_Click(object sender, EventArgs e)
{  

    lvlink1.CssClass = "lv-under";
    lvimg1.ImageUrl = "~/images/1.jpg";

    lvlink2.CssClass = "lv-under";
    lvimg2.ImageUrl = "~/images/spacer.gif";

    lvlink3.CssClass = "lv-under";
    lvimg3.ImageUrl = "~/images/spacer.gif";


    lvlink4.CssClass = "lv-under";
    lvimg4.ImageUrl = "~/images/spacer.gif";
}

想要的是:

[WebMethod]
public static string lvimgclick()
{      
    lvimg1_Click(null, null);
    return "hi";
}

[WebMethod]
public static string lvimgclick()
{      
    lvlink1.CssClass = "lv-under";
    lvimg1.ImageUrl = "~/images/1.jpg";

    lvlink2.CssClass = "lv-under";
    lvimg2.ImageUrl = "~/images/spacer.gif";

    lvlink3.CssClass = "lv-under";
    lvimg3.ImageUrl = "~/images/spacer.gif";


    lvlink4.CssClass = "lv-under";
    lvimg4.ImageUrl = "~/images/spacer.gif";
    return "hi";
}

该怎么办?还必须在许多函数中使用 lvimg1_Click(null,null); lvlink1.CssClass =“lv-under1”; 不是webmethod < /强>

1 个答案:

答案 0 :(得分:1)

如果你使用jquery,你可以这样做

 $.ajax({
   type: "POST",
   url: "index.aspx/lvimgclick",
   contentType: "application/json; charset=utf-8",
   data: JSON.stringify({ value: "test" }),
   dataType: "json", 
   success:function(result){

     if(result.cssclass)
     {
       $('#<%= lvlink1.ClientID %>').addClass(result.cssclass);
     }
   }     
  });

在WebMethod

   public class CustomResult
    {
        public string output{ get; set; }
        public string cssclass{ get; set; }
    }

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static CustomResult lvimgclick()
{      
    var result=new CustomResult{output="hi", cssclass="lv-under1"}

    return result;

}

注意:

有时您可能需要使用d来访问数据,这是由框架添加的。比如result.d.cssclass

修改

如果你想更新多个链接按钮,请在aspx中为它定义一个通用的css类。

<asp:LinkButton ID="lnk1" runat="server" CssClass="menu" Text="Home" />
<asp:LinkButton ID="lnk2" runat="server" CssClass="menu" Text="AboutUs" />

然后在你的ajax调用的成功回调中使用类选择器并更新。

  success:function(result){

     if(result.cssclass)
     {
       $('a.menu').addClass(result.cssclass);
     }
   }