Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

dynamically created button on datagrid is not firing item command

Posted on 2004-08-09
17
Medium Priority
?
741 Views
Last Modified: 2008-01-09
i have a datagrid in which i am dynamically creating template columns. the code i am using to create my delete button is as such..

      btn.Text = "Delete";
      btn.ID = "DeleteButton";
      btn.CommandName = "DeleteMe";
      container.Controls.Add(btn);
                  
the HTML for the datagrid is as such...

<asp:datagrid id=DataGrid1 Width="700px" runat="server" BorderStyle="None" BackColor="White" CellPadding="5" GridLines="Horizontal" AutoGenerateColumns="False" Font-Size="Smaller" Font-Names="Arial" BorderWidth="0px" ShowFooter="True" onItemCommand="DataGrid1_ItemCommand" onItemDataBound="DataGrid1_ItemDataBound" DataKeyField="pay_adjust_id" AllowSorting="True">



the datagrid displays and fills properly. but when i press the delete button, the page reloads and the datagrid is gone. when i debug, it never makes it to the onItemCommand function. Can someone help me figure out why the onItemCommand function is not firing? Thanks!
0
Comment
Question by:trevoray
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 4
  • +2
17 Comments
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11757307
Everything looks right.  Could something else be causing the problem?  Why would the datagrid be gone?  Where are you binding to the datagrid?

-Trevor
0
 

Author Comment

by:trevoray
ID: 11757429
Trevor,

I am binding the datagrid in a seperate function called BindGrid. I am calling this function at the end of my OnItemCommand function. Since the OnItemCommand never fires, not only does the row not get deleted, but datagrid doesn't get bound again on postback either. I'm sure something else could be causing the problem, but my problem is that I don't know what that something else is or how to find out what that something else is. Any ideas? Thanks!

Trevor
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11757458
can you post the code for your datagrid, your event handlers and the bindgrid function?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 8

Accepted Solution

by:
trevorhartman earned 2000 total points
ID: 11757485
could you test by creating a button column and adding that to the datagrid, instead of creating a button and adding it to an existing colum?
0
 
LVL 9

Expert Comment

by:s_sansanwal
ID: 11757504
Instead of a Button, use a ButtonColumn
Write this code in Page_Load before binding the data grid

                  ButtonColumn btn = new ButtonColumn();
                  btn.ButtonType = ButtonColumnType.PushButton;
                  btn.Text = "Delete";
                  btn.CommandName = "DeleteMe";
                  this.DataGrid1.Columns.Add(btn);  


This will display a delete button in front of all rows and pressing them would fire DataGrid1_ItemCommand event
0
 

Author Comment

by:trevoray
ID: 11757511
i am creating a column dynamically. everything works great, except my delete button is not firing the onitemcommand.

if you can show me how to dynamically add a button column to my exisiting code, i will try it, but i don't think it would be compatable with how i have my code setup.

TemplateColumn tc5 = new TemplateColumn();
tc5.HeaderTemplate = new
DataGridTemplate(ListItemType.Header, "", 0, "text", "");
tc5.ItemTemplate = new
DataGridTemplate(ListItemType.Item, "pay_adjust_id", 35, "deletebutton", "mykey");
tc5.FooterTemplate = new
DataGridTemplate(ListItemType.Footer, "Footer Text", 5, "text", "");
tc5.ItemStyle.HorizontalAlign = HorizontalAlign.Right;

DataGrid1.Columns.Add(tc1);
DataGrid1.Columns.Add(tc2);
DataGrid1.Columns.Add(tc3);
DataGrid1.Columns.Add(tc4);
DataGrid1.Columns.Add(tc5);

