Skip to content

Add textbox inside gridview

shibasankar behera

Asked Sep 29, 2013, 9:43 PM EDT8 answers
Share
LinkedInX
Hello,
I have created a gridview and added column dynamically in code behind.
for blank rows I have added textbox with same id.
But I am unable to get those text boxes value and getting error
"

Multiple controls with the same ID 'txt' were found. FindControl requires that controls have unique IDs

" 

8 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    hello Sunny,
    Actually I have created Gridview according to database items and all total is doing in code behind. I dont know how many columns are there. SO I want to assign textbox in gridview according to column numbers.
    0
  • Sunny  Sharma
    0
    I think I'm not getting  your point -"I dont know how many columns are there." as there many ways to get that.  please share your code how  you're doing this.
    0
  • 0
    if (!IsPostBack)
                {
                    BoundField bfield = new BoundField();
                    bfield.HeaderText = "Item ID";
                    bfield.DataField = "Item_ID";
                    GridView1.Columns.Add(bfield);

                    BoundField bfield1 = new BoundField();
                    bfield1.HeaderText = "Item Name";
                    bfield1.DataField = "Item_Name";
                    GridView1.Columns.Add(bfield1);

                    con.Open();
                    SqlDataReader myReader2 = null;
                    string query = "select distinct RFQCond_Desc from RFQRecieveItemCondition where RFQRef_No='mQ/09#f56' ";
                    SqlCommand myCommand2 = new SqlCommand(query, con);
                    myReader2 = myCommand2.ExecuteReader();
                    while (myReader2.Read())
                    {
                        TemplateField tfield1 = new TemplateField();
                        tfield1.HeaderText = myReader2["RFQCond_Desc"].ToString();
                        GridView1.Columns.Add(tfield1);
                    }
                    myReader2.Close();
                    con.Close();

                    TemplateField tfield6 = new TemplateField();
                    tfield6.HeaderText = "Description";
                    GridView1.Columns.Add(tfield6);
                }

    Here I am getting conditions for specific RFQRef_No, and adding those conditions as columns in gridview. Different RFQRef_No contains different number of conditions.
    so after binding I am adding textbox for those conditions.
    0
  • Prashant Srivastava
    0
    Hi Shibasankar,

    You are getting this error because you are assigne the same ID to 2 controls which logically wrong because in this way it will hard to predict for which control you want to work.

    SOLUTION STEPS :

    1. Change the id of the textbox control in .aspx.
    2. Create a  RowDataBound Event.
    3. Find textbox in RowDataBound using FindControl (reason : on run time id of control will be changed and it will hard to know for which row of textbox you want to work).
    4. Once you will get the control (via FindControl) you can do any thing.

    Regards,
    Prashant Srivastava
     
    0
  • 0
    Hello Prasant,
    I am adding columns to grid view dynamically according to need from database. There are no fixed number of columns, so how shall I give id to them. So I am simply adding text boxes to those columns and want to get values. Also I am getting values from those text boxes by this process in one of my demo page. But when I am implementing this concept in my main project it is nit working.
    If you wish I can send you code.
    Thank you for your time and consideration towards me.
    0
  • Prashant Srivastava
    0
    Hi Shibasankar,

    Please send me your code. Let have a look.
    0
  • 0
    foreach (Control cc in gvrC.Cells[x].Controls)
                        {
                            string str = ((TextBox)cc).Text;
                            //here my operations....
                        }
    0