2
<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();
}
我想從另一個類訪問圖像。
如何訪問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();
}
謝謝您的回答。 HttpContext.Current.CurrentHandler中的'T'是什麼? – mohammad
T應該代表你的頁面,然後你有一個你的頁面的實例,你可以訪問你的imagebutton :) –
我使用這個,但得到空引用 – mohammad