簡體   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