2

JS Code:

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

HTML Code:

<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" />

Code Behind 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();
}

I want to access the Image from another class.

How can I access the IMGMostViewed this GetCurrentTime class?

i used this code, but get "page.FindControl("IMGMostViewed")" return 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();
}
mohammad
  • 360
  • 3
  • 8

1 Answers1

0

Theoreticaly you can cast the CurrentHandler to your type of page and then access your button:

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

The beter way would be to access your button on clientside in your success function.

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

Here you also will find a related Answer: How to access page controls inside a static web method?

Community
  • 1
  • 1
Cyril Iselin
  • 596
  • 4
  • 20