Click here to Skip to main content
15,905,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
 Hi,

 we have tried to build gridview with dynamic columns based the data source using the template fields in asp.net through code behind because the no.of columns to display may vary each month.

For that, to implement we have developed one class "DynamicTemplate" which implements the "ITemplate" interface. In that template fields i have inserted the "LinkButton" in each cell and when i click that cell link button i need to show the one Popup with selected cell value.


For that i have created one Default.asxp page and wrote the following.

For Detailed Sample Please download from this link [^]

C#
public partial class Default : System.Web.UI.Page
   {
       DataTable dt;

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

       }

       private void GenateGridView()
       {
           TemplateField tempField;
           DynamicTemplate dynTempItem;
           LinkButton lnkButton;
           Label label;

           GridView gvDynamicArticle = new GridView();

           gvDynamicArticle.Width = Unit.Pixel(500);
           gvDynamicArticle.BorderWidth = Unit.Pixel(0);
           gvDynamicArticle.Caption = "<div>Default Grid</div>";
           gvDynamicArticle.AutoGenerateColumns = false;

           DataTable data = getBindingData();

           for (int i = 0; i < data.Columns.Count; i++)
           {
               tempField = new TemplateField();
               dynTempItem = new DynamicTemplate(ListItemType.AlternatingItem);

               lnkButton = new LinkButton();

               lnkButton.ID = string.Format("lnkButton{0}", i);
               lnkButton.Visible = true;

               string ColumnValue = data.Columns[i].ColumnName;
               tempField.HeaderText = ColumnValue;

               if (ColumnValue == "EmpName")
               {
                   label = new Label();

                   label.ID = string.Format("Label{0}", i);
                   dynTempItem.AddControl(label, "Text", ColumnValue);
                   label.Width = 100;
               }
               else
               {
                   dynTempItem.AddControl(lnkButton, "Text", ColumnValue);
                   lnkButton.Click += lnkButton_Click;
               }
               tempField.ItemTemplate = dynTempItem;
               gvDynamicArticle.Columns.Add(tempField);
               //////grdUserPivotDateTwo.Columns.Add(tempField);
           }

           gvDynamicArticle.DataSource = data;
           gvDynamicArticle.DataBind();

           divContainer.Controls.Add(gvDynamicArticle);

       }

       void lnkButton_Click(object sender, EventArgs e)
       {
           // showing cell values in popUp here..
           ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('cell clicked')");
       }


       private DataTable getBindingData()
       {
           dt = new DataTable();
           dt.Columns.Add(new DataColumn("EmpName"));
           dt.Columns.Add(new DataColumn("Monday"));
           dt.Columns.Add(new DataColumn("TuesDay"));
           dt.Columns.Add(new DataColumn("WednesDay"));
           dt.Columns.Add(new DataColumn("ThursDay"));

           dt.Rows.Add("EmpOne", "p", "p", "p", "a");
           dt.Rows.Add("EmpTwo", "p", "a", "p", "p");
           dt.Rows.Add("EmpThree", "p", "p", "p", "a");
           dt.Rows.Add("EmpFour", "p", "a", "p", "p");
           dt.Rows.Add("EmpFive", "p", "p", "p", "a");
           dt.Rows.Add("EmpSix", "a", "p", "p", "p");

           return dt;

       }



   }


and corresponding DynamicTemplate class is

C#
public class DynamicTemplate : System.Web.UI.ITemplate
{
    System.Web.UI.WebControls.ListItemType templateType;
    System.Collections.Hashtable htControls = new System.Collections.Hashtable();
    System.Collections.Hashtable htBindPropertiesNames = new System.Collections.Hashtable();
    System.Collections.Hashtable htBindExpression = new System.Collections.Hashtable();

    public DynamicTemplate(System.Web.UI.WebControls.ListItemType type)
    {
        templateType = type;
    }

    public void AddControl(WebControl wbControl, String BindPropertyName, String BindExpression)
    {
        htControls.Add(htControls.Count, wbControl);
        htBindPropertiesNames.Add(htBindPropertiesNames.Count, BindPropertyName);
        htBindExpression.Add(htBindExpression.Count, BindExpression);

    }

