How do I access a control from a detailsview programmatically?

1.6k views Asked by At

I need to be able to modify controls from my detailsview programmatically on databind. Right now I'm using this code but I'm getting an "Index was out of range" error.

Private Sub dtlApplication_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtlApplication.DataBound
    Dim resumeLink As HyperLink = dtlApplication.Rows.Item(0).FindControl("lnkResume")
    resumeLink.NavigateUrl = "Resumes/"
End Sub

I also tried this but got an "Object reference not set to an instance of an object" error.

Private Sub dtlApplication_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtlApplication.DataBound
    Dim resumeLink As HyperLink = dtlApplication.FindControl("lnkResume")
    resumeLink.NavigateUrl = "Resumes/"
End Sub

I think the problem might be that the detailsview does not have any controls when the page initially loads since it doesn't get them until I select a row in my main gridview. Basically, I am trying to execute this code when I select a row in the gridview, not when the page initially loads. Could that be it? If so, where should I execute this code if not in the detailsview databound?

Here is the detailsview and corresponding datasource markup:

<asp:DetailsView ID="dtlApplication" runat="server" AutoGenerateRows="false"
                        DataKeyNames="appID" DataSourceID="ds2" CellPadding="0" BorderColor="Transparent" 
                        BorderWidth="0px" GridLines="None" HorizontalAlign="Left" Width="459" CssClass="dtlView">
                        <Fields>                                
                            <asp:TemplateField showheader="false">
                                <ItemTemplate>  

                                    <h3>Resume</h3>

                                    <asp:HyperLink runat="server" ID="lnkResume" Text="View Resume &raquo;"></asp:HyperLink>                                        

                                </ItemTemplate>
                            </asp:TemplateField>                                
                        </Fields>
                        <PagerSettings Mode="NextPreviousFirstLast" PageButtonCount="5" FirstPageText="&larr; First" LastPageText="Last &rarr;"
                            nextpagetext="Next &raquo;" previouspagetext="&laquo; Previous" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" CssClass="paging" />
                    </asp:DetailsView>
<asp:SqlDataSource ID="ds2" runat="server" ConnectionString="<%$ ConnectionStrings:cn %>" 
                         SelectCommandType="StoredProcedure" SelectCommand="sp_SelectApplicationDetail" 
                         EnableCaching="true" CacheDuration="600">  
                         <SelectParameters>
                            <asp:ControlParameter Name="appID" ControlID="gvAdmin" PropertyName="SelectedValue"></asp:ControlParameter>
                         </SelectParameters>         
                    </asp:SqlDataSource>
3

There are 3 answers

0
Industrial Themes On BEST ANSWER

The detailsview's datasource uses a gridview's selectedvalue as it's select control parameter, and upon page load the gridview does not have a selectedindex yet, so the detailsview is empty. I had to set the gridview's selectedindex on page load to fix the problem.

4
apros On

It seems that DataBound event is not a the best event for such issue. Try to use ItemCreated event event handler instead. Like here, for example:

Private Sub dtlApplication_ItemCreated(sender As Object, e As EventArgs) Handles dtlApplication.ItemCreated

    Dim someRow As  DetailsViewRow = dtlApplication.Rows(0);
    If someRow Is Nothing Then Exit Sub       
    Dim link As HyperLink = DirectCast(someRow.FindControl("lnkResume"), HyperLink)

    If link Is Nothing Then Exit Sub

    link.NavigateUrl = "Resumes/"
End
0
htm11h On

You can also set the detailsview visible property to false On page Load Event