Mufaddal
Mufaddal

Reputation: 566

Row Command Event not fired for Dynamically created buttons inside gridView

I have a GridView In which I am dynamically creating Label and Button on gv_RowDataBound.

.cs

Label Tagvalue = new Label();
Tagvalue.ID = "lblTags_"+i;
//Tagvalue.CssClass = "tagBubble tagBubbleDelete";
Tagvalue.Text = tag;

Button delete = new Button();
delete.ID = "btnDelete_" + i;
delete.ToolTip = "Delete";
delete.CssClass = "tagBubbleDelete";
delete.CommandName = "Delete";
delete.CommandArgument = imageID.Text;

The problem is that the "RowCommand" function is not getting fired. So I am not able to reach e.CommandName=="Delete".

What can be done to fire the RowCommand event for a Dynamically Created button inside a GridView.

Any help is Appreciated.

*******EDIT

.aspx

 <asp:GridView runat="server" ID="myGV" AllowSorting="false" AllowPaging="False"
                        PageSize="20" AutoGenerateColumns="False"  

                        DataSourceID="LDSgv" 
                        ondatabound="gv_DataBound"
                        OnRowDataBound="gv_RowDataBound"
                        OnRowCommand="gv_RowCommand"
                        >
  <asp:TemplateField HeaderText="Details" HeaderStyle-CssClass="noSort"
                                    ItemStyle-CssClass="fieldAlignCenter">
                                    <ItemTemplate>
                                      <div class="alignLeft marginTop5 float-l" id="Tags" runat="server"                         style="clear:both">
                                          //**Here I am adding the dynamic Buttons  
                                        </div>
                                       </ItemTemplate>
                                     </asp:TemplateField>
                                </asp:GridView>

Upvotes: 2

Views: 3348

Answers (3)

Mufaddal
Mufaddal

Reputation: 566

Feels good answering own question. I tried the following:

    //**OnPage Load
     if (IsPostBack)
        {
            //String ButtonID = Page.Request.Params["__EVENTTARGET"];

            Control c = GetPostBackControl(this.Page);
            if (c != null)
            {
                //...
                var ID = c.ID;
            }
            else
            {
                Mygv.DataBind();
            }
        }

     protected Control GetPostBackControl(Page page)
      {
        Control control = null;

        string ctrlname = page.Request.Params.Get("__EVENTTARGET");
        if (ctrlname != null && ctrlname != string.Empty)
        {
            control = page.FindControl(ctrlname);
        }
        else
        {
            foreach (string ctl in page.Request.Form)
            {
                Control c = page.FindControl(ctl);
                if (c is System.Web.UI.WebControls.Button)
                {
                    control = c;
                    break;
                }
            }
        }
        return control;
    }

Here I am first finding the control which causes the PostBack. If the control is (not NULL) i.e(if not a dynamically created) control then do not PostBack. Else PostBack for dynamic created controls.

Upvotes: 1

user4058759
user4058759

Reputation:

try this

 protected void Page_Load(object sender, EventArgs e)
    {
       if (!IsPostBack) 
       {
        gridbind();
       }      
    }

Upvotes: 1

yogi970
yogi970

Reputation: 446

The problem is that you are creating the button gv_RowdataBound event and when you click on the button the page gets reloaded and button is not created this time because you have written code in the gv_RowDataBound event.So button wil not work because there is no button!.

You should try like this

protected void Page_Init(object sender, EventArgs e)
    {
        GridView1.DataBind(); //Key code
    }

    protected void gv_DataBound(object sender, EventArgs e)
    {
        
                Button delete = new Button();
               delete.ID = "btnDelete_" + i;
                delete.ToolTip = "Delete";
               delete.CssClass = "tagBubbleDelete";
               delete.CommandName = "Delete";
               delete.CommandArgument = imageID.Text;
            
        
    }

    protected void gv_RowCommand(object sender, CommandEventArgs e) //Your grid view function change accroding you need!!
    {
        if (e.CommandName == "Delete")
        {

        }
    }

I am not sure that it will work 100% but at least you shuld try this

Update

If you are binding the gridview at page_load then you have to use this code

 protected void Page_Load(object sender, EventArgs e)
        {
           if (!IsPostBack) 
           {
            YourGridviewbindCode(); //Key code
            }      
        }

Because if you every time bind the gridview it may cancel out the effect of Button click!

Upvotes: 1

Related Questions