    public void InstantiateIn(System.Web.UI.Control container)
    {
        PlaceHolder ph = new PlaceHolder();

        for (int i = 0; i < htControls.Count; i++)
        {

            //clone control 
            Control cntrl = CloneControl((Control)htControls[i]);

            switch (templateType)
            {
                case ListItemType.Header:
                    break;
                case ListItemType.Item:
                    ph.Controls.Add(cntrl);
                    break;
                case ListItemType.AlternatingItem:
                    ph.Controls.Add(cntrl);
                    ph.DataBinding += new EventHandler(Item_DataBinding);
                    break;
                case ListItemType.Footer:
                    break;
            }
        }
        ph.DataBinding += new EventHandler(Item_DataBinding);

        container.Controls.Add(ph);

    }
    public void Item_DataBinding(object sender, System.EventArgs e)
    {
        PlaceHolder ph = (PlaceHolder)sender;
        GridViewRow ri = (GridViewRow)ph.NamingContainer;
        for (int i = 0; i < htControls.Count; i++)
        {
            if (htBindPropertiesNames[i].ToString().Length > 0)
            {
                Control tmpCtrl = (Control)htControls[i];
                String item1Value = (String)DataBinder.Eval(ri.DataItem, htBindExpression[i].ToString());
                Control ctrl = ph.FindControl(tmpCtrl.ID);

                Type t = ctrl.GetType();
                System.Reflection.PropertyInfo pi = t.GetProperty(htBindPropertiesNames[i].ToString());

                pi.SetValue(ctrl, item1Value.ToString(), null);
            }

        }



    }

    private Control CloneControl(System.Web.UI.Control src_ctl)
    {
        Type t = src_ctl.GetType();
        Object obj = Activator.CreateInstance(t);
        Control dst_ctl = (Control)obj;
        PropertyDescriptorCollection src_pdc = TypeDescriptor.GetProperties(src_ctl);
        PropertyDescriptorCollection dst_pdc = TypeDescriptor.GetProperties(dst_ctl);

        for (int i = 0; i < src_pdc.Count; i++)
        {

            if (src_pdc[i].Attributes.Contains(DesignerSerializationVisibilityAttribute.Content))
            {

                object collection_val = src_pdc[i].GetValue(src_ctl);
                if ((collection_val is IList) == true)
                {
                    foreach (object child in (IList)collection_val)
                    {
                        Control new_child = CloneControl(child as Control);
                        object dst_collection_val = dst_pdc[i].GetValue(dst_ctl);
                        ((IList)dst_collection_val).Add(new_child);
                    }
                }
            }
            else
            {
                dst_pdc[src_pdc[i].Name].SetValue(dst_ctl, src_pdc[i].GetValue(src_ctl));
            }
        }

        return dst_ctl;

    }
}



 Here the Data showing in gridview is fine

 Here the Issues are 
when i click on the linkButton the page reloads and no grid is displaying after the postback.

and for LinkButton the "Click Event" is not firing.

Please provide me the help full information/Sample to show the modal window when we click on the linkButton of the gridview. 
Posted
Updated 16-Dec-13 18:07pm
v5
Comments
Can you show the GridView Markup?
V G S Naidu A 17-Dec-13 1:56am    
I have added the gridview in Code directly instead of markup. Please download the sample and do the next

1 solution

open the modal popup on clicking the edit button in grid view from code behind
C#
protected void lnkButton_Click((object sender, EventArgs e)
{
    hfUser.Value = "0";
    GridViewRow gvRow = (GridViewRow)((LinkButton)sender).NamingContainer;
    int userId = Convert.ToInt32(gvUser.DataKeys[gvRow.RowIndex].Value);
     //get code from database
     // fill all the controls and open modal popup by
// showing cell values in popUp here.. 
//ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('cell clicked')");
    mpeUserDetail.Show();
}


Get more info
ModalPopupExtender from Server Side Code[^]
ajax-modalpopupextender-show-hide-from-code-behind-and-by-javascript[^]
 
Share this answer
 
Comments
V G S Naidu A 17-Dec-13 1:55am    
Hi actully,that's what i am trying to do, but the click event is not fired, please download the sample and observe the action if you can spare some time. thank you
[no name] 17-Dec-13 2:00am    
In our company downloaded is blocked. Hope so do you have any client validation please check. If yes then make CausesValidation="false"
Or Provide your html code
V G S Naidu A 17-Dec-13 2:11am    
here there is no html code, with the above two classes are enough to run the sample.. just you need to add one new "Default.aspx" page and past the first code in code behind.
thank you.
[no name] 17-Dec-13 2:24am    
Just modify click event handler
lnkButton.Click += new EventHandler(lnkButton_Click);
[no name] 17-Dec-13 2:26am    
And coment your post back because every aspx event needs to refresh in each postback time
//if (!IsPostBack) //Comment here in pageload event
GenateGridView();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900