Please find simple resolution below. Note you'll have to write the same code
on every page anyway, so wouldn't just create a user control and expose some
properties to make it generic? (if you don't know how to approach that let me
know)
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
public partial class _Default : BasePage
{
protected void Page_Init(object sender, EventArgs e)
{
CreateGrid();
}
public DataTable GetData()
{
DataTable t = new DataTable();
t.Columns.Add("Id", typeof(int));
t.Columns.Add("Name", typeof(string));
t.Columns.Add("Invoice", typeof(string));
t.Columns.Add("MarkID", typeof(int));
for (int i = 0; i < 10; i++)
{
string str = i.ToString();
DataRow r = t.NewRow();
r[0] = i;
r[1] = "name" + str;
r[2] = "inv" + str;
r[3] = i;
t.Rows.Add(r);
}
return t;
}
private void CreateGrid()
{
DataTable dtt = GetData();
foreach (DataColumn col in dtt.Columns)
{
if (col.ColumnName.Equals("FakAID") || col.ColumnName.Equals("markID") ||
col.ColumnName.Equals("FilID"))
continue;
if (col.ColumnName.Equals("Invoice"))
{
TemplateField tempColumn = new TemplateField();
tempColumn.HeaderText = col.ColumnName;
LinkButtonTemplate template = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&tab=I&cid=", "markID");
template.LinkButtonClick += new EventHandler(template_LinkButtonClick);
tempColumn.ItemTemplate = template;
grvSearchResult.Columns.Add(tempColumn);
}
else if (col.ColumnName.Equals("File"))
{
TemplateField tempColumn = new TemplateField();
tempColumn.HeaderText = col.ColumnName;
tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&fid=", "FilID");
grvSearchResult.Columns.Add(tempColumn);
}
else if (col.ColumnName.Equals("Customer"))
{
TemplateField tempColumn = new TemplateField();
tempColumn.HeaderText = col.ColumnName;
tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screent.aspx?nav=1&cid=", "FakID");
grvSearchResult.Columns.Add(tempColumn);
}
else
{
BoundField boundColumn = new BoundField();
boundColumn.DataField = col.ColumnName;
boundColumn.HeaderText = col.ColumnName;
grvSearchResult.Columns.Add(boundColumn);
}
}
if (!IsPostBack)
{
grvSearchResult.DataSource = dtt;
grvSearchResult.DataBind();
}
}
void template_LinkButtonClick(object sender, EventArgs e)
{
//whatever
}
#region LinkButtonTemplate
public class LinkButtonTemplate : ITemplate, INamingContainer
{
string theColumnName = "", thePostBackUrl = "", theColumnCommandArg = "";
public LinkButtonTemplate(string ColumnName, string PostBackUrl, string
ColumnCommandArg)
{
theColumnName = ColumnName;
thePostBackUrl = PostBackUrl;
theColumnCommandArg = ColumnCommandArg;
}
public event EventHandler LinkButtonClick;
public void InstantiateIn(Control container)
{
LinkButton lnk = new LinkButton();
container.Controls.Add(lnk);
lnk.DataBinding += new EventHandler(lnk_DataBinding);
lnk.ID = "idx";
if (LinkButtonClick != null)
lnk.Click += new EventHandler(LinkButtonClick);
}
void lnk_Click(object sender, EventArgs e)
{
// event handler will execute now
}
void lnk_DataBinding(object sender, EventArgs e)
{
LinkButton lnk = (LinkButton)sender;
DataRowView drv = (DataRowView)((GridViewRow)lnk.NamingContainer).DataItem;
lnk.Text = drv[theColumnName].ToString();
//lnk.PostBackUrl = thePostBackUrl + drv[theColumnCommandArg].ToString();
}
}
#endregion
}
--
Milosz
[quoted text, click to view] "Class" wrote:
> Hi Milosz,
>
> great thanks for the help I will try it.
> One more thing the click event will be in the Template class.
> How do I get it to be in the Page?
> I want to use the LinkButtonTemplate class in more pages and grids.
>
> Thank you.
>
> "Milosz Skalecki [MCAD]" <mily242@DONTLIKESPAMwp.pl> schreef in bericht
> news:B2FC8162-D6AC-4703-A102-5E35C05F43BA@microsoft.com...
> > Please find fixed code below. Two things, 1. you have to create grid on
> > init
> > event handler, 2. bind data only once (IsPostBack == false)
> >
> > protected void Page_Init(object sender, EventArgs e)
> > {
> > CreateGrid();
> > }
> >
> > public DataTable GetData()
> > {
> > DataTable t = new DataTable();
> >
> > t.Columns.Add("Id", typeof(int));
> > t.Columns.Add("Name", typeof(string));
> > t.Columns.Add("Invoice", typeof(string));
> > t.Columns.Add("MarkID", typeof(int));
> >
> > for (int i = 0; i < 10; i++)
> > {
> > string str = i.ToString();
> > DataRow r = t.NewRow();
> > r[0] = i;
> > r[1] = "name" + str;
> > r[2] = "inv" + str;
> > r[3] = i;
> > t.Rows.Add(r);
> > }
> >
> > return t;
> > }
> >
> > private void CreateGrid()
> > {
> > DataTable dtt = GetData();
> >
> > foreach (DataColumn col in dtt.Columns)
> > {
> >
> > if (col.ColumnName.Equals("FakAID") || col.ColumnName.Equals("markID") ||
> > col.ColumnName.Equals("FilID"))
> >
> > continue;
> >
> > if (col.ColumnName.Equals("Invoice"))
> > {
> >
> > TemplateField tempColumn = new TemplateField();
> >
> > tempColumn.HeaderText = col.ColumnName;
> >
> > tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
> > "~/screen.aspx?nav=1&tab=I&cid=", "markID");
> >
> > grvSearchResult.Columns.Add(tempColumn);
> >
> > }
> >
> > else if (col.ColumnName.Equals("File"))
> > {
> >
> > TemplateField tempColumn = new TemplateField();
> >
> > tempColumn.HeaderText = col.ColumnName;
> >
> > tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
> > "~/screen.aspx?nav=1&fid=", "FilID");
> >
> > grvSearchResult.Columns.Add(tempColumn);
> >
> > }
> >
> > else if (col.ColumnName.Equals("Customer"))
> > {
> >
> > TemplateField tempColumn = new TemplateField();
> >
> > tempColumn.HeaderText = col.ColumnName;
> >
> > tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
> > "~/screent.aspx?nav=1&cid=", "FakID");
> >
> > grvSearchResult.Columns.Add(tempColumn);
> >
> > }
> >
> > else
> > {
> >
> > BoundField boundColumn = new BoundField();
> >
> > boundColumn.DataField = col.ColumnName;
> >
> > boundColumn.HeaderText = col.ColumnName;
> >
> > grvSearchResult.Columns.Add(boundColumn);
> >
> > }
> >
> > }
> >
> > if (!IsPostBack)
> > {
> > grvSearchResult.DataSource = dtt;
> > grvSearchResult.DataBind();
> > }
> > }
> >
> >