2015-06-25 62 views
1

我需要在ASP.net中創建一個可以嵌入到另一個網頁的按鈕。在ASP.net中創建一個嵌入式圖像按鈕?

本質上,按鈕將在頁面加載時查詢數據庫服務器端,然後根據查詢返回更改其圖像。

有沒有人知道最好的方法呢?你可以告訴我,我不是專業的ASP.net。最好用C#編寫。

+0

不知道從哪裏開始。我只想弄清楚如何製作一個只有一個按鈕的簡單視圖。我可以確保編碼...但我不能爲我的生活得到一個模板,只是按鈕,所以我可以點擊它並編寫數據庫連接服務器端+所有其他的東西。 – willxj

回答

1

在這種情況下,我會建議使用IHttpHandler而不是通用WebForm。 https://msdn.microsoft.com/en-us/library/system.web.ihttphandler(v=vs.110).aspx

處理程序更適合此請求,因爲它可以快速響應,並且是專門爲處理不一定基於HTML的特定請求而構建的。這可以非常簡單地連接以接受請求,查詢數據庫並生成您選擇的圖像。現在你還沒有提供關於圖像來自哪裏的很多信息,但讓我們看看一個簡單的請求。

要從webforms web應用程序中開始,請選擇一個新的GenericHandler,我們將命名爲DynamicImage.ashx。這將構建我們的初始模板如下。

public class DynamicImage : IHttpHandler 
{ 

    public void ProcessRequest(HttpContext context) 
    { 
     context.Response.ContentType = "text/plain"; 
     context.Response.Write("Hello World"); 
    } 

    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 
} 

該模板提供了處理我們請求的基礎知識。當請求到達時,WebServer將執行傳入HttpContextProcessRequest()方法作爲參數。從這裏我們可以用它來提供我們的迴應。

對於參數sakes可以說我們正在查詢的圖像基於QueryString參數username它代表我們的數據庫中的用戶。我已經在您的步驟中包含了一些基本代碼來實現此目的。 (代碼註釋)

public void ProcessRequest(HttpContext context) 
{ 
    //get our username from the query string 
    var username = context.Request.QueryString["username"]; 

    //clear the response and set the content type headers 
    context.Response.Clear(); 
    context.Response.ContentType = "image/png"; 

    //if the username is empty then end the response with a 401 not found status code 
    if (string.IsNullOrWhiteSpace(username)) 
    { 
     context.Response.StatusCode = 401; 
     context.Response.End(); 
     return; 
    } 

    //do a db query to validate the user. If not valid do a 401 not found 
    bool isValidUser = new UserManager().IsValidUser(username); 
    if (!isValidUser) 
    { 
     context.Response.StatusCode = 401; 
     context.Response.End(); 
     return; 
    } 

    //get the user image file path from a server directory. If not found end with 401 not found 
    string filePath = context.Server.MapPath(string.Format("~/App_Data/userimages/{0}.png", username)); 
    if (!System.IO.File.Exists(filePath)) 
    { 
     context.Response.StatusCode = 401; 
     context.Response.End(); 
     return; 
    } 

    //finish the response by transmitting the file 
    context.Response.StatusCode = 200; 
    context.Response.TransmitFile(filePath); 
    context.Response.Flush(); 
    context.Response.End(); 
} 

要調用這個處理程序,你可以簡單地設置圖像的src類似於/DynamicImage.ashx?username=johndoe的路徑。

現在您的要求可能會稍有不同。例如,您可能會從數據庫檢索圖像作爲byte[],因此而不是使用context.Response.TransmitFile()方法,您可能希望使用context.Response.BinaryWrite()方法。該方法傳送byte[]作爲響應流。

最後,我會引用你另一篇文章(我的),談論從客戶端角度緩存這些圖像。如果您的按鈕生成頻率很高,這非常有用。 Leverage browser caching in IIS (google pagespeed issue)

+0

我相信這個解決方案會起作用。基本上,它所查詢的狀態是可用性的「是」或「否」狀態。如果是,則顯示一個圖像,如果沒有顯示另一個圖像。 – willxj

0

它可以作爲

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/arrow-down.gif" OnClick="ImageButton1_Click" /> 

和代碼背後的簡單的東西:

protected void ImageButton1_Click(object sender, ImageClickEventArgs e) 
{ 
    // do some DB processing here 
    ImageButton1.ImageUrl = "~/arrow-up.gif"; 
} 

如果我理解你的要求。

把它放在網頁負載下看起來是這樣的:

private void Page_Load() 
{ 
    if(!Page.IsPostBack) 
    { 
     // perform db processing here 
     ImageButton.ImageUrl = "~/arrow-up.gif"; 
    } 
} 

是所有需要。設置ImageUrl線可以放在任何你需要的地方。

+0

我需要數據庫處理頁面加載...來更改按鈕圖像。 – willxj

+0

@willxj:只需將click事件更改爲pageload事件即可。也包裹在裏面!!IsPostBack' – naveen

+0

naveen是正確的。將這條線放在需要的地方很簡單。我已經相應地更新了答案。 – SteveFerg

相關問題