繁体   English   中英

如何从另一个类访问ImageButton控件?

[英]How to access ImageButton control from another class?

JS代码:

<script type="text/javascript">
         function ShowCurrentTime(name) {
         PageMethods.GetCurrentTime(name, OnSuccess);
         }
         function OnSuccess(response, userContext, methodName) {
          alert(response);
         }
</script>

HTML代码:

<asp:ImageButton ID="IMGBTN001" runat="server" ImageUrl="Images/ico/labaniat.png"
class="img-responsive em-img-lazy" OnClientClick="ShowCurrentTime('01')" />

<asp:Image class="img-responsive retina-img em-img-lazy" ID="IMGMostViewed" runat="server"ImageUrl="Images/Banner/block1_banner.jpg" />

C#背后的代码

[System.Web.Services.WebMethod]
public static string GetCurrentTime(string name)
{
    //string x = IMGMostViewed.ImageUrl;
    return "Hello " + name + Environment.NewLine + "The Current Time is: "
            + DateTime.Now.ToString();
}

我想从另一个类访问Image。

我如何访问IMGMostViewed这个GetCurrentTime类?

我使用了这段代码,但是得到“ page.FindControl(“ IMGMostViewed”)“返回null

    [System.Web.Services.WebMethod]
public static string GetCurrentTime(string name)
{
    if (HttpContext.Current != null)
    {
        Page page = (Page)HttpContext.Current.Handler;
        Image IMGMostViewed = (Image)page.FindControl("IMGMostViewed");
        string x = IMGMostViewed.ImageUrl;
    }
    return "Hello " + name + Environment.NewLine + "The Current Time is: "
            + DateTime.Now.ToString();
}

从理论上讲,您可以将CurrentHandler强制转换为您的页面类型,然后访问您的按钮:

var currentHandler = HttpContext.Current.CurrentHandler as T;
currentHandler.IMGBTN001.ImageUrl = "abc";

更好的方法是在成功功能中访问客户端上的按钮。

     function ShowCurrentTime(name) {
      PageMethods.GetCurrentTime(name, OnSuccess);
     }
     function OnSuccess(response, userContext, methodName) {
      //Access here your button and modify it
     }

在这里您还将找到相关的答案: 如何访问静态Web方法中的页面控件?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM