470,566 Members | 1,400 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,566 developers. It's quick & easy.

GridView: cannot get data from dynamically created Textbox

Hi all,

I have created a gridview vith dynamic textbox columns (they are in
variable number, it depends on what things I select from database)

aspx code:

<asp:GridView AutoGenerateColumns="false" ID="GridView2" Runat="server"

<ASP:BUTTON ID="buttSave" Runat="Server" CssClass="text" Text="Save"

this is the codebehind:

protected void Page_Load(object sender, System.EventArgs e)
if (!IsPostBack) //prima volta che entro nella pagina
GridView2.DataSource = MakeEditTable();

private DataTable MakeEditTable()
//load data from db

DataTable dtab = new DataTable();
DataColumn dcol = new DataColumn();
BoundField bf = new BoundField();
TemplateField tf = new TemplateField();

dcol = new DataColumn();
dcol.DataType = typeof(int);
dcol.ColumnName = "Id";
bf = new BoundField();
bf.DataField = "Id";
bf.HeaderText = "Id";

dcol = new DataColumn();
dcol.DataType = typeof(string);
dcol.ColumnName = "Name";
tf = new TemplateField();
tf.ItemTemplate = new GridViewTemplate();
tf.HeaderText = "Name";

//cycle to add more columns with GridViewTemplate

and this is the class GridViewTemplate:
(simplifyed for the 'Name' column only)

public class GridViewTemplate : ITemplate
public GridViewTemplate()
{ }

void ITemplate.InstantiateIn(System.Web.UI.Control container)
TextBox tb1 = new TextBox();
tb1.ID = "tbox_Name";
tb1.DataBinding += new EventHandler(tb1_DataBinding);

void tb1_DataBinding(object sender, EventArgs e)
TextBox txtdata = (TextBox)sender;
GridViewRow container = (GridViewRow)txtdata.NamingContainer;
object dataValue = DataBinder.GetPropertyValue(container.DataItem,

if (dataValue != DBNull.Value)
txtdata.Text = dataValue.ToString();

When the page is loaded the GridView is populated with the correct
data, but when i click the button I cannot get the data from the

I've tryed with


and also with


but I always get a Null value...

Anyone can help me?

Oct 3 '06 #1
2 5680
Anyone can help me??? :(

Oct 4 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Giovanni | last post: by
7 posts views Thread by GaryDean | last post: by
4 posts views Thread by Ken Wigle | last post: by
4 posts views Thread by Tomasz Jastrzebski | last post: by
1 post views Thread by savajx1 | last post: by
4 posts views Thread by Craig Buchanan | last post: by
5 posts views Thread by sandhyascs | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.