[英]Link Button in asp.net click event not firing
<asp:LinkButton ID="lbDownloadFile" name = "lbDownloadFile" runat="server" CausesValidation="false"
onclick="lbDownloadFile_Click" />
我有這個鏈接按鈕。 點擊:
protected void lbDownloadFile_Click(object sender, EventArgs e)
{ //here is my debug pointer/breakpoint
.........................
}
但是這個事件沒有解雇。 我的Page_Load()事件正在觸發。 如何解決這個問題呢?
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
if (Session[Antrage_AnfrageSessionNames.AgntNr] == null)
{
Response.Redirect("../UserSessionError.aspx");
}
try
{
if (Request.QueryString["Kundennummer"].ToString() != null)
{
//If kundennummer exists in QueryString then stores it to further use
kundennummer = Request.QueryString["Kundennummer"].ToString();
}
}
catch
{
kundennummer = string.Empty;
}
}
}
編輯:
我正在添加代碼,FireFox firebug向我展示了LinkButton。
我認為自動生成的href是這里的主要問題。
@belogix評論很好
這是正常ASP .NET WebForms頁面生命周期的一部分...每次發生回發時都會調用頁面加載。 在PageLoad之后你的事件應該開始......但是你在頁面加載中做了什么可以阻止這種情況發生嗎?
我認為你的頁面加載方法做錯了什么。 可能是您的鏈接按鈕是從頁面加載事件重新加載。
示例錯誤
如果您使用網格視圖以及網格內部的此鏈接按鈕,那么您正在執行此操作
寫入網格綁定方法
然后你在頁面加載事件中調用了網格綁定方法
你的代碼現在看起來像
Page_load()
{
// called here Grid bind method
}
現在,網格重新加載每個帖子。
解
現在你必須設置!IsPostBack
,然后調用!IsPostBack
里面的網格綁定方法
代碼看起來像
Page_load()
{
if(!IsPostBack)
{
// called here Grid bind method
}
}
這是你的問題。 而且這是我的猜測。
請告訴我你是否使用任何控件(Gridview,listview等)
編輯
如果我沒有在頁面加載事件上編寫任何代碼,那么您的代碼對我有用
看到
Default.aspx的
<asp:LinkButton ID="lbDownloadFile" Text="he he he" name="lbDownloadFile" runat="server" CausesValidation="false" OnClientClick="lbDownloadFile_Click"
OnClick="lbDownloadFile_Click" />
和服務器端代碼是
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Empty code
}
protected void lbDownloadFile_Click(object sender, EventArgs e)
{
}
}
這對我來說很好,所以你錯過了頁面加載事件中的任何內容
* OnClientClick
和onclick
沒有任何問題。 在pageload事件中創建的問題,
請發布您的pageload
代碼,否則我們無法解決。 :)
請檢查鏈接按鈕是否為表單元素的外部。 此鏈接按鈕應位於表單元素內
你的頁面加載事件應該是
protected void Page_Load(**object sender, EventArgs e**) { //Code }
不
protected void Page_Load(){}
你錯過了
object sender, EventArgs e
我已經創建了演示項目並復制了相同的代碼。它工作正常
<asp:LinkButton ID="lbDownloadFile" name = "lbDownloadFile" Text="Click me" runat="server" CausesValidation="false" OnClientClick="lbDownloadFile_Click"
onclick="lbDownloadFile_Click" />
在代碼后面的文件
protected void lbDownloadFile_Click(object sender, EventArgs e)
{ //here is my debug pointer/breakpoint
}
我剛剛在鏈接按鈕上添加了文字。
只需轉到按鈕屬性並設置即可
UseSubmitBehaviour= False
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.