2011-09-27 124 views
0

我有一個鏈接按鈕裏面的asp.net中繼器控制。我試圖在點擊事件上調用serverside方法,但沒有運氣。我試過html錨,但它不工作,所以我切換到鏈接按鈕。asp.net鏈接按鈕沒有射擊

<ItemTemplate> 
<li class="showmenu"> 
    <p class="subtext">&nbsp;&nbsp;<asp:LinkButton ID="LinkButton1" runat="server" onclick="frontimagechange_click">Front</asp:LinkButton></p> 
    <a href="#"><img id="Img1" src='<%# this.ResolveUrl("~/testimages/" + Eval("front")) %>' width="350" height="560" alt='<%# Eval("stylenumber") %>' runat="server" align="left" /></a>        
</li>     
</ItemTemplate> 

服務器端代碼:

protected void frontimagechange_click(object sender, EventArgs e) 
{ 
//code to get the id of link button and change the 
//src of the image control inside the repeater 
} 
+0

@rahul的大部分我還沒有得到的答案和我嘗試了一些其他的方式來解決這個問題的問題,它不會工作。 – sam

+0

你確定你沒有得到答案。其實我檢查了你的問題,他們至少有一個或兩個答案,除了網格一個outofindex錯誤 – rahularyansharma

回答

3

您需要處理ItemCommand事件Repeater控制。

數據控件,如Repeater,DataList,GridView,FormView和DetailsView控件使用Forwarded events

總結:

而不是每個按鈕單獨養,從 嵌套控件事件被轉發到容器控制的事件。容器 反過來引發一個通用的ItemCommand事件,該事件的參數允許您發現哪個單獨的控件引發了原始事件 。通過 對此單一事件做出響應,您可以避免必須爲子控件編寫單個事件處理程序 。

演示:

標記(的.aspx)

<asp:Repeater ID="Repeater1" runat="server" 
    onitemcommand="Repeater1_ItemCommand"> 
    <ItemTemplate> 
     <asp:LinkButton 
      ID="LinkButton1" 
      runat="server" 
      CommandName="cmd" 
      >Click Me</asp:LinkButton> 
    </ItemTemplate> 
</asp:Repeater> 

在代碼隱藏文件,

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) 
{ 
    if (e.CommandName == "cmd") 
    { 
     LinkButton button = e.CommandSource as LinkButton; 

    } 
} 
+0

我需要得到linkbutton的id或獨特的東西來改變圖像控制的src。如何通過ItemCommand – sam

+0

@sam在linkbutton上實現這一點,設置CommandName和CommandArgument。在ItemCommand處理程序中,捕獲並查找CommandArgument值。文檔鏈接應該解釋更多。 –

+0

@Blake問題是如果linkbutton被觸發,那麼我可以捕獲CommandArgument。但它並沒有開火。 – sam

1

是autoEventWireUp =在你的頁面是真的嗎?

是否啓用了轉發器或頁面的視圖狀態。

如果視圖狀態是關閉