0

我需要能够在数据绑定上以编程方式从我的详细信息视图中修改控件。现在我正在使用此代码,但出现“索引超出范围”错误。

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

我认为问题可能是页面最初加载时详细信息视图没有任何控件,因为在我在主网格视图中选择一行之前它没有获得它们。基本上,当我在 gridview 中选择一行时,我试图执行此代码,而不是在页面最初加载时。会是这样吗?如果是这样,如果不在 detailsview 数据绑定中,我应该在哪里执行此代码?

这是详细信息视图和相应的数据源标记:

<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>
4

3 回答 3

1

detailsview的数据源使用gridview的selectedvalue作为它的选择控制参数,并且在页面加载时gridview还没有selectedindex,所以detailsview是空的。我必须在页面加载时设置 gridview 的 selectedindex 才能解决问题。

于 2011-03-04T14:36:18.853 回答
0

似乎 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
于 2011-03-02T20:04:48.863 回答
0

您还可以将 detailsview 可见属性设置为 false On page Load Event

于 2012-05-30T19:06:48.723 回答