سؤال

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

هل كانت مفيدة؟

المحلول 2

I finally found the solution :) and there was many problems:

1. my code was not place in the right Page_event , I thought that the DetailsView must had the controls properly rendered before trying to "FindControl them" so I put it in the Page_LoadComplete, but it was wrong.
The DetailsView comes with a few events, and the one that interests us is the OnItemCreated (Occurs when a record is created in a System.Web.UI.WebControls.DetailsView control).

2. my test were not relevant, the right test to check simply:

if (_DetailsView.FindControl("TextBoxInsertItem") != null)

3. my Visual Sudio (not SP1) was full of bugs (when I was running the same code, sometimes it was throwing errors, sometimes not, until I closed VS2010 and reopened it...

4. I had to initialize a "TextBox" and not a "Control" as said Jane

I hope these few lines will help :) and as said in the MSDN: You should not create application logic that relies on the change events being raised in a specific order unless you have detailed knowledge of page event processing

نصائح أخرى

 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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top