落后在GridView使用的ImageButton调用code一functon [英] Calling a functon in code behind by using an imagebutton in a gridview
问题描述
我有一个的ImageButton
A GridView控件
中的.aspx上的点击的这种内的ImageButton
我要调用一个函数。
这是我的尝试和功能没有被调用。
code inside.aspx页:
< GridView的......>
< ASP:超链接ID =HyperLink2=服务器
NavigateUrl ='&下;%#DataBinder.Eval的(的Container.DataItemVehID,mngVeh.aspx delid = {0}?)%>'>
< ASP:ImageButton的=服务器ID =DeleteUrlImageButton
宽度= '24'高度= '24'
的ImageUrl =〜/图片/ delete.jpeg
的OnClick =DeleteUrlImageButton_Click
的OnClientClick =返回确认(你确定要删除吗?'); />
<! - < IMG SRC =图像/ delete.jpegALT =编辑BORDER =0WIDTH = '24'高度= '24'/> - >
< / ASP:超链接>
< / GridView的>
code在.aspx.cs页:
公共无效DeleteUrlImageButton_Click(对象发件人,EventArgs的发送)
{
// code执行必要的行动。
}
当然,因为它是嵌套在一个超链接不会被解雇。所以的ImageButton作为文本的hperlink和超链接不会导致回发。的ImageButton可导致所需的操作,只有当它代表了超链接的。试试这个:
< ASP:GridView控件....
< ASP:的TemplateField ShowHeader =FALSE>
<&ItemTemplate中GT;
< ASP:ImageButton的=服务器ID =DeleteUrlImageButton
宽度= '24'高度= '24'
的ImageUrl =〜/图片/ delete.jpeg
的OnClick =DeleteUrlImageButton_Click
的OnClientClick =返回确认(你确定要删除吗?');
一项PostBackUrl ='<%#DataBinder.Eval的(的Container.DataItem,VehID,?mngVeh.aspx delid = {0})
%GT;/>
< / ItemTemplate中>
< / ASP:的TemplateField>
< / ASP:GridView的>
的ImageButton可以做的工作不需要超链接只是使用一项PostBackUrl,它会将您重定向到该页面。可以省略的超级链接。
Button控件(如LinkButton的,ImageButton的和Button)旨在默认回发。
修改:确保事件名称和参数是否正确。这是我用来测试它的事件。 Ÿ途中不要忘了放置在的ImageButton一个TemplateField,请参阅上述
在code 保护无效DeleteUrlImageButton_Click(对象发件人,ImageClickEventArgs E)
{
TextBox5.Text =炒鱿鱼; //Response.Redirect(((ImageButton的)寄件人).PostBackUrl); //取消注释这如果按钮不会自动重定向。此行应该是最后一个
}
I have an ImageButton
within a GridView
in .aspx on clicking this ImageButton
i have to call a function.
This is how i tried and the function was not being called.
Code inside.aspx page:
<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:
public void DeleteUrlImageButton_Click(object sender, EventArgs e)
{
//code to perform the necessary action.
}
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 can cause the desired action only if it stands out of the Hyperlink. 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. You can omit the HyperLink. Button controls (like LinkButton,ImageButton and Button) are designed to cause postback by default.
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
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
}
这篇关于落后在GridView使用的ImageButton调用code一functon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!