0

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";
}

2 Answers 2

2

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());
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

I also tried this, but it doesn't go inside grvBubDetails_RowCommand !!! Is that because it is a footer??
I did similar code before with a Button but with ImageButton I don't know why it doesn't work
Is EnableViewState true for page and grid view control? If you have databindig code in page load event, make sure it is in if(!IsPostBack) block.
yes it is true, and I make bound for the grid inside if(!IsPostBack) block
Try adding same button inside Datarow. i am currently on a mac computer and i can not run .net code ...
|
0

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)
     {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.