FindControl is not working in DetailsView

Last post 04-15-2008 12:05 PM by anas. 12 replies.

Sort Posts:

  • FindControl is not working in DetailsView

    04-11-2008, 4:08 PM
    • Loading...
    • xyz789
    • Joined on 01-29-2007, 5:43 PM
    • Posts 105

    This should be a simple question to answer for some, but I cannot figure it out.

    I have a DetailsView called dvEmployees that is set to edit mode.

    I also have a date picker embedded in the detailsview that will allow me to choose a date from the calendar. When I select the date it will simply insert the date into a textbox within dvEmployees called txtCertDate. The field that holds txtCertDate is converted to a template field. This is an example of the code I am using. It is working great, no problems here.

    Protected Sub calCertDate_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles calCertDate.SelectionChanged
              CType(dvEmployees.Rows(0).Cells(1).FindControl("txtCertDate"), TextBox).Text = calCertDate.SelectedDate.ToString("d")
    End Sub

    However, I would like to retrieve the value back out of txtCertDate to run a case statement, but I keep getting this error message

    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    Here is the code:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Select Case "True"

    Case Is = CType(dvEmployees.Rows(0).Cells(1).FindControl("txtCertDate"), TextBox).Text.StartsWith("1/")
            CType(dvEmployees.Rows(0).Cells(1).FindControl("ddlInService"), TextBox).Text = "AUGUST-DECEMBER"
    Case Is = CType(dvEmployees.Rows(0).Cells(1).FindControl("txtCertDate"), TextBox).Text.StartsWith("12/")
           
    CType(dvEmployees.Rows(0).Cells(1).FindControl("ddlInService"), TextBox).Text = "JANUARY-MAY"
    End Select

    End Sub

    I am using the same find control method as before, the only difference is that I am now trying to retreive the value from this textbox.

    Could anyone help me?

     

    Thanks,

    xyz789

  • Re: FindControl is not working in DetailsView

    04-11-2008, 4:31 PM
    • Loading...
    • anas
    • Joined on 09-21-2006, 4:31 AM
    • Jerusalem
    • Posts 4,067

    Hi

    you need to move the code from Page_load to page_PreRender

    because in pageLoad , the DetailsView is not created yet,,,

     


     

    Best Regards,

    Anas Ghanem| LogFile
  • Re: FindControl is not working in DetailsView

    04-11-2008, 4:41 PM
    • Loading...
    • xyz789
    • Joined on 01-29-2007, 5:43 PM
    • Posts 105

    Thanks, I just moved the code to Page_PreRender and I got the same error message:

    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    Line 51:     Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
    Line 52:         Select Case "True"
    Line 53:             Case Is = CType(dvEmployees.Rows(0).Cells(1).FindControl("txtCertDate"), TextBox).Text.StartsWith("1/")
    Line 54:                 CType(dvEmployees.Rows(0).Cells(1).FindControl("ddlInService"), TextBox).Text = "AUGUST-DECEMBER"
    Line 55:             Case Is = CType(dvEmployees.Rows(0).Cells(1).FindControl("txtCertDate"), TextBox).Text.StartsWith("2/")

    Is there something else I can do?

    Thanks,

    xyz789

  • Re: FindControl is not working in DetailsView

    04-11-2008, 4:58 PM
    • Loading...
    • rcimba
    • Joined on 04-01-2008, 7:36 PM
    • Posts 11

     Check your Rows.Count and Cells.Count first. Make sure your indexes are valid.

    Signature
  • Re: FindControl is not working in DetailsView

    04-11-2008, 6:17 PM
    • Loading...
    • xyz789
    • Joined on 01-29-2007, 5:43 PM
    • Posts 105

    Thank you for helping me out. The row and cell count is the exact same code in the date picker, which is working great.

    The data is being stored in the right place when edit the data. 

    Do you know of something else I can do?

     

    Thanks,

    xyz789

     

    P.S.

    This field is set to a template. I forgot to show the code for the ItemTemplate values. Instead of finding the textbox controls, I am now finding the Labels. But I am still getting the same error message.

  • Re: FindControl is not working in DetailsView

    04-14-2008, 1:47 AM

    Hi xyz789 ,

    Please make sure detailsview is in the correct mode.

    say, you can not retrieve the control in EditTemplate but detailsview control is in normal mode.

     

    Sincerely,
    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
  • Re: FindControl is not working in DetailsView

    04-14-2008, 2:57 PM
    • Loading...
    • xyz789
    • Joined on 01-29-2007, 5:43 PM
    • Posts 105

    This is becoming very frustrating now! I do want to say thank you Samu for your help though, but now I am completely lost. I have never had this type of problem before.

     

    I have tried to retrieve the data in Read-Only, and Edit mode. In addition to using this code when retrieving a field in EditItemTemplate.


    Dim
    z As String  z = CType(gvEmployees.Rows(0).Cells(1).FindControl("TextBox1"), TextBox).Text 

    I also tried to use this code when retrieving a field within a ItemTemplate.

     Dim t As String

     t = CType(dvEmployees.Rows(0).Cells(1).FindControl("Label1"), Label).Text  

     

    This also failed to produce results.

     

    I have also tried changing the index within the rows and cells, and still the same error message.

     

    I even started over from the beginning and created a new SqlDataSource with less fields and a GridView instead of a DetailsView.

     

    I still get the same error message! I went back to my database to review the data, and there are no Null values, or spaces, plus I have no problems when I want to edit these fields. I can modify a record using both a DetailsView and a GridView without any issues. However, when I try to store the value into a variable I get this error message.

     

    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
     

    Plus I even tried a different field to make sure there were no problems with the data type.

     

    For example, originally I wanted to grab a field that holds a date. This date will be stored in a String variable called ‘z’ if you look at the code above. So to check that there were no problems with the data type I switch the field to grab another field that stores a name that is basically a char value, and I still get the same error message.

     

     Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
     

     

    What else can it be????

     

  • Re: FindControl is not working in DetailsView

    04-14-2008, 3:08 PM
    • Loading...
    • anas
    • Joined on 09-21-2006, 4:31 AM
    • Jerusalem
    • Posts 4,067

    Hi ,

    I tried it , if the Current displayed template( for example edit template ) , and if there is a control with ID "TextBox1" in that template ,

    then the following code must works well

    Note that you don't have to specify the Row and the cell ... just use FindControl

    Dim z As String  z = CType(gvEmployees.FindControl("TextBox1"), TextBox).Text 

    If you still have problems , let me see the DetailsViewCode , and where you access the textBox

    Regards,

    Best Regards,

    Anas Ghanem| LogFile
  • Re: FindControl is not working in DetailsView

    04-14-2008, 4:20 PM
    • Loading...
    • xyz789
    • Joined on 01-29-2007, 5:43 PM
    • Posts 105

    Hello,

    I am still getting an error message, but it is a different message now, because I took your advice and used the CType(gvEmployees.FindControl("TextBox1"), TextBox).Text instead of using the row and cell count.

    Object reference not set to an instance of an object.

     Here is the code for the DetailsView:

    <asp:DetailsView ID="dvEmployees" runat="server" AutoGenerateRows="False" DataSourceID="srcEmployeeRecord"
    Font-Size="Medium" Height="50px" Width="604px" DataKeyNames="KEY_ID">
    <Fields>
    <asp:BoundField DataField="KEY_ID" HeaderText="KEY_ID" InsertVisible="False" ReadOnly="True" SortExpression="KEY_ID" Visible="False" />
    <asp:BoundField DataField="DEPARTMENT_ID" HeaderText="DEPARTMENT_ID" SortExpression="DEPARTMENT_ID" Visible="False" />
    <asp:BoundField DataField="RANK_ID" HeaderText="RANK_ID" SortExpression="RANK_ID" Visible="False" />
    <asp:TemplateField HeaderText="DEPARTMENT" SortExpression="DEPARTMENT">
    <EditItemTemplate>
    <asp:DropDownList ID="ddlDepart" runat="server" DataSourceID="srcDepartment" DataTextField="DEPARTMENT" DataValueField="DEPARTMENT_ID" SelectedValue='<%# Bind("DEPARTMENT_ID") %>' Width="171px" AutoPostBack="True">
    </asp:DropDownList>
    </EditItemTemplate>
    <InsertItemTemplate>
    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("DEPARTMENT") %>'></asp:TextBox>
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label3" runat="server" Text='<%# Bind("DEPARTMENT") %>'></asp:Label>
    </ItemTemplate>
    <HeaderStyle Width="90px" Font-Bold="True" />
    <ItemStyle Wrap="False" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="RANK" SortExpression="RANK">
    <EditItemTemplate>
    <asp:DropDownList ID="ddlRank" runat="server" DataSourceID="srcRank" DataTextField="RANK" DataValueField="RANK_ID" SelectedValue='<%# Bind("RANK_ID") %>' Width="160px" AutoPostBack="True">
    </asp:DropDownList>
    </EditItemTemplate>
    <InsertItemTemplate>
    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("RANK") %>'></asp:TextBox>
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label4" runat="server" Text='<%# Bind("RANK") %>'></asp:Label>
    </ItemTemplate>
    <HeaderStyle Font-Bold="True" />
    </asp:TemplateField>
    <asp:TemplateField HeaderText="FIRST_NAME" SortExpression="FIRST_NAME">
    <EditItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FIRST_NAME") %>'></asp:TextBox>
    </EditItemTemplate>
    <InsertItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FIRST_NAME") %>'></asp:TextBox>
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Bind("FIRST_NAME") %>'></asp:Label>
    </ItemTemplate>
    <HeaderStyle Font-Bold="True" />
    </asp:TemplateField>
    <asp:BoundField DataField="LAST_NAME" HeaderText="LAST_NAME" SortExpression="LAST_NAME">
    <HeaderStyle Font-Bold="True" />
    </asp:BoundField>
    <asp:TemplateField HeaderText="CERTDATE" SortExpression="CERTDATE">
    <EditItemTemplate>
    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("CERTDATE") %>'></asp:TextBox>
    </EditItemTemplate>
    <InsertItemTemplate>
    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("CERTDATE") %>'></asp:TextBox>
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label2" runat="server" Text='<%# Bind("CERTDATE") %>'></asp:Label>
    </ItemTemplate>
    <HeaderStyle Font-Bold="True" />
    </asp:TemplateField>
    <asp:BoundField DataField="INSERVICE" HeaderText="INSERVICE" SortExpression="INSERVICE">
    <HeaderStyle Font-Bold="True" />
    </asp:BoundField>
    <asp:CommandField ShowEditButton="True" />
    </Fields>
    <EditRowStyle Font-Size="Medium" />
    </asp:DetailsView>

    Here is the code behind:

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
    Dim z As String
    z = CType(dvEmployees.FindControl("TextBox2"), TextBox).Text
    End Sub

    Thank you

     

  • Re: FindControl is not working in DetailsView

    04-14-2008, 4:20 PM
    • Loading...
    • xyz789
    • Joined on 01-29-2007, 5:43 PM
    • Posts 105

     

  • Re: FindControl is not working in DetailsView

    04-14-2008, 5:57 PM
    • Loading...
    • anas
    • Joined on 09-21-2006, 4:31 AM
    • Jerusalem