[英]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.
默认情况下,按钮控件(如LinkButton,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.