public class DataGridTemplate : ITemplate
      {
            ListItemType templateType;
            string columnName;
            int textboxsize;
            string columntype;
            string textboxname;
            
            public DataGridTemplate(ListItemType type, string colname, int txtsize, string coltype, string txtname)
            {
                  templateType = type;
                  columnName = colname;
                  textboxsize = txtsize;
                  columntype = coltype;
                  textboxname = txtname;
                  

            }

            public void OnDataBinding(object sender, EventArgs e)
            {
                  
                  Type senderType = sender.GetType();
                  if (senderType == typeof(Literal))
                  {
                        /* do this */
                        Literal lc = (Literal) sender;
                        DataGridItem container = (DataGridItem) lc.NamingContainer;
                        lc.Text = ((DataRowView)container.DataItem)[columnName].ToString();
                  
                  }
                  else if (senderType == typeof(TextBox))
                  {
                        /* do this */
                        TextBox txt = (TextBox) sender;
                        DataGridItem container = (DataGridItem) txt.NamingContainer;
                        txt.Text = ((DataRowView)container.DataItem)[columnName].ToString();
                  }
                  


            }
            
            public void InstantiateIn(System.Web.UI.Control container)
            {
                  Literal lc = new Literal();
                  TextBox txt = new TextBox();
                  Button btn = new Button();    
                  switch(templateType)
                  {
                        case ListItemType.Header:
                              lc.Text = "<B>" + columnName + "</B>";
                              container.Controls.Add(lc);
                              break;
                        case ListItemType.Item:
                        switch(columntype)
                        {
                              case "text":
                                    txt.Columns = textboxsize;
                                    txt.DataBinding += new EventHandler(this.OnDataBinding);
                                    txt.ID = textboxname;
                                    container.Controls.Add(txt);
                                    break;
                              case "deletebutton":
                                    txt.ID = "mykey";
                                    txt.DataBinding += new EventHandler(this.OnDataBinding);
                                    txt.Visible = false;
                                    btn.Text = "Delete";
                                    btn.ID = "DeleteButton";
                                    btn.CommandName = "DeleteMe";
                                    container.Controls.Add(txt);
                                    container.Controls.Add(btn);
                                    break;
                        }
                              break;
                        case ListItemType.Footer:
                              lc.Text = "<I>" + columnName + "</I>";
                              container.Controls.Add(lc);
                              break;
                              
                  }
            }
      }
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11757836
not sure how to add a button column with the setup that you have... sorry i couldn't help
-Trevor
0
 
LVL 9

Expert Comment

by:s_sansanwal
ID: 11757859
removing if(Page.IsPostBack == false) condition from Page_Load function makes the event to fire properly
0
 
LVL 2

Expert Comment

by:dlarlick
ID: 11759181

if you are using asp.net as I am assuming from the last comment check out the following.


   "Manage Dynamically Created Controls".

  http://devcenter.infragistics.com/Articles/ArticleTemplate.ASPX?ArticleID=2149

  see the "rember controls" section.

  If you're dynamically added controls are disappearing you do not have them added to the viewstate for the page to rerender the control on the postback.

  Not sure if this is what you're doing but good luck... It looks like you have alot of good feedback.

  -DLARLICK
0
 
LVL 4

Expert Comment

by:rohanpandya
ID: 11762899
when you add the button on the datagrid add following line to it

     test.EditCommand += new DataGridCommandEventHandler(doedit); here you can take any event

 it work's fine best luck


 Rohan
0
 

Author Comment

by:trevoray
ID: 11763000
ok, as a test, i added a button column to my code and it is performing exactly the same way.

dlarick, i don't think that article applies to my situation since the controls i am creating are being loaded on every page load.

0
 
LVL 2

Expert Comment

by:dlarlick
ID: 11763229

Trevoray -

Are you recreating the control or is ASP recreating your control from the viewstate?

If ASP is recreating the control on postback you are doing things correctly and others are giving you appropriate feedback. (you are not doing an .add again)

However,  If your code runs again and you are re-adding the control yourself,  the article will address your issue in depth.


 
0
 
LVL 2

Expert Comment

by:dlarlick
ID: 11763305
Ignore my last comment
0
 

