Click here to Skip to main content
16,014,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I've been trying for so long to try and find out why I can't access Textboxes mainly but also labels or any other controls that are placed inside a GridView (or FormView).

I have a need to put a Textbox inside a Custom GridView template that will allow users to enter a date and then click on Submit to post this date to the database.

I like to try and handle all Insert commands in the codebehind file, but Intellisense won't recognise the names of the Textboxes.

Please could somebody simply explain how I reference these textboxes in the GridView.

Thanks in advance.

Pete

--------------------------------------------------------

Thanks guys for the answers so far, most helpful but I'm still being thick and can't get it working:

Right here's a little more. I've included the code behind below and what I want to happen is this:

When the user clicks on the Select link in the GridView "ActionsGridView" (the standard Allow Selection Link for GridViews) it will take the value of TextBox "ActionDateCompText" and then update this in the database.

I am using a SqlDataConnection called "ActionsAddActionConn" and the Code Behind update parameters are as follows:

C#
protected void ActionsGridView_SelectedIndexChanged(object sender, EventArgs e)
{
    ActionsAddActionConn.UpdateParameters["ACTION_STATUS_ID"].DefaultValue = "12";
    ActionsAddActionConn.UpdateParameters["ACTION_COMPLETED_DATE"].DefaultValue = DateTime.Now.ToShortDateString();
    ActionsAddActionConn.Update();
}


I want the entry of "ACTION_COMPLETED_DATE" to be the Value of the TextBox "ActionDateCompText" in the GridView.

Hope this helps you all to get me on the right track (or am I doing it all wrong)?
Posted
Updated 17-Nov-10 3:42am
v2

Try this

protected void ActionsGridView_SelectedIndexChanged(object sender, EventArgs e))
{
 ActionsAddActionConn.UpdateParameters["ACTION_STATUS_ID"].DefaultValue = "12";
    ActionsAddActionConn.UpdateParameters["ACTION_COMPLETED_DATE"].DefaultValue = DateTime.Now.ToShortDateString();

   foreach (GridViewRow row in GridView1.Rows)
   {
      
      Control ctrl = row.FindControl("txtdate") as Textbox;
        if (ctrl != null)
        {
            Textbox txtdate=(Textbox)ctrl;
ActionsAddActionConn.UpdateParameters["ActionDateCompText"].DefaultValue=   txtdate.Text;

           
      
        }
   }
}
 
Share this answer
 
Comments
codemagpie 18-Nov-10 4:43am    
Thanks SChristmas, worked like a charm, excellent thank you so much
codemagpie 18-Nov-10 5:08am    
Hi
Just found a problem with this code. If there's more than 1 row it doesn't populate the date correctly, it just enters a blank date (I assume this is because of the foreach statement).

When ther is just 1 row, then the code works fine.

Would you be able to offer me an improved method?

Thanks

Pete
You can't access the controls directly because they're in a template. In the case of the GridView, on your GridView_RowUpdating event, you can do something like this to find controls in that row:

C#
GridViewRow row = myGridView.Rows[e.RowIndex];

TextBox txt = row.FindControl("ID_OF_TEXTBOX") as TextBox;


After you've found your control, it's business as usual.
 
Share this answer
 
Hi,

There is no way you can directly access textbox placed under gridview.But there is workaround, you can try this
C#
foreach (GridViewRow item in grvCustomers.Rows)
       {
            TextBox txtFirstName = item.FindControl("txtFirstName") as TextBox;
       }


Considering that your gridview id is grvCustomers and your desired textbox id is txtFirstName

You can write this inside Submit button click event where you need to retrieve all user entered data and send all them to database insert/update .

Hope this will help.
 
Share this answer
 
Hi,

look at this solution : jQuery'ish Selector for WebControls - recursive FindControl[^]

I'm using this, but I extended this class with a few extension methods so it is much more productive. :-D
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900