2012-02-10 56 views
-2
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" ChildrenAsTriggers="true" 
runat="server"> 
    <ContentTemplate> 
    <asp:ImageButton ID="imagebutton1" runat="server" ImageUrl="~/image.jpg" Width="125" 
    Height="125" onclick="imagebutton1_Click" /> 
    <asp:Timer ID="timer1" runat="server" Interval="10000" ontick="timer1_Tick" /> 
    <asp:Label ID="label1" Visible="false" runat="server" Text="" /> 
    </ContentTemplate> 
</asp:UpdatePanel> 

如果我不使用更新面板,整個頁面將刷新。Imagebutton onclick不能在updatepanel中工作,我怎樣才能使它工作?

protected void imagebutton1_Click(object sender, ImageClickEventArgs e){ 
    string link = label1.Text; 
    Page.ClientScript.RegisterStartupScript(this.GetType(), "OPEN", "window.open(" + link + 
    ",'mywindow','width=200,height=200,');", true); 
} 

這是變量「鏈接」在這裏的定時器處理

Random r = new Random(); 

if (datatable1.Rows.Count > 0) 
{ 
    int randomnumber = r.Next(0, i); 
    DataRow datarow1= datatable1.Rows[randomnumber ]; 
    imagebutton1.ImageUrl = (String)datarow1["image"]; 
    imagebutton1.DataBind(); 
    label1.Text = (String)datarow1["Link"]; 
} 
+0

你可以發佈一些代碼,你的'Page_Load'方法的一部分和ImageButton設置(可能來自設計器源文件)的設置? – twilson 2012-02-10 20:49:11

+2

這是你的第12個問題,與此同時,你應該注意到一個問題應該包含超過半句話和「不工作」。請提供確切的錯誤/錯誤行爲和相關代碼。 – 2012-02-10 20:51:58

+0

@TimSchmelter它不調用代碼隱藏中的onclick事件。 – Seesharp 2012-02-10 20:55:36

回答

2

我只加回傳觸發作爲UpdatePanel的圖所示,使其工作。

<Triggers> 
<asp:PostBackTrigger ControlID="ImageButton"/> 
</Triggers> 
2

這是困難的,因爲你沒有說真正有一個問題,但如果你想要一塊的客戶端代碼在UpdatePanel刷新時運行,請使用客戶端pageLoad() method

另外,您正在使用服務器端事件來執行客戶端操作。使用OnClientClick處理程序來代替:

<asp:ImageButton ID="imagebutton1" runat="server" ImageUrl="~/image.jpg" Width="125" 
    Height="125" OnClienClick="return openPopUp(this.href);" /> 

function openPopUp(link) 
{ 
    window.open(this.href,'mywindow','width=200,height=200,'); 
    return false; 
} 
+0

我想調用imagebutton1_Click,當我點擊圖像按鈕時,它不會觸發 – Seesharp 2012-02-10 20:59:33

+0

我需要它在服務器中,因爲我需要從數據庫獲取圖像和鏈接,並且onclientclick在updatepanel中仍然不起作用。 – Seesharp 2012-02-10 21:06:59

0

嘗試改用UpdateMode="Always"

相關問題