簡體   English   中英

在gridview中使用imagebutton在代碼后面調用functon

[英]Calling a functon in code behind by using an imagebutton in a gridview

單擊ImageButton我在.aspx的GridView中有一個ImageButton ,我必須調用一個函數。 這是我嘗試過的方法,沒有調用該函數。 Inside.aspx頁面中的代碼:

<GridView ......>
    <asp:HyperLink ID="HyperLink2" runat="server" 
        NavigateUrl='<%# DataBinder.Eval(Container.DataItem,"VehID","mngVeh.aspx?delid={0}") %>'>   
        <asp:ImageButton runat="server" ID="DeleteUrlImageButton" 
            width='24' height='24'
            ImageUrl="~/images/delete.jpeg" 
            OnClick="DeleteUrlImageButton_Click"
            OnClientClick="return confirm('Are you sure you want to delete?');" />
        <!--<img src="images/delete.jpeg" alt='edit' border="0" width='24' height='24'/> -->
   </asp:HyperLink>
</GridView>

.aspx.cs頁中的代碼:

public void DeleteUrlImageButton_Click(object sender, EventArgs e)
{
    //code to perform the necessary action.
}

因為您將ImageButton包裝在超鏈接中,所以瀏覽器可能會轉到超鏈接的URL,而不是回發以單擊OnClick函數。 您應該具有DeleteUrlImageButton_Click函數,調用Server.Transfer或Response.Redirect到相應的URL,並擺脫超鏈接。

確保它不會被觸發,因為它嵌套在超鏈接中。 因此,imagebutton用作hperlink的文本,而超鏈接不會引起回發。 僅當ImageButton在超鏈接中脫穎而出時,它才能引起所需的操作。 嘗試這個:

<asp:GridView ....
<asp:TemplateField ShowHeader="False">
    <ItemTemplate>
     <asp:ImageButton runat="server" ID="DeleteUrlImageButton" 
        width='24' height='24'
        ImageUrl="~/images/delete.jpeg" 
        OnClick="DeleteUrlImageButton_Click"
        OnClientClick="return confirm('Are you sure you want to delete?');" 
  PostBackUrl='<%# DataBinder.Eval(Container.DataItem,"VehID","mngVeh.aspx?delid={0}")
 %>'/>
  </ItemTemplate>
  </asp:TemplateField>
</asp:GridView>

ImageButton無需超鏈接即可完成此工作,只需使用postbackurl,它將把您重定向到頁面。 您可以省略HyperLink。 默認情況下,按鈕控件(如LinkBut​​ton,ImageButton和Button)旨在引起回發。

編輯 :確保事件名稱和參數正確。 這是我用來測試的事件。 y別忘了將ImageButton放在TemplateField中,請參考上面的代碼

protected void DeleteUrlImageButton_Click(object sender, ImageClickEventArgs e)
{
    TextBox5.Text = "Fired ";

 //Response.Redirect( ((ImageButton)sender).PostBackUrl);//uncomment this if the button does not automatically redirects. This line should be the last one 
}

暫無
暫無

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

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