动态添加点击到gridview中的图像按钮 [英] Add click to imagebutton inside gridview dynamically
问题描述
我试图触发事件单击从gridview的页脚中单击ImageButton的事件,但是我没有触发, 我将不胜感激, 提前致谢, 这是代码
I tried to fire the event click for an ImageButton from the footer of a gridview but I it didn't fire, I will appreciate any help, thanks in advance, here is the code
protected void grvBubDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
ImageButton imgbtnSendMail = new ImageButton();
//ImageButton imgEdit = new ImageButton();
imgbtnSendMail.ImageUrl = "~/Images/mail01.png";
imgbtnSendMail.AlternateText = "Edit";
imgbtnSendMail.ToolTip = "for send mail press here";
imgbtnSendMail.Width = imgbtnSendMail.Height = 20;
//imgbtnSendMail.CommandName = "sendMail";
//imgbtnSendMail.CommandArgument = "somevalue";
imgbtnSendMail.Click += new ImageClickEventHandler(imgbtnSendMail_Click);
//imgbtnSendMail.Attributes["runat"] = "server";
//imgbtnSendMail.Attributes["onclick"] = "imgbtnSendMail_Click";
e.Row.Cells[6].Controls.Add(imgbtnSendMail);
}
}
protected void imgbtnSendMail_Click(object sender, ImageClickEventArgs e)
{
string Text = "Image clicked";
}
推荐答案
像这样更新grvBubDetails_RowDataBound事件;
update grvBubDetails_RowDataBound event like this;
ImageButton imgbtnSendMail = new ImageButton();
imgbtnSendMail.CommadName = "cmdSendMail"; // add this line
imgbtnSendMail.CommadArgument = "also you can pass a parameter from here";
将RowCommand事件添加到网格视图.在RowCommand事件处理函数中执行此操作;
Add RowCommand event to grid view. Do this in RowCommand event handler function;
if(e.CommandName.equals("cmdSendMail")){
string Text = "Image clicked";
string argument = e.CommandArgument;
}
更新:
在PageLoad事件之后触发了网格视图的RowCommand事件.您的按钮在每次重新加载页面后都被删除,并且由于rowDatabound事件不会触发而没有重新创建.
Grid view's RowCommand event fired after PageLoad event. Your button removed after every page reload, and has not recreated because rowDatabound event does not fire.
工作代码:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<string> list = new List<string>()
{
"item 1",
"item 2",
"item 3"
};
GridView1.DataSource = list;
GridView1.DataBind();
}
// make sure outside of !IsPostback
// recreate button every page load
Button btn = new Button();
btn.CommandName = "cmdSendMail";
btn.CommandArgument = "sample arg";
btn.Text = "send mail";
GridView1.FooterRow.Cells[0].Controls.Add(btn);
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Response.Write(e.CommandName + new Random().Next().ToString());
}
}
这篇关于动态添加点击到gridview中的图像按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!