Author Comment

by:trevoray
ID: 11765218
ok, this is the Itemplate class that allows dynamic datagrid template columns to be created. whenever i run the adding the template code in my main page class, it runs the functions from this code. this was from MSDN site. it all works fine my data is filled and everything. but i am not sure it is correctly assigning the command property for my delete button. anyone familiar with ITemplate that can help me figure out this solution? I do not want to add a button template column. Thank you.

public class DataGridTemplate : ITemplate
      {
            ListItemType templateType;
            string columnName;
            int textboxsize;
            string columntype;
            string textboxname;
            
            public DataGridTemplate(ListItemType type, string colname, int txtsize, string coltype, string txtname)
            {
                  templateType = type;
                  columnName = colname;
                  textboxsize = txtsize;
                  columntype = coltype;
                  textboxname = txtname;
                  

            }

            public void OnDataBinding(object sender, EventArgs e)
            {
                  
                  Type senderType = sender.GetType();
                  if (senderType == typeof(Literal))
                  {
                        /* do this */
                        Literal lc = (Literal) sender;
                        DataGridItem container = (DataGridItem) lc.NamingContainer;
                        lc.Text = ((DataRowView)container.DataItem)[columnName].ToString();
                  
                  }
                  else if (senderType == typeof(TextBox))
                  {
                        /* do this */
                        TextBox txt = (TextBox) sender;
                        DataGridItem container = (DataGridItem) txt.NamingContainer;
                        txt.Text = ((DataRowView)container.DataItem)[columnName].ToString();
                  }
                  
            }
            
            public void InstantiateIn(System.Web.UI.Control container)
            {
                  Literal lc = new Literal();
                  TextBox txt = new TextBox();
                  Button btn = new Button();    
                  switch(templateType)
                  {
                        case ListItemType.Header:
                              lc.Text = "<B>" + columnName + "</B>";
                              container.Controls.Add(lc);
                              break;
                        case ListItemType.Item:
                        switch(columntype)
                        {
                              case "text":
                                    txt.Columns = textboxsize;
                                    txt.DataBinding += new EventHandler(this.OnDataBinding);
                                    txt.ID = textboxname;
                                    container.Controls.Add(txt);
                                    break;
                              case "deletebutton":
                                    txt.ID = "mykey";
                                    txt.DataBinding += new EventHandler(this.OnDataBinding);
                                    txt.Visible = false;
                                    btn.Text = "Delete";
                                    btn.ID = "DeleteButton";
                                    btn.CommandName = "DeleteMe";
                                    container.Controls.Add(txt);
                                    container.Controls.Add(btn);
                                    break;
                        }
                              break;
                        case ListItemType.Footer:
                              lc.Text = "<I>" + columnName + "</I>";
                              container.Controls.Add(lc);
                              break;
                              
                  }
            }
      }
0
 

Author Comment

by:trevoray
ID: 11765288
also, thought i should show my HTML to prove that i have my OnItemCommand property set...

<asp:datagrid id=DataGrid1 Width="700px" runat="server" BorderStyle="None" BackColor="White" CellPadding="5" GridLines="Horizontal" AutoGenerateColumns="False" Font-Size="Smaller" Font-Names="Arial" BorderWidth="0px" ShowFooter="True" onItemCommand="DataGrid1_ItemCommand" onItemDataBound="DataGrid1_ItemDataBound" DataKeyField="pay_adjust_id" AllowSorting="True">
0
 

Author Comment

by:trevoray
ID: 11767484
i don't think there is a way around this except to hard code my delete column. closing out and giving points to trevor
0
 
LVL 2

Expert Comment

by:dlarlick
ID: 11767806

Trevorary -
 
  can you provide your email address if you are still interested and I will communicate with you outside of this forum.

  I was playing around with nested datagrids and had a similiar problem.  

  Are you binding to your datagrid after the initialization method?

   
 
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question