Here is one thing I found interesting to share, specially for those who
doesn't know how to create a DataGrid dynamically and make it with a
dynamic TemplateColumn whith his own controls, and bind data to them.
For example, I was trying to create a DataGrid, and I wanted to place
an html button into his first column, my goal was to show a few details
about some product, having his serial number, in this case, the value
of the html button, i mean, when this one rise the onclick, some
javascript function is call with this serial number as a parameter and
shows the respective details of the product.
I'm using Ajax, and one simple but newbie solution is send the
datasource to the javascript callback function and draw the entire
table, there you can put your html button without problem, but, there
is a better and less dirty solution, at least, that's my opinion.
Here it is:
Client side code:
JavaScript functions:
function Products_callback(res)
{
document.getElementById("products").innerHTML = res.value;
}
function Products()
{
Methods.Products(Products_callback);
}
Server side code:
public class Methods
{
...
[AjaxMethod]
public string Products()
{
DataGrid dg = new DataGrid();
TemplateColumn tc = new TemplateColumn();
tc.HeaderText = "Serial Number";
tc.ItemTemplate = new TemplateColumnCode();//This class
implementation is the key of the solution.
dg.Columns.Add(tc);
//Add other columns to the DataGrid
//Set DataGrid properties
dg.DataSource = /*your data source*/;
dg.DataBind();
StringBuilder sb = new StringBuilder();
StringWriter stWriter = new StringWriter(sb);
HtmlTextWriter htmlWriter = new HtmlTextWriter(stWriter);
dg.RenderControl(htmlWriter);
return sb.ToString();
}
...
}
public class TemplateColumnCode : ITemplate
{
public void InstantiateIn(Control container)
{
Literal l = new Literal();
l.DataBinding += new EventHandler(this.BindData);
container.Controls.Add(l);
}
// Create a public method that will handle the
// DataBinding event called in the InstantiateIn method.
public void BindData(object sender, EventArgs e)
{
Literal l = (Literal)sender;
DataGridItem container = (DataGridItem)l.NamingContainer;
string code = ((DataRowView)container.DataItem)[0].ToString();
l.Text = "<input id=\"" + code + "\" type=\"button\" value=\""
+ code + "\" onclick=\"Details(this.value);\" />";
}
}
I hope this could help you somehow. As you can see, It can be use with
or without using Ajax, but, I might say that if you are using Ajax and
you want to have something like this, is quite probable you should use
this.
If anybody have other way to solve it, please, post it.
Thanks everybody.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ajax.NET Professional" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/ajaxpro
The latest downloads of Ajax.NET Professional can be found at
http://www.ajaxpro.info/
Don't forget to read my blog at http://weblogs.asp.net/mschwarz/
The open source project is now located at
http://www.codeplex.com/Wiki/View.aspx?ProjectName=AjaxPro
-~----------~----~----~----~------~----~------~--~---