简体   繁体   English

在gridview中使用imagebutton在代码后面调用functon

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

I have an ImageButton within a GridView in .aspx on clicking this ImageButton i have to call a function. 单击ImageButton我在.aspx的GridView中有一个ImageButton ,我必须调用一个函数。 This is how i tried and the function was not being called. 这是我尝试过的方法,没有调用该函数。 Code inside.aspx page: 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>

code in .aspx.cs page: .aspx.cs页中的代码:

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

Because you are wrapping your ImageButton inside of a Hyperlink, the browser is probably going to the hyperlink's URL instead of posting back to hit the OnClick function. 因为您将ImageButton包装在超链接中,所以浏览器可能会转到超链接的URL,而不是回发以单击OnClick函数。 You should have the DeleteUrlImageButton_Click function call Server.Transfer or Response.Redirect to the appropriate URL and get rid of the Hyperlink. 您应该具有DeleteUrlImageButton_Click函数,调用Server.Transfer或Response.Redirect到相应的URL,并摆脱超链接。

Sure it won't be fired because it is nested in a Hyperlink. 确保它不会被触发,因为它嵌套在超链接中。 So the imagebutton serves as the text for the hperlink and hyperlink does not cause postback. 因此,imagebutton用作hperlink的文本,而超链接不会引起回发。 ImageButton can cause the desired action only if it stands out of the Hyperlink. 仅当ImageButton在超链接中脱颖而出时,它才能引起所需的操作。 Try this: 尝试这个:

<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 can do the job no need for Hyperlink just use the postbackurl and it will redirect you to the page. ImageButton无需超链接即可完成此工作,只需使用postbackurl,它将把您重定向到页面。 You can omit the HyperLink. 您可以省略HyperLink。 Button controls (like LinkButton,ImageButton and Button) are designed to cause postback by default. 默认情况下,按钮控件(如LinkBut​​ton,ImageButton和Button)旨在引起回发。

Edit : Make sure event name and arguments are correct. 编辑 :确保事件名称和参数正确。 This is the event I used to test it. 这是我用来测试的事件。 y the way don't forget to place the ImageButton in a TemplateField, refer the code above 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.

相关问题 使用代码隐藏控制ImageButton中的名称属性 - Control the Name Attribute within an ImageButton using code-behind 使用后面的代码对Gridview中的列进行重新排序 - Reorder column in Gridview using code behind 在asp.net中,要启用/禁用图像按钮,如何在asp.net代码后面的文件中访问Gridview-&gt; Itemtemplate-&gt;面板的ImageButton控件 - In asp.net, To enable/disable imagebutton, How can I access ImageButton control of Gridview->Itemtemplate->panel in asp.net code behind file 如果ImageButton是在后面的代码中创建的,如何使用ImageButton.OnClick - how to use an ImageButton.OnClick if the ImageButton was created in code behind 如何在C#中的代码中使用ImageButton - How to work with ImageButton in code behind in C# 从ImageButton传递参数到后面的代码 - passing a parameter from a ImageButton to code behind 从后面的代码以及aspx代码中的函数调用gridview textbox textchanged事件 - calling gridview textbox textchanged event from a function from code behind as well as aspx code 通过使用后面的代码在Gridview中为DropDownList设置数据源时获取错误 - Get error when set datasource for DropDownList in Gridview by using code behind 在背后的代码中将GridData的BindData绑定到ITemplate - BindData to ITemplate for GridView in code behind 通过后面的代码更新GridView - Updating GridView through code behind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM