GridView: cannot get data from dynamically created Textbox

Discussion in 'ASP .Net' started by JaM, Oct 3, 2006.

  1. JaM

    JaM Guest

    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.Cells[j].FindControl("tbox")).Text

    but I always get a Null value...



    Anyone can help me?
    Thanks
     
    JaM, Oct 3, 2006
    #1
    1. Advertising

  2. JaM

    JaM Guest

    Anyone can help me??? :(
     
    JaM, Oct 4, 2006
    #2
    1. Advertising

  3. JaM

    JaM Guest

    JaM, Oct 4, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack Johnston
    Replies:
    5
    Views:
    583
    Natty Gur
    Jul 28, 2003
  2. Shilpi Chaudhry
    Replies:
    1
    Views:
    3,074
    Hermit Dave
    Sep 15, 2004
  3. Raed Sawalha
    Replies:
    2
    Views:
    1,548
    =?Utf-8?B?UHN5Y2hv?=
    Oct 5, 2004
  4. msimmons
    Replies:
    0
    Views:
    524
    msimmons
    Jul 16, 2009
  5. Tatyana
    Replies:
    0
    Views:
    211
    Tatyana
    Jun 26, 2006
Loading...

Share This Page