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

JaM
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"
>
<Columns>
</Columns>
</asp:GridView>

<ASP:BUTTON ID="buttSave" Runat="Server" CssClass="text" Text="Save"
onclick="buttSave_Click">
</ASP:BUTTON>
__________________________________________

this is the codebehind:
__________________________________________

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

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";
dtab.Columns.Add(dcol);
bf = new BoundField();
bf.DataField = "Id";
bf.HeaderText = "Id";
GridView2.Columns.Add(bf);

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

//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);
container.Controls.Add(tb1);
}

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

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
textbox.

I've tryed with

((TextBox)row.FindControl("tbox")).Text

and also with

((TextBox)GridView2.Rows[i].Cells[j].FindControl("tbox")).Text

but I always get a Null value...

Anyone can help me?
Thanks

Oct 3 '06 #1
2 5680
JaM
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.