using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI;
namespace SomeName.Classes
{
public class GridViewTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
private string columnNameBinding;
private string controlType;
private int ctrllength;
public GridViewTemplate(DataControlRowType type, string colname, string colNameBinding, string ctlType,int length)
{
templateType = type;
columnName = colname;
columnNameBinding = colNameBinding;
controlType = ctlType;
ctrllength = length;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = columnName;
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
if (controlType == "Label")
{
Label lb = new Label();
lb.ID = "lbl_" + columnNameBinding;
lb.DataBinding += new EventHandler(this.ctl_OnDataBinding);
container.Controls.Add(lb);
}
else if (controlType == "TextBox")
{
TextBox tb = new TextBox();
tb.ID = "tb" + columnNameBinding;
tb.DataBinding += new EventHandler(this.ctl_OnDataBinding);
tb.Width = ctrllength;
container.Controls.Add(tb);
}
else if (controlType == "CheckBox")
{
CheckBox cb = new CheckBox();
cb.ID = "cb1" + columnNameBinding;
cb.DataBinding += new EventHandler(this.ctl_OnDataBinding);
container.Controls.Add(cb);
}
else if (controlType == "HyperLink")
{
HyperLink hl = new HyperLink();
hl.ID = "hl1";
hl.DataBinding += new EventHandler(this.ctl_OnDataBinding);
container.Controls.Add(hl);
}
else if (controlType == "HiddenField")
{
HiddenField hf = new HiddenField();
hf.ID = "hf" + columnNameBinding;
hf.DataBinding += new EventHandler(this.ctl_OnDataBinding);
container.Controls.Add(hf);
}
break;
default:
break;
}
}
public void ctl_OnDataBinding(object sender, EventArgs e)
{
if (sender.GetType().Name == "Label")
{
Label lb = (Label)sender;
GridViewRow container = (GridViewRow)lb.NamingContainer;
lb.Text = ((DataRowView)container.DataItem)[columnNameBinding].ToString();
}
else if (sender.GetType().Name == "TextBox")
{
TextBox tb = (TextBox)sender;
GridViewRow container = (GridViewRow)tb.NamingContainer;
tb.Text = ((DataRowView)container.DataItem)[columnNameBinding].ToString();
}
else if (sender.GetType().Name == "CheckBox")
{
CheckBox cb = (CheckBox)sender;
GridViewRow container = (GridViewRow)cb.NamingContainer;
cb.Checked = Convert.ToBoolean(((DataRowView)container.DataItem)[columnNameBinding].ToString());
}
else if (sender.GetType().Name == "HyperLink")
{
HyperLink hl = (HyperLink)sender;
GridViewRow container = (GridViewRow)hl.NamingContainer;
hl.Text = ((DataRowView)container.DataItem)[columnNameBinding].ToString();
hl.NavigateUrl = ((DataRowView)container.DataItem)[columnNameBinding].ToString();
}
else if (sender.GetType().Name == "HiddenField")
{
HiddenField hf = (HiddenField)sender;
GridViewRow container = (GridViewRow)hf.NamingContainer;
hf.Value = ((DataRowView)container.DataItem)[columnNameBinding].ToString();
}
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////Place the above class seperately and
//////////////Use the Following code in aspx.cs,
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
string strColName="Some DaField Column Name";
string strColLabel = "Header text";
TemplateField tField = new TemplateField();
tField.HeaderText = strColLabel;
tField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, strColLabel, strColName, "", 0);
if (Some Condition)
tField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "", strColName, "Label", 100);
else
tField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "",strColName, "TextBox", 100);
grdMapValues.Columns.Add(tField);
}
if (dtMapData.Rows.Count > 0)
{
grdMapValues.DataSource = dtMapData;
grdMapValues.DataBind();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////In Code Behind, the Gridview is like this
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<asp:GridView ID="grdname" runat="server"
CssClass="GridStyle" ShowHeader="true" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField><ItemTemplate></ItemTemplate></asp:TemplateField>
</Columns>
</asp:GridView>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////In Code Behind, the Gridview is like this
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<asp:GridView ID="grdname" runat="server"
CssClass="GridStyle" ShowHeader="true" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField><ItemTemplate></ItemTemplate></asp:TemplateField>
</Columns>
</asp:GridView>
1 comment:
I have seen many examples to dynamically adding templated field to grid view, BUT havent seen any how to get value after postback from these dynamically added controls
Post a Comment