SHARE
TWEET

Dynamic Gridview

a guest Mar 15th, 2013 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Linq;
  3. using System.Web.UI;
  4. using System.Web.UI.WebControls;
  5.  
  6. namespace DynamicGridView
  7. {
  8.   public partial class DynamicGrid : Page
  9.   {
  10.     protected void Page_Load(object sender, EventArgs e)
  11.     {
  12.       var data = Enumerable.Range(1, 100).Select(x => new { ID = "id" + x, Data = x }).ToList();
  13.  
  14.       var gw = new GridView { DataKeyNames = new string[] { "ID" }, AutoGenerateColumns = false };
  15.  
  16.       var dataField = new BoundField { DataField = "Data", HeaderText = "Data" };
  17.  
  18.       var t = new TemplateField();
  19.  
  20.       var tf = new MyTemplate(x =>
  21.       {
  22.         ScriptManager.RegisterStartupScript(this, this.GetType(), "key", string.Format("alert('you click to item with ID = {0} and Data = {1}')", x.ID, x.Data), true);
  23.       });
  24.  
  25.       t.ItemTemplate = tf;
  26.  
  27.       gw.Columns.Add(dataField);
  28.       gw.Columns.Add(t);
  29.       gw.DataSource = data;
  30.       gw.DataBind();
  31.  
  32.       this.Form.Controls.Add(gw);
  33.     }
  34.  
  35.     public class MyTemplate : ITemplate
  36.     {
  37.       private readonly Action<dynamic> action;
  38.  
  39.       public MyTemplate(Action<dynamic> action)
  40.       {
  41.         this.action = action;
  42.       }
  43.  
  44.       public void InstantiateIn(Control container)
  45.       {
  46.         var bt = new Button();
  47.         bt.Text = "press me please";
  48.  
  49.         bt.Click += bt_Click;
  50.  
  51.         var hfId = new HiddenField();
  52.         hfId.ID = "hfId";
  53.         hfId.DataBinding += hfId_DataBinding;
  54.  
  55.         var hfData = new HiddenField();
  56.         hfData.ID = "hfData";
  57.         hfData.DataBinding += hfData_DataBinding;
  58.  
  59.         container.Controls.Add(bt);
  60.         container.Controls.Add(hfId);
  61.         container.Controls.Add(hfData);
  62.       }
  63.  
  64.       void hfData_DataBinding(object sender, EventArgs e)
  65.       {
  66.         var hf = sender as HiddenField;
  67.         if (hf != null)
  68.         {
  69.           var row = hf.NamingContainer as GridViewRow;
  70.           if (row != null)
  71.           {
  72.             object dataValue = DataBinder.Eval(row.DataItem, "Data");
  73.             hf.Value = dataValue != null ? dataValue.ToString() : string.Empty;
  74.           }
  75.         }
  76.       }
  77.  
  78.       void hfId_DataBinding(object sender, EventArgs e)
  79.       {
  80.         var hf = sender as HiddenField;
  81.         if (hf != null)
  82.         {
  83.           var row = hf.NamingContainer as GridViewRow;
  84.           if (row != null)
  85.           {
  86.             object dataValue = DataBinder.Eval(row.DataItem, "ID");
  87.             hf.Value = dataValue != null ? dataValue.ToString() : string.Empty;
  88.           }
  89.         }
  90.       }
  91.  
  92.       void bt_Click(object sender, EventArgs e)
  93.       {
  94.         var bt = sender as Button;
  95.         if (bt != null)
  96.         {
  97.           var row = bt.BindingContainer as GridViewRow;
  98.           if (row != null)
  99.           {
  100.             var id = ((HiddenField)row.FindControl("hfId")).Value;
  101.             var data = ((HiddenField)row.FindControl("hfData")).Value;
  102.             action(new { ID = id, Data = data });
  103.           }
  104.         }
  105.       }
  106.     }
  107.   }
  108. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top