簡體   English   中英

ASP:Net LinkBut​​ton控件回發問題

[英]ASP:Net LinkButton control Postback issue

我的profile.aspx頁面中有一個asp.net linkBut​​ton(或imageButton)控件。 我正在檢查下面代碼中的頁面中的Request.Querystring(“ id”)。

HTTP:// //localhost:42932/profile.aspx?id=1

當我第一次加載個人資料頁面時,它不會回發。 沒關系!。 當我轉到另一個用戶配置文件(同一頁面上只是查詢字符串是不同的)時, 使用帶有添加地址的imageButton控件;

HTTP:// //localhost:42932/profile.aspx?id=2

它被回發。 我不希望它被寄回。 但是,如果我使用常規的html輸入元素(例如

a href =“ http:// //localhost:42932/profile.aspx?id=2”

它不會回發。 因此,我希望圖像按鈕的行為類似於html輸入元素。

這是我的imageButton;

ASPX:

<asp:ImageButton ID="imgProfile" ImageUrl="images/site/profile1.png" runat="server"/>

.CS

imgProfile.PostBackUrl = "profile.aspx?id=" + Session["userID"];

編輯:

 if (!IsPostBack)
    {
        Session["order"] = 0;
    }

此控件在頁面加載中。 因此,應該使用上面提到的狀態進行回發。 因為在Session [“ order”] = 0時所有其他功能都在工作

利用OnCLientClick代替OnClick ,以便僅運行客戶端代碼。 然后,確定return false;否則, return false;

<asp:ImageButton ID="imgProfile" ImageUrl="images/site/profile1.png" runat="server" OnClientClick="return false;" />

但是,為什么可以使用普通的<img .. html控件來使用服務器控件呢?

我建議您在按鈕單擊事件處理程序中使用Response.Redirect()而不是指定PostBackUrl

public void imgProfile_Click(object sender, eventArgs e){
   Response.Redirect("profile.aspx?id=" + Session["userID"]);
}

或者,只需使用Hyperlink控件並在Page_Load期間設置NavigateUrl屬性:

<asp:HyperLink ID="imgProfile" runat="server"><img src="images/site/profile1.png" /></asp:Hyperlink>

imgProfile.NavigateUrl = "profile.aspx?id=" + Session["userID"];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM