• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 451
  • Last Modified:

setting the content of a textbox in a GridView template field programatically

Excuse a newby's etiquette, I just posted this question as a comment in a previously answered question and am afraid that no one will see it and respond.

I have a DataGrid in which I set some of the columns as bound fields from a datasource.  There are several columns, not bound, that I want to either allow the user to enter, or to get the data from other sources and fill in arbitrarily.  It is no problem having the user enter the data and retrieving what they have entered.

In the example below, I have omitted all the fields except the one I am trying to manipulate.  What I want to do is update the contents of the cell in the DataGrid, not access the contents.

Here is my DataGrid:
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 
        AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" 
        GridLines="None">
        <RowStyle BackColor="#E3EAEB" />
        <Columns>
         ...
       <asp:TemplateField HeaderText="First Name">
                <ItemTemplate>
                      <asp:TextBox ID="tbxFname" runat="server"></asp:TextBox>
                </ItemTemplate>
       </asp:TemplateField>
       ---
</Columns>
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#7C6F57" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>

Open in new window


In the code behind I want to put text into the tbxFname textbox of the template field.  I cannot find the correct construct to do this.  I can easily get the contents of it, but actually modifying its contents is eluding me.

GridView1.Rows[index].Cells[1].Controls[0].Text = "some string";

Open in new window

 is wrong - .Text is not a valid property.  I have tried many other combinations of attributes with no success.  Any help?
Edit Comment
0
kparent
Asked:
kparent
  • 2
1 Solution
 
Jerry MillerCommented:
You have to create a textbox in the code behind and assign it's value from the one in the gridview that you need to manipulate.

The VB version is something like:

dim txtBox as textbox = GridView1.Rows[index].Cells[1].Controls[0]
0
 
kparentAuthor Commented:
I've been able to do this without problem.  The problem is how to put the value back into the GridView cell.
0
 
sonawanekiranCommented:
Try like this.
Note :  FindControl method should take control id which you are looking for...



TextBox LastName = (TextBox)GridView1.Rows[index].Cells[1].FindControl("tbxFname");
LastName.Text = "Some string";
0
 
kparentAuthor Commented:
Thanks.  It was not intuitive to me but it worked perfectly.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now