How to access a DetailsView's ItemTemplate's TextBox in code behind? ASP.Net C#

Bro picture Bro · Jun 11, 2012 · Viewed 7.3k times · Source

I have a DetailsView with 1 of its field converted to TemplateField to be able to manipulate the InsertItemTemplate that contains a TextBox (cf: code below). The problem is that I cannot access to that TextBox Properties in my code behind... and I really don't get it :( Here is my aspx code (portion of it):

<asp:DetailsView ID="_DetailsView" ClientIDMode="Static" runat="server" Height="50px"
        Width="125px" AllowPaging="True" AutoGenerateRows="False" DataKeyNames="IDUniv"
        DataSourceID="EntityDS" OnModeChanging="_OnModeChanging">
        <Fields>
            <asp:TemplateField HeaderText="DateUpdateUniv" SortExpression="DateUpdateUniv" ConvertEmptyStringToNull="False">
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBoxInsertItem" runat="server" Text='<%# Bind("DateUpdateUniv") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
        </Fields>
    </asp:DetailsView>
    <asp:EntityDataSource ID="EntityDS">

and in the Page_LoadComplete event Handler I have something like this:

private void Page_LoadComplete(object sender, EventArgs e)
    {
        if (_DetailsView.HasControls())
        {
            Control _InsertDate = _DetailsView.FindControl("TextBoxInsertItem") as TextBox;
            if (_InsertDate != null)
            {
                _InsertDate.Text  = "something";
            }
        }
    }

but the following code is wrong: _DetailsView.FindControl("TextBoxInsertItem") and also this doesn't work: _InsertDate.Text = "something";

I found an interesting article, but still...: http://www.devproconnections.com/article/aspnet2/getting-under-the-detailsview-control

can someone help me find my path ? How to find this TextBoxInsertItem control and interact with it ? Thanks

Answer

Thousand picture Thousand · Jun 11, 2012
 TextBox txtB = _DetailsView.FindControl("TextBoxInsertItem") as TextBox;
 string text = txtB.Text;

try it like that? Apart from declaring a TextBox instead of a Control i cant see any differences.. i've used this particular way of getting data from child controls alot, and it's always worked for me.