You must Sign In to post a response.
  • Category: ASP.NET

    Unable to read dynamically created texbox in gridview

    Hello

    I added columns dynamically to gridview at run time and have added textboxs to every column using row data bound.
    but now when i try to get this value in another button click i get null reference error.
    I know that as pageload do not retains values ,i am not getting the required result.but what is solution on it?
    here is my code..
    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    for (int i = 0; i < 6; i++)
    {
    TextBox txt = new TextBox();
    txt.CssClass = "SmallTextBoxDate";
    txt.ID = "TextBox" + (i).ToString();
    if (dt != null && dt.Rows.Count > 0)
    {
    {
    txt.Text = dt.Rows[e.Row.RowIndex][i - 1].ToString();
    }
    }
    e.Row.Cells[i].Text = "";
    e.Row.Cells[i].Controls.Add(txt);
    }
    }
    }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {

    string A=(((TextBox)gvrItem.FindControl("txt")).Text).tostring();//Here I m getting null value
    }
    please help
  • #762642
    The main problem is the control "txtE" is not finding in gridview, because of that you are getting object reference error. To resolve that check whether control is added into gridview or not. If control is available inside gridview then obviously it should be pointed out.
    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #762647
    sorry that was typing mistake in post... It is corrected in code but still I am not getting value for textbox

  • #762648
    Hi

    You are not getting the value of txt in the add button click.
    Try using the below line by changing Cells[0],Cells[1] or Cells[4] based on the position of textbox.

    TextBox txtname = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0];
    string A = txtname.Text;

    Sridhar Thota.
    Editor: DNS Forum.

  • #762652
    Hello Shridhar
    Thank you for your reply
    But actually my problem is that I can get the value from textbox before the page is postback.
    but I get it null when the postback occurs.
    So is there any way where I can recreate this controls on button click...will be great helpfull

  • #762653
    Hi

    Make use of session state on viewstate depending up on your requirement.
    store value in viewstate variable.
    TextBox txtname = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0];
    ViewState["txt"] = txtname.Text;

    string s=ViewState["txt"].ToString();
    You can use session variable also. Better to use Inproc session.

    Sridhar Thota.
    Editor: DNS Forum.

  • #762689
    Hai Piyusha,
    Creation of the controls should be done under the RowCreated event and not under the RowDataBound event.
    You can use the RowCreated event and then generate the controls inside the GridView.This event will be called initially and will generate the controls.
    Once the controls are generated, you can fill data data using the RowDataBound event and then you will be able to retrieve the controls with the data filled with it.
    Let us know if you still get the error.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #762702
    i tried creating control in RowCreated event but controls are not created

  • #762713
    Pls help i have not yet got solution..
    @Shridhar Thota-you said that keep value in viewstate.
    but I don't want to retain the value during page load.
    I have textbox in grid and I want value entered in that textbox after pageload


Sign In to post your comments