如何以编程方式从详细信息视图访问控件?

我需要能够在databind上以编程方式修改我的detailsview中的控件。现在我正在使用此代码,但我收到“索引超出范围”错误。
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
我也尝试了这个,但得到了“对象引用没有设置为对象的实例”错误。
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
我认为问题可能是,在页面最初加载时,detailsview没有任何控件,因为在我在主gridview中选择一行之前它没有得到它们。基本上,当我在gridview中选择一行时,我尝试执行此代码,而不是在页面最初加载时。可能是吗?如果是这样,如果不在详细信息视图数据绑定中,我应该在哪里执行此代码? 这是详细信息视图和相应的数据源标记:
<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>
    
已邀请:
detailsview的数据源使用gridview的selectedvalue作为其选择控制参数,并且在页面加载时gridview还没有selectedindex,因此detailsview为空。我必须在页面加载时设置gridview的selectedindex以解决问题。     
似乎DataBound事件不是此类问题的最佳事件。尝试使用ItemCreated事件事件处理程序。像这里一样,例如:
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
    
您还可以将detailsview visible属性设置为false在页面上加载事件     

要回复问题请先登录注册