C# 在gridview中动态添加单击到imagebutton
我试图从gridview的页脚点击ImageButton触发事件,但没有触发, 我会感谢你的帮助, 提前感谢,, 这是密码C# 在gridview中动态添加单击到imagebutton,c#,asp.net,web-applications,imagebutton,C#,Asp.net,Web Applications,Imagebutton,我试图从gridview的页脚点击ImageButton触发事件,但没有触发, 我会感谢你的帮助, 提前感谢,, 这是密码 protected void grvBubDetails_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Footer) { ImageButton imgbtnSendMail = new Image
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";
}
您必须在全局范围(类级别)上声明
ImageButton
像这样更新grvBubDetails_rowdabund事件
ImageButton imgbtnSendMail = new ImageButton();
imgbtnSendMail.CommadName = "cmdSendMail"; // add this line
imgbtnSendMail.CommadArgument = "also you can pass a parameter from here";
将RowCommand事件添加到网格视图。在RowCommand事件处理程序函数中执行此操作
if(e.CommandName.equals("cmdSendMail")){
string Text = "Image clicked";
string argument = e.CommandArgument;
}
更新:
在PageLoad事件之后触发网格视图的RowCommand事件。每次重新加载页面后,您的按钮都会被删除,并且由于未触发rowDatabound事件,因此未重新创建该按钮
工作代码:
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());
}
}
public分部类\u默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
列表=新列表()
{
“项目1”,
“项目2”,
“项目3”
};
GridView1.DataSource=列表;
GridView1.DataBind();
}
//确保在外部!i返回
//每次加载页面时重新创建按钮
按钮btn=新按钮();
btn.CommandName=“cmdSendMail”;
btn.CommandArgument=“示例参数”;
btn.Text=“发送邮件”;
GridView1.FooterRow.Cells[0].Controls.Add(btn);
}
受保护的void GridView1_row命令(对象发送方,GridViewCommandEventArgs e)
{
Write(例如CommandName+newrandom().Next().ToString());
}
}
我也尝试过这个,但它没有进入grvBubDetails\u row命令中!!!这是因为它是一个页脚吗?我以前用一个按钮做过类似的代码,但用ImageButton我不知道为什么它不工作,页面和网格视图控件的EnableViewState为true?如果在页面加载事件中有DataIndig代码,请确保它在If(!IsPostBack)块中。是,这是真的,我在If(!IsPostBack)块中为网格绑定尝试在Datarow中添加相同的按钮。我目前在mac电脑上,无法运行.net代码。。。
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());
}
}