Reputation: 3
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";
}
Upvotes: 0
Views: 7515
Reputation: 319
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";
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;
}
Update:
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.
working code:
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());
}
}
Upvotes: 2
Reputation: 148110
You have to declare the ImageButton
on global scope (class level)
ImageButton imgbtnSendMail = new ImageButton();
protected void grvBubDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
Upvotes: 0